1 = Rollover issues in time sources
2 include::include-html.ad[]
8 * link:#intro[Introduction]
9 * link:#terminology[Terminology]
10 * link:#cycles[Major cycles of interest]
11 * link:#gps_pivots[GPS pivot dates]
12 * link:#ntp_pivots[NTP pivot dates]
17 All time-handling software and hardware suffers from a fundamental
18 problem: finite-sized data representations for times means that,
19 sooner or later, your time counter is going to roll over to zero. If
20 you are lucky, the space you have to represent time is large enough
21 that you can expect not to observe a rollover during the expected
22 lifetime of the universe (or whatever portion of it concerns you).
23 On real-world hardware you will seldom be that lucky.
25 This page discusses calendar cycles related to NTP, the ways they
26 interact, the consequences of rollover and common workarounds for
32 Every calendar cycle has at least three basic properties: a start date
33 (usually referenced to a solar astronomical date in the Gregorian
34 calendar), a unit (such as seconds) and a rollover time or cycle
35 length in that unit. It also matters what the calendar does about
38 As an important example, the basic time representation used on
39 computers running Unix has a start date of 1970-01-01T00:00:00
40 (midnight of January 1st 1970), a unit of seconds and a rollover time
41 of either 2^31^ seconds or 2^63^ seconds, depending on the word size
42 of the hardware. Negative times represent seconds before the start
45 The start date is often called the "epoch" and a rollover period may
46 be called an "era". Beware that eras are often numbered zero-origin;
47 it is likely that Unix software will refer to dates immediately before
48 the 2038 rollover as "era 0", not "era 1".
50 Rollover problems arise when a counter wraps to zero, so that a system
51 behaves or reports as though time has warped into the far past. The
52 consequences can range from trivial to catastrophic; time-dependent
53 software enters previously unexplored regions of its behavior space
54 and may crash or deliver nonsensical results. Because these edge
55 cases are difficult to test, they are defect attractors even when
56 system designers have been careful and conscientious.
59 == Major cycles of interest
61 For purposes related to NTP there are four major cyclic calendars of
62 interest: two-digit years, Unix time, NTP time, GPS time (because GPSes
63 are so often used as primary time sources).
65 === Two-digit year reports
67 A well-known historical rollover problem was "Y2K", which derived from
68 the use of two-digit decimal year counters in mainframe software
69 (written when storage was so limited that longer counters would have
70 been dramatically more expensive). Expensive preparation and
71 mitigation work prevented any major disasters in the year 2000, but
72 this was in part because there were many fewer vulnerable systems than
75 Alas, Y2K-like problems are not dead. Some will persist as long
76 as bad old time-source hardware that only reports two-digit years
79 Many GPSes have this problem due to NMEA0183's inadequacies as a
80 reporting format (and analogous problems in various proprietary GPS
81 protocols). Unless the device ships a particular $GPZDA sentence
82 (which many do not) the year parts of time stamps are the low two
85 Dedicated non-GPS reference-clock hardware designed in the 20th
86 century frequently also report only two-digit year dates - for
87 example, the Arbiter and Spectracom devices supported by this
88 distribution. This problem may persist in post-2000 devices that
89 are designed for compatibility to use the same reporting protocols,
90 such as Spectracom's "Type 2".
92 There is an internal workaround to deal with two-digit dates in
93 {ntpdman}, but it relies on the system clock being at least roughly
94 correct - it won't work (for example) at boot time if the clock
95 has been trashed or zeroed due to a failed battery backup and
96 you have no remote check peers yet.
98 Warnings have been attached to the documentation of NTP clock drivers
99 that may have a vulnerability here. Notably, the NMEA driver
100 does *not* have this problem; it
101 uses a clever calendrical trick to deduce a 4-digit year
102 that should work until 2399.
106 The basics of Unix time have already been described.
108 32-bit Unix time will roll over at 2038-01-19T03:14:08, beginning its
109 second era. It will not roll over to zero but rather to the minimum
110 *negative* Unix time, which will report as 1901-12-13T20:45:52.
112 Unix time is leap second-corrected. When a leap second is inserted,
113 the Unix counter stutters - increases during the leap second, then
114 drops back a second so that the same counter value represents two
115 distinct times. If a leap second were to be deleted, the counter
116 would skip that second.
118 The possibility of stutter or skip also means that in order to compute
119 elapsed time in seconds between two Unix times, you need to know
120 the leap second offset at each time. It is not guaranteed that
121 these offsets are the same if the interval crosses midnight at the
122 end of any calendar month. To date in 2018, IERS (the International
123 Earth Rotation service) has performed all corrections just after
124 midnight of June 31 and December 31 respectively.
126 The purpose of leap second correction is to keep Unix time
127 synchronized to the solar calendar. With it, Unix time after
128 1972-01-01T00:00:00 (the start date of UTC, Universal Coordinated
129 Time) coincides with UTC. Unix time before 1972 was an approximation
130 of GMT (Greenwich Mean Time) without leap seconds.
132 On POSIX-compliant Unix systems (which are now effectively all) Unix
133 time can be retrieved with nanosecond precision; some older versions
134 supported only millisecond precision. The subseconds counter is not
135 involved in any of the rollover or leap second issues with the
138 The Unix 32-bit rollover may fizzle the way Y2K did, and will if a
139 large enough percentage of 32-bit computers are replaced with 64-bit
140 systems (the time counters on those won't roll over for approximately
141 292 billion years, which is twenty times the lifetime of the universe
142 so far). There is, however, reason for worry; various Unix
143 filesystems, binary file formats, and databases even on 64-bit systems
144 use 32-bit fields. A more serious worry is embedded 32-bit hardware
145 quietly ticking away in life-critical systems everywhere from medical
150 NTP time has a 32-bit cycle with an epoch of 1900-01-01T00:00:00. NTP
151 time is unsigned; the cycle is thus twice that from a signed 32-bit
152 counter and the era 1 rollover will be on 2036-01-08. A detailed
153 discussion can be found at
154 https://www.eecis.udel.edu/~mills/y2k.html[The NTP Era and Era
155 Numbering]. NTP time has a subsecond part in units of 2^-32^
156 seconds, which does not participate in the seconds counter's rollover
159 NTP time is leap second corrected, and the reference implementation
160 may exhibit stutter or skip.
162 There is a technique called
163 https://developers.google.com/time/smear["leap smearing"] used
164 by some servers that avoids these by changing the length of the issued
165 second slightly before and after the leap. The AWS equivalent is at
166 https://aws.amazon.com/blogs/aws/look-before-you-leap-the-coming-leap-second-and-aws/["Look Before You Leap"] .
168 Leap Smear is supported by NTPSec, please see
169 link:leapsmear.html[Leap Second Smearing with NTP] .
173 Raw GPS time is expressed as weeks since the GPS epoch
174 1980-01-06T00:00:00, and seconds within the week. Due to cost
175 constraints at the time the system was designed, the week counters are
176 only 10 bits long; thus, GPS time has a period of 1024 weeks (a bit
177 over 19 years). At time of writing, two GPS rollovers
178 have occurred; the first at 1999-08-22T00:00:00 and the second at
179 2019-04-07T00:00:00. Note that these times are "GPS Time", not
180 UTC, the corresponding UTC was 2019-04-06T23:59:42Z , ie a
181 difference of the 18 leapseconds that had elapsed since the
184 It is planned that future GPS satellites will increase the week
185 counter length to 13 bits, for a period of 8192 weeks or more
188 (GPS was originally designed for military geolocation, not time
189 service. It does not seem to have occurred to the originators that
190 long-service GPS clocks would ever be built.)
192 Raw GPS time does not include subseconds, but does ship a
193 top-of-second notification accurate to 50ns. Receivers are expected
194 to compose this with message data to report time at subsecond
195 precision. Many, however, do not, and those that do are often
196 inaccurate; consumer-grade hardware often has jitter of over a tenth
197 of a second (100ms). On the other hand, carefully designed GPSes
198 easily deliver 1ms accuracy, and some do 1000 times better than that. For
199 a more detailed discussion of accuracy budgets see the
200 https://gpsd.io/time-service-intro.html[Introduction to Time
203 Raw GPS time is not leap second corrected, but the satellite messages
204 include a leap second offset field in a special update approximately
205 every 20 minutes. If the receiver firmware is not carefully designed
206 there will be a window between device boot time and the next
207 leap second update, and around leap second corrections, during which
208 the reported second will be incorrect.
210 The qualifications about careful design are important because GPS
211 firmware - especially in inexpensive consumer-grade devices - is
212 often low-quality and poorly tested due to vendors trying to squeeze
213 every possible dime out of their costs. Even major vendors like
214 SiRF have a history of embarrassing firmware glitches, and the
215 semi-anonymous outfits in Shenzhen and Taiwan are worse.
217 Cheaper, consumer-grade, GPS devices also suffer from early EOL, and
218 manufacturers may never release firmware updates.
220 The worst impact of careless, low-budget design when using a GPS as
221 an NTP clock is not around either jitter or leap second corrections,
222 but rather the behavior near GPS and century rollovers.
225 == GPS pivot dates and rollover compensation
227 To yield a UTC date, the weeks+second information from the GPS satellites
228 has to be used as an offset to a base date.
230 The most naive way to do this would be to simply use the zero date of the
231 current GPS era (1024-week cycle) as the base. Some very early GPS
232 equipment seems to have worked this way, but it has the drawback that
233 the device time-warps at the next GPS era rollover - which, if your
234 device first shipped only a few years before that rollover is a
237 There is a relatively simple way to extend the useful life of a GPS
238 used as a clock. That is to choose a pivot date in GPS weeks. When
239 your device reports a week number *before* the pivot, you assume that
240 a GPS rollover has occurred and add 1024 weeks to the base date.
242 This technique will postpone time-warping to a full 1024 weeks after
243 the pivot date - which the vendor typically makes the release date
244 of the device firmware. But it won't do any better than that; without the
245 ability to reconfigure the base date, the device will ship incorrect
246 reports forever afterwards. A few refclocks, like the HP-GPS line,
247 have that ability; most do not.
249 This is where careless, low-budget design begins to matter. GPS vendors
250 do not document even the fact that they use base or pivot dates, let
251 alone what the base and pivot dates for their devices are. As of 2018,
252 NTP's developers do not know of any devices for which you can even
253 query these parameters, let alone set them. The best you can generally
254 do (and on only some devices) is get the firmware release date; from this,
255 you can assume that you will get non-timewarped reports for 1024 weeks
256 after that and probably be right.
258 This is not entirely the GPS vendors' fault. The truth is, GPS
259 reporting protocols are an awful mess. The closest thing to a
260 standard is NMEA0183, originally designed for a different purpose; it
261 is poorly specified and has no standard device-control functions at
262 all, let alone any for querying base and pivot dates.
263 https://gpsd.io/[GPSD], the ubiquitous open-source GPS
264 manager daemon that shares some developers with NTPsec, works around a
265 lot of the general messiness, but can't solve this problem
266 because the device capabilities to address it are simply absent.
268 Finally, when you buy a GPS device, you do not know - and often will not be
269 able to discover - how far in the past its hidden pivot date is. This
270 means that time service operators using a GPS as a local Stratum 0
271 need to be aware of the possibility that their device could roll over
272 at any time (but especially near the zero days of a GPS era) and be
273 ready to compensate by dropping in a device with a newer pivot date.
275 Actionable advice that follows from this: If you don't know the
276 manufacturing date of a GPS-based device to within a couple of years,
277 don't trust that it won't be rolled over *the first time
278 you start it*. Shopping for cheap refclocks at surplus houses or
279 on the Web is particularly hazardous this way.
281 NTPsec includes a workaround option for these old devices. Each "g"
282 suffix on the 'time1' offset option for a device adds the number of seconds
283 in a 1024-week era; this corresponds to the 10-bit week counters used
284 in today's GNSS. "G" adds the number of seconds in the 8192-week era
285 of the future. Unfortunately, this will only be useful *after* you
288 A fairly bulletproof mitigation strategy, if you are running
289 autonomously, is to have multiple GPS clocks per server and replace
290 each individual one at least once every nine years (half a GPS era).
295 For purposes of this discussion, a 'resolved' timestamp is one that
296 has been mapped from a 32-bit seconds offset in some era to a full
297 64-bit timestamp implicitly based in some known era.
299 When {ntpdman} receives an unresolved timestamp from an upstream server,
300 that timestamp could be based in any era prior to the current
301 wall-clock date - but by definition we may not know the wall-clock
302 date with confidence yet (not having achieved sync).
304 To resolve this ambiguity, NTP also uses an internal pivot date. It
305 chooses the era that would minimize the absolute value of the time
306 difference between the resolved timestamp and its internal pivot date.
308 This resolution technique is part of the NTP specification. It will
309 normally resolve to the current era, but may in cases where the pivot
310 is within a half-cycle of a rollover time resolve to either the
311 previous or the next era.
313 An {ntpdman} instance's pivot date will be the date it was compiled
316 It is fairly easy to see that this guarantees correct resolution if the
317 pivot dates of the communicating ntpds are within a half-cycle
318 (88 years) of each other, even if there is a rollover between the pivot
323 include::includes/footer.adoc[]