Saturday, June 30, 2012

3...2...1...1... Happy Leap Second (and a merry Linux to all)!


Science is awesome, and so is time. With the advent of better measurement devices, such as clocks, humans are able to calculate the time it takes for one day on earth to occur with the utmost precision. This time is known as the mean solar time, and our best representation of this is represented by the Coordinated Universal Time (UTC) measurement [1]. This measurement standard defines a common frame of reference for which the world can tick off of. Effectively, every time zone is +/- so many hours with respect to UTC. As technology has progressed, so has the capability for man to more accurately measure the length of a day. In fact, a day from the perspective of a mean solar day lasts 86,400 seconds. The second is a unit of measure in the International System of Units (SI) which the world recognizes for maintaining the definitions of weights and measures. The SI value for a second is defined as being "the duration of 9,192,631,770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium 133 atom" [2]. In other words, an atomic clock based on atoms jumping around, and a counter to count these jumps, can be used to measure the duration of a second, and can be further used to measure the length of a day.

This awesome precision in measuring time is so accurate that we now can measure very small amounts of error between our actual astronomical day (mean solar day) and the clock. How do we account for such discrepancies, despite how small, between the astronomical universe, and our very earthly clocks? ... wait for it... wait for it... in the name of Van Halen I proclaim Jump seconds... err I mean "Leap Seconds." Every so often, and it does not appear to be periodic, we can either add or subtract twice a year a leap second from our clocks. This does not occur every year, see the chart linked here [1], but it can occur twice, or at least, it has once before. Scientists determine when a leap second should occur, and then they insert these puppies to the UTC representation of time at June 30th and/or on December 31st. If the choice is to add a second, then the time on one of the chosen dates will be 23:59:60 (not 23:59:59 marking the end of a normal non-leap-second-day). If the choice is to remove a second, then the removal will be 23:59:58 ending the day. Pretty sweet huh? These announcements for leap seconds are made by the International Earth Rotation and Reference Systems Service (IERS). The most recent update can be found here.

Shootin' off a Leap Second and Linux

So, now you might just be saying: "Sweetness so these French dudes choose when to mess with our clocks." Well, shall I clarify, nicely pamper our clocks so that they become more accurate to the ever pulsating state of our universe, e.g. make more accurate. Anyways, I've been a Linux guy for years, and I do occasionally like to know how things work. How does this magic in the box (GNU/Linux) handle this second bumping? Well, it is all handled as a tightly coupled dance between the userland NTP daemon and the Linux kernel. Upon boot, the Linux kernel registers a high resolution timer (hrtimer) to trigger the insert/removal of a leap second if needed. This acts like a loaded gun. The NTP daemon, which runs in userland, is responsible for detecting either through the 'leap indicator' flag in the NTP packet from a high-stratum NTP server or from a data file that specifies leap seconds. Since leap seconds are announced 6 months in advance of the event [6], it should be no surprise. Further, the leap indicator sent in an NTP packet is a warning from a higher-stratum server to its peers saying that "Hey guys... we got a leap second on the way at the end of the current month" [5]. On the day that the leap second is to occur, the communication between userland and Linux kernel happens with the daemon telling the kernel about the nature of the leap second (either to add or remove one). The method of dispatch is the 'adjtimex' system call which the userland daemon passes information to the NTP code in the kernel. This will trigger the hrtimer to add or remove a leap second at the end of the day. So, the kernel basically will start its timer, the loaded gun that has been waiting since boot, and set it to go off at the end of the day. And when that occurs.... *bang* leap second goodness! Oh, and the next leap second is just in a few hours June 30 at 23:59:60. Careful readers will remember that such notation means we are adding a second. So sleep in my friends!





  2. This is cool

    In the date and timestamp formats, the prime epoch, or base date of era 0, is 0 h 1 January 1900 UTC, when all bits are zero. It should be noted that strictly speaking, UTC did not exist prior to 1 January 1972, but it is convenient to assume it has existed for all eternity, even if all knowledge of historic leap seconds has been lost.

    [5] NTP Version 4 RFC 5905: