Convert NTP 64 format to date and time
이전 댓글 표시
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:
- how do I parse it? as 2 UINT32 numbers (first 4 bytes and then remaining bytes)?
- how do I convert it to a date and time?
Thanks!
채택된 답변
추가 답변 (2개)
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
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
2016년 1월 8일
I would expect ntp64(2)/2^32 rather than 1/ntp64(2)
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.
카테고리
도움말 센터 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!