datetime comparison fails but datenum works
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Resul Al
el 28 de Jun. de 2023
Comentada: Peter Perkins
el 17 de Jul. de 2023
clear all
datetime.setDefaultFormats('default','yyyy-MM-dd HH:mm:ss.SSS')
load sample_t t
t
t.EndDate(2)
t.StartDate(3)
t.EndDate(2)<=t.StartDate(3) % false
t.EndDate(3)<=t.StartDate(4) % true
datenum(t.EndDate(2)) <= datenum(t.StartDate(3)) % true
0 comentarios
Respuesta aceptada
Stephen23
el 28 de Jun. de 2023
Editada: Stephen23
el 28 de Jun. de 2023
"datetime comparison fails but datenum works"
Actually DATENUM fails but DATETIME works.
The dates you uploaded have a higher precision than the micro-seconds that you show. Lets have a look:
S = load('sample_t.mat');
T = S.t
fprintf('%64.42f\n',T.StartDate.Second)
fprintf('%64.42f\n',T.EndDate.Second)
Apparently you expect EndDate(2) <= StartDate(3), but it is clear that for the values you uploaded this is not true: from the 14th(?) fractional digit of the seconds unit the EndDate(2) is larger than the StartDate(3). This is around the binary floating point limit for the double class, which implies that this is just some accumulated floating point error.
DATENUM does not really work better, it just ignores the high precision of the DATETIME data by smudging it into one lower-precision scalar double. If the data are different (like yours), then the logical operation should reflect this, not hide it. So DATETIME actually works, just as it should.
You could include a tolerance in the comparison, e.g.:
tol = seconds(0.0005);
(T.StartDate(1:3)-T.EndDate(2:4)) < tol
1 comentario
Peter Perkins
el 17 de Jul. de 2023
Right!
Not clear where these timestamps came from, but if they were originaly datenums, or excel serial date numbers. Same is true for some other numeric representations, so you need to be careful if you expect to work with timestamps with sub microsec precision (sub-muillisec for datenums, really).
Hard to give advice without knowing where your timestamps came from.
Más respuestas (0)
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!