How can I parse a timestamp in the format yyyy/doy/hh/mm/ss
Mostrar comentarios más antiguos
I am running R2014b. How can I parse a UTC timestamp in the format yyyy/doy/hh/mm/ss where doy (day of year) is an integer value 1 to 366. I want to convert doy/hh/mm/ss into seconds.
1 comentario
Stephen23
el 3 de Dic. de 2016
With R2014b you can use datetime objects to achieve this. For earlier versions you can use my FEX submission datenum8601, which converts ISO 8601 dates and timestamps to serial date numbers. It would be easy to apply to your use case, you would just need to change the separator characters to match the ISO 8601 standard. Note that the ISO standard calls the DOY timestamp an "ordinal" date.
Respuesta aceptada
Más respuestas (2)
Str = '2016/183/13/20/15';
dv = sscanf(Str, '%d/%d/%d/%d/%d', [1, 5]);
sec = dv(2:5) * [86400; 3600; 60; 1] - 86400;
The subtraction of 86400 considers, that the doy start at 1 and not at 0.
This works for cell strings also very efficiently:
CStr = {'2016/183/13/20/15', '2016/194/08/20/15'};
Str = sprintf('%s*', CStr{:});
dv = sscanf(Str, '%d/%d/%d/%d/%d*', [5, inf]).';
sec = dv(:, 2:5) * [86400; 3600; 60; 1] - 86400;
Peter Perkins
el 2 de Dic. de 2016
You have R2014b, use datetime:
>> str = '2016/183/13/20/15';
>> d = datetime(str,'InputFormat','yyyy/DDD/HH/mm/ss')
d =
01-Jul-2016 13:20:15
Seconds since ... I think you mean start of year:
>> t = d - dateshift(d,'start','year')
t =
4381:20:15
>> s = seconds(t)
s =
15772815
But "seconds since 1970" is also common:
>> t = posixtime(d)
t =
1467379215
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!