milliseconds time conversion problem
4 次查看(过去 30 天)
显示 更早的评论
I'm trying to print the milliseconds field with the following
DS = datestr(MM,'HH:MM:SS.FFF')
the SS.FFF field is rounded up to the nearest second
ie. 59.000
So on the command line I input
K>>MM = .9833
then K>> ZZ = MM * 60.00
the returned value for ZZ on the command line is
59.00
The interpreter shouldn't know if this is anything other than a double multiplied by a double returning a double not a time value.
How do I get around this and how can I print HH:MM:SS.FFF in my code?
4 个评论
Walter Roberson
2011-6-23
JD = 97.9833
>> datestr(JD,'HH:MM:SS.FFF')
ans =
23:35:57.120
Notice this is different than the times you get. I am using 2008b.
It does not appear to be single() vs double() as in my release datestr() will not accept single()
Arithmetically, consider
>> .9833 * 24*60
ans =
1415.952
That is, 0.9833 of a day is 1415 full minutes and 0.952 partial minutes. Multiply that by 60 and you get 57.12 -- 57 seconds and 120 milliseconds, just as is calculated on my system.
Please repeat those calculations on your system and see what gets returned.
Walter Roberson
2011-6-23
Note that if JD is a Julian Day then there needs to be a 1/2 day conversion and possibly a timezone conversion in order to get the local time. There are different Julian Day standards with different corrections; see http://en.wikipedia.org/wiki/Julian_day
These corrections could account for the hour discrepency (23 vs 17), but I do not see how they could account for the difference in minutes and seconds.
回答(2 个)
Walter Roberson
2011-6-23
Michael, which MATLAB version are you using?
Also please check that none of your values are single precision numbers. In the time range of today, eps(datenum(now)) is about 11/10 of a millisecond so if you were using single precision or a far future date, you could run out of precision.
3 个评论
Walter Roberson
2011-6-24
That one is 52.9999996799961 seconds. Is it important that those 3/10-millionth of a second cause the result to be output as 52.999 ?
Michael
2011-6-23
5 个评论
Walter Roberson
2011-6-24
fix() and floor() truncate.
What you are looking at came in in R14, long before the R2008b that I am using, so it would not be responsible for any differences.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!