Datevec to Datetime conversion results in 1 millisecond error?

15 ビュー (過去 30 日間)
Brad
Brad 2015 年 10 月 28 日
コメント済み: Brad 2015 年 10 月 28 日
When converting a datevector to a datetime, it appears that I'm losing a millisecond and I can't figure out why. It doesn't appear to be a rounding or formatting issue. Any help on what's going on here would be much appreciated.
Thanks,
Brad

採用された回答

Peter Perkins
Peter Perkins 2015 年 10 月 28 日
Screen shots are probably not the most convenient way for others to diagnose this problem.
You haven't lost a millisecond, you never had it to begin with. Here's the deal:
>> dv = [2015 8 13 21 6 19.172; 2015 8 13 21 6 19.180]
dv =
2015 8 13 21 6 19.172
2015 8 13 21 6 19.18
You only think that last value is 19.18. It isn't. It's the floating point value that's closest to 19.18, which happens to be just less than the real number 19.18. The display simply shields you from that. With "numbers", that's probably what you'd want display to do, but in "time", you'd probably be unhappy if the display for something just before midnight rounded up to the next day. So:
>> dt = datetime(2015,8,13,21,6,[19.172 19.180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.179999
Your problem is that by the time you've typed "19.18", you're done for. datetime has the precision to give you what you want, though:
>> dt = datetime('13-08-2015 21:06:19.180','Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.180000
>> dt = datetime(2015,8,13,21,6,19,[172 180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.180000
  1 件のコメント
Brad
Brad 2015 年 10 月 28 日
Excellent! So it WAS a floating point / rounding / display issue. Great example process. Very clear and easy to follow.
You're right, the screenshot is difficult to see and work with. I will post inline code for future questions and answers.
Many thanks,
Brad

サインインしてコメントする。

その他の回答 (1 件)

Ingrid
Ingrid 2015 年 10 月 28 日
I do not seem to be able to reproduce this deviation, so how did you obtain the DV and DT variables?
>> timeVec = [2015 8 13 21 6 19.1800]
timeVec =
1.0e+03 *
Columns 1 through 5
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000 0.006000000000000
Column 6
0.019180000000000
>> timeNum = datenum(timeVec)
timeNum =
7.361898793886574e+05
>> timeVecBack = datevec(timeNum)
timeVecBack =
1.0e+03 *
Columns 1 through 4
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000
Columns 5 through 6
0.006000000000000 0.019180000000008
  1 件のコメント
Brad
Brad 2015 年 10 月 28 日
編集済み: Brad 2015 年 10 月 28 日
Hi Ingrid,
I'm using the 'datetime' data type, not serial date numbers as you have done with 'datenum()'.
The DV (as in "datevector") and DT (as in "datetime) variables were obtained from timestamp metadata from TIFF images. I converted this serial string data to a datevector, and then to a datetime to be able to easily correct and fix timestamp issues.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by