Why do Swift's Date time intervals ignore leap seconds?

139 Views Asked by At

30 June 2012 had a leap second. As such it lasted 86401 seconds. However, in the following Swift code, timeInterval is 86400. How come?

let formatter = ISO8601DateFormatter()
let date = formatter.date(from: "2012-06-30T00:00:00Z")!
let dayAfter = formatter.date(from: "2012-07-01T00:00:00Z")!
let timeInterval = date.distance(to: dayAfter)

(ps: in what context did I write this code? It was just about having fun with code. I was trying to write confusing code results, but I was disappointed by this attempt at trying to be smart)

1

There are 1 best solutions below

6
Itai Ferber On BEST ANSWER

The basis for Apple date and time APIs and system implementations is the ICU library, and the underpinnings for most of the results you see depends on ICU behavior. Notably, ICU doesn't support or consider the existence of leap seconds:

For historical reasons, the reference point is Greenwich, England. Local time in Greenwich is referred to as Greenwich Mean Time, or GMT. (This is similar, but not precisely identical, to Universal Coordinated Time, or UTC. We use the two terms interchangeably in ICU since ICU does not concern itself with either leap seconds or historical behavior.)

As such, Apple platforms also don't represent historical (or future) leap seconds in any way in APIs; systems effectively deal with leap seconds via NTP, and simply set their clocks appropriately, but querying for leap seconds like this won't yield anything.