Leap seconds and Unix time

Unix time has no room for a leap second. When UTC inserts one, the Unix clock has to repeat a second, or stretch the seconds around it.

What a leap second is

Atomic clocks keep steadier time than the Earth's rotation, which is irregular and slowly losing speed. UTC follows atomic time but is kept within 0.9 seconds of solar time by occasionally adding a second at the end of 30 June or 31 December. On those days the last minute has 61 seconds, and the clock reads 23:59:60.

There have been 27 leap seconds since the scheme began in 1972, all of them additions. The most recent was at the end of 31 December 2016. They are announced only about six months ahead, because the Earth's rotation cannot be predicted further.

Why Unix time cannot represent one

POSIX defines Unix time by arithmetic on the UTC date in which each day counts as exactly 86,400 seconds. Under that formula 23:59:60 on a leap-second day and 00:00:00 on the next day produce the same number:

UTC                     Unix time
2016-12-31 23:59:58     1483228798
2016-12-31 23:59:59     1483228799
2016-12-31 23:59:60     1483228800   the leap second
2017-01-01 00:00:00     1483228800   the same value again
2017-01-01 00:00:01     1483228801

So Unix time is not a count of the seconds that have really elapsed since 1970. It is behind such a count by the number of leap seconds, and it is ambiguous for one second each time. In exchange, a timestamp converts to a date with simple division and without a table of leap seconds that would need updating.

What clocks actually do

  • Step. The traditional behaviour on Linux with NTP: the kernel repeats the last second of the day. Time read with sub-second precision appears to jump backwards by one second, and an event during the leap second gets the same timestamp as one a second later.
  • Smear. Google, Amazon Web Services and other large operators instead run their clocks very slightly slow for many hours around the leap second, so that it is absorbed gradually and the clock never repeats or goes backwards. Google's public NTP service spreads it linearly over the 24 hours from noon to noon UTC. During a smear, clocks differ from true UTC by up to half a second.

Do not mix time sources that smear with ones that step: they disagree by up to a second during the window.

What this means for code

  • Subtracting two timestamps that span a leap second gives an answer one second short of the real elapsed time. For almost every application that does not matter.
  • Never measure durations with the wall clock. It can be stepped by a leap second, by NTP or by an administrator. Use a monotonic clock: performance.now(), time.monotonic(), System.nanoTime(), Instant in Rust, CLOCK_MONOTONIC.
  • Parsing 23:59:60. A valid UTC time during a leap second, but many parsers reject it. JavaScript's Date does. Others clamp it to 23:59:59 or roll it over to the next minute.
  • Time that must count every second, as in astronomy and satellite navigation, uses TAI or GPS time, which have no leap seconds. TAI is currently 37 seconds ahead of UTC, and GPS time 18 seconds ahead.

The end of leap seconds

Leap seconds have caused real outages, most visibly in 2012 when a Linux kernel bug triggered by the leap second left servers at several large sites spinning at full CPU. In November 2022 the General Conference on Weights and Measures resolved to stop keeping UTC within a second of solar time, by 2035 at the latest. After that no leap seconds are expected for at least a century, and the difference from the Earth's rotation will be allowed to grow.

Until then one more could still be announced. Because the Earth has recently been rotating slightly faster, the first negative leap second, in which 23:59:59 would be skipped, has also been discussed. None has ever been applied.

Questions

Does Unix time include leap seconds?

No. Every day counts as 86,400 seconds, so the 27 leap seconds inserted since 1972 are not counted. During a leap second the Unix time of the previous or following second is repeated.

How many leap seconds have there been?

27, between 1972 and the end of 2016. All were added; none has been removed.

Can I calculate elapsed time by subtracting Unix timestamps?

Yes, for ordinary purposes. The result ignores any leap seconds in between, so it can be short by a second or two across years. For precise intervals inside one program use a monotonic clock instead.