How NTP Represents the Time (Computer Network Time Synchronization)

In a very real sense, this topic is all about time values. Every computer operating system has means to read the system clock and return a time value representing the current time of day in one format or another. Time values are ephemeral; in principle, every reading returns a value greater than the previous reading. In other words, time never stands still or runs backward. Since time values are represented in words or structures with a limited number of bits, they can represent only a limited range of time, called an era, and a limited resolution within the era.

Most of the discussion in this topic is centered about NTP, so its time format is a natural choice, as shown in Figure 2.1. There are two formats: a 64-bit unsigned timestamp format, which represents time in packet headers exchanged between clients and servers, and a 128-bit signed datestamp format, which can represent dates from the dawn of the universe to when the Sun grows dim. In both formats, the high-order bits represent time in seconds since the NTP base epoch in era 0, 0 h 1 January 1900, while the low-order bits represent the fraction of the second.

NTP time formats.

FIGURE 2.1 NTP time formats.

In datestamp format, era 0 represents the years from the beginning in 1900 to some time in 2036 when era 1 begins, while era -1 represents the years from 1900 to some time in 1854 when era -2 ended. In timestamp format, the years span from the beginning of an era to the end of that era 136 years later, while in datestamp format the years span the age of the universe. In time-stamp format, the second can be represented to 232 ps, which seems tiny, but computers are getting so fast that this might soon be the precision-limiting factor. In datestamp format, the second can be represented to about 500 atto-seconds, or about a tenth of the time light takes to pass through an atom.

There is a special value for both formats when both the seconds and fraction fields are zero, which designates a condition in which the system clock is unsynchronized. Datestamp values are considered twos complement, signed values as used in ordinary 128-bit arithmetic, so negative values represent times before era 0. Timestamp values are considered unsigned; the only computations allowed are differences between 64-bit values producing a 63-bit signed result. Furthermore, the calculations producing clock offset and round-trip delay are differences between 63-bit signed values producing a 62-bit signed result. The 30-bit signed seconds field can represent only from 34 years in the past to 34 years in the future. This is an intrinsic characteristic of any time synchronization protocol using 64-bit integer arithmetic. However, while it is necessary to use 64-bit integer arithmetic for the first-order differences to preserve precision, the second-order differences can be done after conversion to 64-bit floating-double representation without diminishing precision.

There are only two operations possible with time values: subtraction of one time value from another (time interval) and signed addition of a constant to a time value (time adjustment). Having survived the millennium rollover in 2000, the anxious reader will ask what happens when era 0 rolls over in 2036? The answer is that as long as the computer clock is set within 68 years of the correct time, NTP will synchronize correctly, even if an adjustment spans an era boundary.

To set the clock correctly means setting the era number, which presumably comes from the real-time clock or the file system. It does not have to be accurate, just within 68 years of the time to which NTP will eventually steer. To see why this is so, consider the subtraction operation in twos-complement arithmetic. First, the second number (subtrahend) is ones complemented, then incremented by one, then added to the first number. Assume the two numbers are in adjacent eras but within 68 years of each other. Then, the sum of the two numbers considered as a signed number cannot overflow the 32-bit seconds field.

Note with emphasis that nothing in this topic says anything about the system clock format, whether seconds and microseconds, seconds and nanoseconds, or seconds and fraction. To be consistent with present-day means that to disseminate conventional civil time, the NTP timescale is coincident with UTC, including leap seconds. It says nothing about the local time zone or standard/daylight time, which is established by the operating system. It says nothing about how NTP timestamps are converted to external representation or displayed in a message or on the task bar. These things are completely determined by the operating system and application programs that interpret the system clock values.

Next post:

Previous post: