NSCalendar Calculations Don’t Account for Leap Second

Originator:SlaunchaMan
Number:rdar://31757928 Date Originated:21-Apr-2017 11:21 AM
Status:Open Resolved:
Product:iOS + SDK Product Version:iOS 10.3
Classification:Other Bug Reproducible:Always
 
Area:
Foundation


Summary:
If you use NSCalendar to measure the time between two dates, it doesn’t account for leap seconds. Consider the following code:

let year = 2016

let beginningOfAnyYearComponents = DateComponents(
    month: 1,
    day: 1,
    hour: 0,
    minute: 0,
    second: 0,
    nanosecond: 0)

var beginningOfThisYearComponents = beginningOfAnyYearComponents
beginningOfThisYearComponents.year = year

let beginningOfThisYear = calendar.date(from: beginningOfThisYearComponents)!

let beginningOfNextYear = calendar
    .nextDate(after: beginningOfThisYear,
              matching: beginningOfAnyYearComponents,
              matchingPolicy: .strict)!

let seconds = beginningOfNextYear.timeIntervalSince(beginningOfThisYear)



This code returns 31,622,400 seconds, which does not include a leap second.


Steps to Reproduce:
1. Enter the above code into an Xcode Playground and run it.


Expected Results:
The value of `seconds` is 31,622,401.


Observed Results:
The value of `seconds` is 31,622,400.


Version:
iOS 10.3


Notes:
According to Wikipedia, there was a leap second on December 31, 2016 at 11:59:60: https://en.wikipedia.org/wiki/Leap_second


Configuration:
Xcode 8.3.2

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!