NTP Timestamp Reloaded

As I am still getting questions regarding NTP Timestamp calculation, I should reload this topic and provide more detail information 

About NTP Timestamp

1. NTP timestamp uses 64 bits representation and consists of 2 parts, first 32-bits is the Integer Part that represent the seconds and the next 32-bits is the Fraction Part that represent fractional of a second

According and referencing RFC 958 for point 2 and 3

 NTP timestamps are represented as a 64-bit fixed-point number, in
 seconds relative to 0000 UT on 1 January 1900 (See Point 2). The integer 
 part is in the first 32 bits and the fraction part in the last 32 bits, as
 shown in the following diagram.

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |                         Integer Part                          |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |                         Fraction Part                         |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

 This format allows convenient multiple-precision arithmetic and
 conversion to Time Protocol representation (seconds), but does
 complicate the conversion to ICMP Timestamp message representation
 (milliseconds).  The low-order fraction bit increments at about
 0.2-nanosecond intervals (See point 3), so a free-running one-millisecond 
 clock will be in error only a small fraction of one part per million, or
 less than a second per year.


2. First 32-bits is simply seconds. 2^32 bits seconds means it will rolls over every 136 years. NTP elapse since epoch of Jan 1, 1900

3. Next 32-bits is fractional of a second. In theory, it can have a resolution of 2^-32 seconds (233 picoseconds)

Calculating NTP Timestamp

In order to calculate NTP timestamp, you need the following step

1. Break your time value into 2 parts
     a) Integer Part that represent in seconds
     b) Fraction Part that represent in microsecond. (Note, you can use millisecond or others and simply do     the conversion)

2. Calculate the Integer Part. All you need is to make sure your seconds is represented since 1 January 1900 and assign it to the first 32-bits of NTP timestamp

3. Calculate the Fraction Part. This is the part that is most confusing. Let's think in this way. Imagine you save $10 when your original cost is $50 and  you want to know dollar discount in percentage, you will do the following: $10 / $50 * 100 = 20%. That actually means to represent your dollar discount in 100 parts. Same to calculating Fraction Part of NTP. Since NTP has a theoretical resolution of picoseconds, it is common to convert the fractional part of time value to picoseconds and represent the picoseconds in 2^32 parts. So, the generic formula will be (N in microseconds) / 10^6 * 2^32. After the calculation, assign the value to the next 32-bits of NTP timestamp.

Useful Link

Unix Timeval is very commonly used to compute NTP timestamp. Please note the following

1. tv_sec elapse since 1 Jan 1970, thus, you need to do a conversion to NTP second representation since 1 Jan 1900
2. tv_usec is in microsecond which is good for Fractional Part calculation
 

RFC 958 describe NTP Protocol http://tools.ietf.org/html/rfc958

Wiki provide a good overview of NTP http://en.wikipedia.org/wiki/Network_Time_Protocol

Comments

Popular posts from this blog

FFMPEG - Creating timelapse with FFMPEG with JPEG

MP4 File Format Part 1

RTSP - RTP over TCP