Convert NTP 64 format to date and time

41 views (last 30 days)
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!

Accepted Answer

Federico
Federico on 14 Jan 2016
Edited: Federico on 14 Jan 2016
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)

More Answers (2)

Star Strider
Star Strider on 8 Jan 2016
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 on 8 Jan 2016
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 Comments
Walter Roberson
Walter Roberson on 8 Jan 2016
I would expect ntp64(2)/2^32 rather than 1/ntp64(2)
Peter Perkins
Peter Perkins on 14 Jan 2016
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.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!