matatu blog

Brian writes about computing

Leap Seconds

Dec 31, 2018

Is there a leap second in January, 2019? (spoiler: no)

Tonight we count down from 10 to 0 at midnight and bring in the New Year.

But will it be 2019 when we reach 0? What if there is a leap second?

Quick -- let's check the list of leap seconds provided by the IETF.

No -- it looks good, January 1, 2017 was the last one, and that list is good until June 28, 2019.

Two years ago, things were different.

At "midnight" (UTC), it became 2017.

~ $ raku -e 'say DateTime.new("2017-01-01T00:00:00Z").year'
2017

But actually, TWO seconds had passed since 11:59:59 the night (and year) before!

~ $ raku -e 'say DateTime.new("2017-01-01T00:00:00Z") - DateTime.new("2016-12-31T23:59:59Z")'
2

At the instant that it became 2017, there was a leap second. The value returned by now would have indicated that.

Tonight will be fine though:

~ $ raku -e 'say DateTime.new("2019-01-01T00:00:00Z") - DateTime.new("2018-12-31T23:59:59Z")'
1

If we want to be more explicit, we can turn a DateTime into an Instant and back again by using posix and from-posix -- the latter takes a boolean value as a second parameter to indicate whether or not leap seconds should be taken into account.

my $date = DateTime.new("2017-01-01T00:00:00Z");
my $instant = Instant.from-posix: $date.posix, True;
my $instant2 = Instant.from-posix: $date.posix;
say $instant.DateTime.year;
say $instant2.DateTime.year;
2016
2017

I wonder how many languages have native support for leap seconds?

It looks like not many. Which is too bad. If leap seconds catch you off guard, weird things can happen.

Conclusions

  • Leap seconds happen once in a while. When they do, two seconds elapse between two datetimes which appear to differ by only one second.
  • Programming languages should support leap seconds.
  • There is no leap second in January, 2019.
  • Happy New Year!