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

Guillaume
Guillaume 2020년 3월 4일
"where is the problem?"
Your input is not a posix time, most likely. What is the value of t_ne?
Valeria Leto
Valeria Leto 2020년 3월 4일
it's 1582650648869329937

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2020년 3월 4일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2020년 3월 4일

댓글:

2020년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by