Datevec to Datetime conversion results in 1 millisecond error?

21 views (last 30 days)
Brad
Brad on 28 Oct 2015
Commented: Brad on 28 Oct 2015
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

Accepted Answer

Peter Perkins
Peter Perkins on 28 Oct 2015
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 Comment
Brad
Brad on 28 Oct 2015
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

Sign in to comment.

More Answers (1)

Ingrid
Ingrid on 28 Oct 2015
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 Comment
Brad
Brad on 28 Oct 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!