datetime with nanosecond precision, but arithmetic with millisecond precision?

17 views (last 30 days)
I need to process GPS-based time stamps that have 100ns precision. I have successfully read and stored them with this precision using the datetime data type. Now, I would like to do simple calculations, i.e. difference between time stamps. Any function for time arithmetic (diff, between) or direct subtraction of two datetime objects only provides me a result with millisecond precision, although my datetime data has 100ns precision.
How can I subtract two 100ns precision time stamps and still get a 100ns precision result in Matlab?
  1 Comment
dpb
dpb on 19 Jun 2018
Help us out with attaching a dataset to play with but I believe calculations are in full precision but the default display for durations is msec. You can set .SSSS fractions but that's probably rather inconvenient.
I'm guessing you may need to write some specialty functions; the datetime stuff really isn't made for other than just human calendar scale stuff.

Sign in to comment.

Answers (2)

Peter Perkins
Peter Perkins on 5 Jul 2018
As Walter says, datetime and duration* should have no trouble with this. I suspect this is just a format issue:
>> d = datetime(2018,7,5,0,0,0,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS') + milliseconds((0:5)'/1e4)
d =
6×1 datetime array
05-Jul-2018 00:00:00.000000000
05-Jul-2018 00:00:00.000000100
05-Jul-2018 00:00:00.000000200
05-Jul-2018 00:00:00.000000300
05-Jul-2018 00:00:00.000000400
05-Jul-2018 00:00:00.000000500
>> dt = diff(d);
>> dt.Format = 's'
dt =
5×1 duration array
1e-07 sec
1e-07 sec
1e-07 sec
1e-07 sec
1e-07 sec
  • duration will start having round-off issues at 100ns precision when representing elapsed times larger than about 14 years. Not sure if your data are like that.

Walter Roberson
Walter Roberson on 19 Jun 2018
The calculation does have nanosecond precision; however the default formats do not display it.
aaa = seconds(sort(rand(1,2)));
aaa.Format = 's' %the default for duration objects created with seconds(), but used for emphasis
bbb = diff(aaa);
bbb.Format = 's'
aaa =
1×2 duration array
0.0975404049994095 sec 0.278498218867048 sec
bbb =
duration
0.180957813867639 sec
>> sym('0.278498218867048') - sym('0.0975404049994095')
ans =
0.1809578138676385
so the 15th digit is rounded in the display of bbb.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!