Bad time format, import to datetime
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Stephen23
2022년 5월 4일
편집: Stephen23
2022년 5월 4일
"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
Stephen23
2022년 5월 5일
"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
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!