What every developer should know about leap seconds

adminguy's picture
Posted January 6th, 2016 by adminguy



Image Credit: The atomic clock made by Hewlett-Packard which was used as master clock for Japan Standard Time
I am going to start this new year with a series of articles which explain basic computer science concepts which every developer should know. This article is about leap seconds.
Everyone has heard about leap years but not many developers have heard of leap seconds. Just as a day is added to February every four years, leap seconds are frequently added to atomic time to coordinate it with the time determined by the earth's rotation. Let me begin a more detailed explanation with some terminology.
UT1: UT1 is a form of universal time which is based on the earth's rotation. It is based on the length of a tropical year in 1900 where 1 second is considered to be 1/31, 556,925.9747 of the tropical year in 1900. This gives 86400 seconds in a day. However, this mode of calculation is not accurate because the earth's rotation is not constant and has been slowing down at a rate which itself is increasing. To account for this slowing down, we would either have to increase the number of seconds in a day or vary the length of a second itself. The latter is clearly a horrific idea. What has been implemented in practice, as we will soon see, is a certain variation of the former, as we shall soon see. But before that we need to define a very precise, never changing, standard for what second means. 
TAI: TAI (or International Atomic Time) is a very precise never changing standard for what 1 second means. It is not based on earth's rotation but is defined as the amount of time it takes one Cesium-133 atom at ground state to oscillate precisely 9,192,631,770 times. 1 second in TAI time is also known as an SI second. This is a highly accurate measure but does not take into account the changing speed of earth's rotation. While this would not be a problem for people living in spaceships it is a definite problem for people living on earth. At an average UT1 would go 1 second behind atomic time every 1.5 years. Not a big problem initially, but as the years add on, after a very long time, we could run into the issue where 12 PM is midnight instead of noon. This is off course a long time into the future, but our notion of time based on solar patterns will certainly become skewed as the seconds add up. Another issue is that solar dials will no longer be accurate. 
UTC: Clearly we needed some mechanism which uses the precision of SI seconds but does not get out of synch with the solar time on earth. UTC or Coordinated Universal time was created for this purpose. A typical UTC day consists of 86,400 SI seconds, however, the difference between the time showed by UTC ad UT1 (based on earth's rotation) is never allowed to exceed 1 second. This is done by adding or subtracting 1 second from a UTC day (typically on 30th June or 31st December) whenever it's offset with UT1 exceeds 0.6 seconds.
Leap seconds: 
The seconds that are added or subtracted to coordinate UTC with solar time (UT1) are known as leap seconds. Even though there is provision to add as well as subtract time, leap seconds have never been subtracted from UTC as yet, since the earth's rotation is consistently getting slower.
In 1972, before the first leap second was added, UTC was 10 seconds behind atomic time. Since then 26 seconds have been added to UTC, with the most recent addition happening on June 30th 2015, 23:59:60 UTC.
Different computer systems deal with leap seconds in different ways. Some computer systems set their clock back by 1 second at the end of the day when the leap second has to be added. Other systems spread the difference over one or several hours such that a second actually becomes slightly longer within that period of adjustment. Here's how Google handles it:
The solution we came up with came to be known as the “leap smear.” We modified our internal NTP servers to gradually add a couple of milliseconds to every update, varying over a time window before the moment when the leap second actually happens. This meant that when it became time to add an extra second at midnight, our clocks had already taken this into account, by skewing the time over the course of the day. All of our servers were then able to continue as normal with the new year, blissfully unaware that a leap second had just occurred. We plan to use this “leap smear” technique again in the future, when new leap seconds are announced by the IERS.
However, not everyone likes the idea of leap seconds. Here's a Wired article on the problems leap seconds can cause and why we should do away with this entire concept of leap seconds.