Hi,
I am trying to use this command to convert the time vector t_ne expressed in posix time to a date
d = datetime(t_ne, 'ConvertFrom', 'posixtime')
but I get
datetime
5.0152e+10 CE
where is the problem?

2 Kommentare

Guillaume
Guillaume am 4 Mär. 2020
"where is the problem?"
Your input is not a posix time, most likely. What is the value of t_ne?
Valeria Leto
Valeria Leto am 4 Mär. 2020
it's 1582650648869329937

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Mär. 2020

0 Stimmen

This is a bit tricky because you are working past double precision accuracy.
t_ne = uint64(1582650648869329937);
NS = 1e9;
right_over = mod(t_ne, NS);
left_over = t_ne - right_over;
d = datetime( double(left_over)/NS, 'convertfrom', 'posixtime', 'Format', 'dd-MMM-uuuu HH:mm:ss.SSSSSSSSS') + seconds(double(right_over)/NS)

4 Kommentare

Valeria Leto
Valeria Leto am 5 Mär. 2020
it worked! I don't kwow why..but it worked...Thanks
Guillaume
Guillaume am 5 Mär. 2020
"I don't kwow why..but it worked"
First, as I suspected, your t_ne is not a posix time. Posix time is the number of seconds since 1st January 1970. Your number appears to be the number of nanoseconds since 1st January 1970. So, it's 1e9 times the magnitude it should be, which brings another problem as Walter mentioned: the number is too big to be stored accurately as double.
With the magnitude of your timestamp, if you store it as double you'll be rounding by up to 256 nanoseconds. Perhaps, you don't care about the nanesconds and microseconds, in which case:
d = datetime(double(t_ne)/1e9, 'convertfrom', posixtime', 'Format', 'dd-MM-uuuu HH:mm:ss.SSSS');
is simpler. Walter's code does some arithmetic to preserve the nanoseconds part accurately. For 1582650648869329937, this results in a difference of 17 ns between the two.
Note that if you're reading the timestamps from a file and are not careful to read them as 64-bit integers, then you'll have lost the nanosecond precision anyway.
Walter Roberson
Walter Roberson am 5 Mär. 2020
Yup, the complications are to preserve the 17 nanoseconds.
Valeria Leto
Valeria Leto am 7 Mär. 2020
thanks a lot! very kind :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by