필터 지우기
필터 지우기

Convert NTP 64 format to date and time

조회 수: 34 (최근 30일)
Federico
Federico 2016년 1월 8일
댓글: Peter Perkins 2016년 1월 14일
Kind all,
I'm parsing some binary files which contains, amongst other things, a NTP64 timestamp. I know that the timestamp should be an UINT64 number for which the first 4 bytes represents seconds from an epoch (in this case 1/1/1900) and the last 4 bytes the fraction of second.
My questions are:
  1. how do I parse it? as 2 UINT32 numbers (first 4 bytes and then remaining bytes)?
  2. how do I convert it to a date and time?
Thanks!

채택된 답변

Federico
Federico 2016년 1월 14일
편집: Federico 2016년 1월 14일
Thanks all for the answers, unfortunately, since datetime is not available in my old MATLAB version, I had to follow another way:
- I have read the data as two uint32 as suggested by Star and Guillaume
- I have converted the fraction of seconds to a decimal number between 0 and 1 by dividing it by 2^32 (maximum number representable by a 32 bit integer), as suggested by Walter
- I have summed the seconds to the fraction of seconds, obtaining my total seconds from the epoch
- I have obtained the date number (precise to the milliseconds) by running
date_number = datenum([1900 1 1 0 0 total_seconds])
- I have got the human readable date and time by running
readable_datetime = datestr(date_number)

추가 답변 (2개)

Star Strider
Star Strider 2016년 1월 8일
Without data to test, I’m hesitant to attempt to write code to do the conversion. However, to get a decimal representation of the first 32 bits of a decimal number representing a 64-bit timestamp, this could be:
x = DATE STAMP DECIMAL REPRESENTATION
dbx = dec2bin(x,64);
d32_1 = bin2dec(dbx(1:32));
Beyond that, note that MATLAB date numbers are in terms of days and fractions, so you would need to convert seconds returned from the first 32 bits of the NTP64 timestamp to days and fractions to have them compatible with MATLAB date numbers. MATLAB also begins its date numbers with January 0, 0000, [0000 00 00] so the difference between that and [1900 01 01] would generate your offset to convert them to MATLAB date numbers.
I looked online to see if anyone had posted a MATLAB function to do this conversion, but I was unable to find one.

Guillaume
Guillaume 2016년 1월 8일
Read it as two uint32 bytes, and use the 'ConvertFrom', 'epochtime', 'epoch' constructor option of datetime:
ntp64 = fread(fid, 2, 'uint32');
d = datetime(ntp64(1) + 1/ntp64(2), 'ConvertFrom', 'epochtime', 'epoch', '1900-01-01')
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 1월 8일
I would expect ntp64(2)/2^32 rather than 1/ntp64(2)
Peter Perkins
Peter Perkins 2016년 1월 14일
Something to consider is that for contemporary timestamps, adding the two pieces together to get a double limits your precision:
>> eps(seconds(datetime('now') - datetime(1900,1,1))) ans = 4.7684e-07
So microseconds at best. Probably the actual accuracy of timestamps from NTP is lower than that, so no worries. But in general, see this other post for a way to retain more precision.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by