Bad time format, import to datetime
6 views (last 30 days)
Show older comments
Hi everyone,
because of my sensor, I have trouble importing the data. The output data is suboptimal, but I hope there is an easy solution. So this is the time format the sensor gave me, it is stored in a table column (195500x1 table) as strings
%Example strings in the table
"2021-07-18T17:41:22.1692422+02:00"
"2021-07-18T17:41:22.1732451+02:00"
"2021-07-18T17:41:22.1752471+02:00"
"2021-07-18T17:41:22.1792497+02:00"
"2021-07-18T17:41:22.1812511+02:00"
How can I safely read these strings with a complete precision (22.1792497), and store them in a variable (195500x1)? I would also like them to have the datetime format to match them with another datetime variable. later, I would like to convert them to duration.
Thanks!
0 Comments
Answers (1)
Stephen23
on 4 May 2022
Edited: Stephen23
on 4 May 2022
"Bad time format"
Good time format: it looks like a completely standard ISO 8601 timestamp
txt = [...
"2021-07-18T17:41:22.1692422+02:00"
"2021-07-18T17:41:22.1732451+02:00"
"2021-07-18T17:41:22.1752471+02:00"
"2021-07-18T17:41:22.1792497+02:00"
"2021-07-18T17:41:22.1812511+02:00"];
dtm = datetime(txt,'InputFormat','y-M-d''T''H:m:s.SSSSSSSZZZZZ','TimeZone','UTC')
dtm.TimeZone = '+2' % if you prefer
drn = timeofday(dtm);
drn.Format = 'hh:mm:ss.SSSSSSS'
So not the complete precision, but pretty close.
3 Comments
Stephen23
on 5 May 2022
"It needs to be completely precise"
DATETIME objects (and presumably DURATION too) ultimately use binary floating point numbers to store their data, which has inherent limited precision. If you want "completely" precise then those will be the wrong tools. However it is very unlilkely that the original data source had infinite precision time, so the quest is likely to be trying to get more precision out of data which simply does not exist...
Anyway, lets increase the displayed number of digits and see what happens
txt = [...
"2021-07-18T17:41:22.1692422+02:00"
"2021-07-18T17:41:22.1732451+02:00"
"2021-07-18T17:41:22.1752471+02:00"
"2021-07-18T17:41:22.1792497+02:00"
"2021-07-18T17:41:22.1812511+02:00"];
dtm = datetime(txt,'InputFormat','y-M-d''T''H:m:s.SSSSSSSZZZZZ','TimeZone','UTC');
dtm.TimeZone = '+2';
drn = timeofday(dtm);
drn.Format = 'hh:mm:ss.SSSSSSSSS'
"because i need to match the timepoints. Is that not possible?"
The recommended approach with floating point numbers is to compare the absolute difference against some acceptable tolerance. I don't see any reason why we can't do something similar with DATETIME/DURATION objects:
tol = duration(0,0,0.000001);
new = duration(17,41,[22.173244,22.173245]);
new.Format = 'hh:mm:ss.SSSSSSSSS'
abs(drn-new)<tol
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!