# Leaping Leap Seconds, Batman... 😔

**URL:** <https://forums.swift.org/t/leaping-leap-seconds-batman/48516>\
**Category:** Using Swift\
**Tags:** foundation, date\
**Created:** [May 17, 2021, 9:32pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516 "2021-05-17T21:32:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [May 17, 2021, 9:32pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/1 "2021-05-17T21:32:27Z")

</div>

When I create a `Date` value from this string `"2005-12-31T23:59:59Z"` and then check its `timeIntervalSinceReferenceDate` value I find `157766399`. The same process for `"2006-01-01T00:00:00Z"` yields `157766400`. If I create a `Date` value directly from the number `157766400` and then check it's description I find `"2006-01-01T00:00:00Z"`.

This would all be fine, except that as far as I've understood from what I've read there was a leap second inserted at the end of 2005, which would mean that exactly `157766400` seconds after the reference date (the beginning of 2001) the officially accepted UTC timestamp was either `"2005-12-31T23:59:60Z"` or `"2005-12-31T23:59:59Z"` repeated, but definitely not `"2006-01-01T00:00:00Z"`. It was not until `157766401` seconds after the reference date that the worldwide UTC timestamp was `"2006-01-01T00:00:00Z"`.

Does this mean that `Date.timeIntervalSinceReferenceDate` does not return the actual number of physically elapsed seconds since the reference date? Is the `Date` type incapable of referring to a leap second? `"2005-12-31T23:59:60Z"`, for example?

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [May 17, 2021, 10:27pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/2 "2021-05-17T22:27:43Z")

</div>

After some digging, I found [this documentation](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW8) which indicates that the Foundation date-related types and methods do not account for leap seconds. Not sure how the official story has changed since that was updated in 2013, but there's only been two additional leap seconds since then so I doubt it's changed much.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [May 17, 2021, 10:40pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/3 "2021-05-17T22:40:28Z")

</div>

Thanks! Nice to at least see "leap seconds" mentioned somewhere in the official documentation.

---

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [May 18, 2021, 12:38am UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/4 "2021-05-18T00:38:14Z")

</div>

An important follow-up to call out:

> Since Cocoa implements time according to the NTP standard, these methods ignore leap seconds in the calculation.

The implication here isn't clear, but Apple platforms _do_ adjust for leap seconds using NTP: when leap seconds occur, the _system_ clock will correctly update to leap forward or backward, and the effective meaning of existing timestamps shifts forward or backward by 1 second. Just to be clear that leap seconds aren't ignored on these systems; they are just not differentiated by the APIs when searching backward in time, nor predicted when searching forward in time.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 18, 2021, 4:43am UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/5 "2021-05-18T04:43:37Z")

</div>

> [@itaiferber](#):
>
> Apple platforms _do_ adjust for leap seconds using NTP: when leap seconds occur, the _system_ clock will correctly update to leap forward or backward

I think it's worth mentioning that some other systems, instead of _leaping_, may leap smear (or slew) and spread out that adjustment over a longer period (hrs - days).

> [@jeremyabannister](#):
>
> Does this mean that `Date.timeIntervalSinceReferenceDate` does not return the actual number of physically elapsed seconds since the reference date?

I don't know about that one in particular, but I believe the seconds in POSIX time are never the SI seconds. It is [implementation-defined](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16) even:

> The relationship between the actual time of day and the current value for seconds since the Epoch is unspecified.
> 
> How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined. As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

If `Date` uses POSIX time in under the hood, it might have similar "inaccuracy".

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [May 18, 2021, 2:14pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/6 "2021-05-18T14:14:30Z")

</div>

> [@Lantua](#):
>
> I think it's worth mentioning that some other systems, instead of _leaping_ , may leap smear (or slew) and spread out that adjustment over a longer period (hrs - days).

That's a feature of particular NTP servers (rather than clients), right? I.e., if you pointed iOS/macOS to a Google NTP server (which implements smearing), then you'd get the smear behavior?

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [May 18, 2021, 2:32pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/7 "2021-05-18T14:32:16Z")

</div>

For now then I'm conceptualizing it as such:

`Date.timeIntervalSinceReferenceDate` returns the number of Apple-defined POSIX seconds since the reference date, where the duration of an Apple-defined POSIX second is almost always exactly one SI second, with the exception of the handful of leap POSIX seconds which have a duration of exactly two SI seconds. This means, for example, that POSIX second `157766399.5` occurred one SI second after `157766399`. Good thing that all I needed was a clear and comprehensible grid of time. If I were creating some predictive physics engine that depends on knowing exactly how many SI seconds elapsed between two dates then I think that would require manually maintaining the log of leap seconds as they are issued.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 18, 2021, 3:41pm UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/8 "2021-05-18T15:41:45Z")

</div>

> [@Jumhyn](#):
>
> That's a feature of particular NTP servers (rather than clients), right?

Kind of, it's still technically possible that the clients would leap smear themselves when a leap second is expected. Though it's more common to add the `23:59:60`, double the duration of `23:59:59`, or even ignore it entirely and re-sync the clock after the fact.

> [@jeremyabannister](#):
>
> This means, for example, that POSIX second `157766399.5` occurred one SI second after `157766399` .

Hmm, from what I read above, it's probably more of a replay, where `157766399-157766400` is played twice. In which case, `157766399.5` is ambiguously either `0.5` or `1.5` SI seconds after `157766399`. It probably won't matter much if you're using a second granularity (`157766399 - 157766400` spans two SI seconds regardless).

Ain't clock synchronization fun? 😏

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [May 19, 2021, 9:26am UTC](https://forums.swift.org/t/leaping-leap-seconds-batman/48516/9 "2021-05-19T09:26:03Z")

</div>

> [@Lantua](#):
>
> , it's probably more of a replay,

I remember some twenty years ago, when I moved to Trondheim, Norway. City regulations forbid serving of alcohol after 02:30 at night. However, during the yearly daylight saving adjustments, the hour 02-03 is played twice. So, serving stopped from 02:30:00-02:59:59, and re-opened again when the interval 02:00:00 to 02:29:59 replayed.

I was stunned. But can now clearly visualise what @Lantua is talking about.
