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 Comments

"where is the problem?"
Your input is not a posix time, most likely. What is the value of t_ne?
it's 1582650648869329937

Sign in to comment.

 Accepted Answer

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 Comments

it worked! I don't kwow why..but it worked...Thanks
"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.
Yup, the complications are to preserve the 17 nanoseconds.
thanks a lot! very kind :)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!