Calendar.nextDate does not respect provided time zone
| Originator: | sam | ||
| Number: | rdar://29788075 | Date Originated: | 2016/12/22 |
| Status: | Open | Resolved: | |
| Product: | iOS + SDK | Product Version: | iOS 8.4, iOS 9.3, iOS 10.2 |
| Classification: | Other Bug | Reproducible: | Always |
Area:
Foundation
Summary:
When you try to compute the next date with provided time zone information, it will not respect it and instead use the system's time zone to compute the next date that matches provided date components.
Steps to Reproduce:
1. Change Mac timezone to Europe/London
2. Open attached playground and look at printed logs. You should see that the next date computed for Edmonton will instead show date computed for the London time zone.
Expected Results:
When computing the next date that should match a set of date components that includes a specified time zone, that time zone should be preserved in the calculations.
Actual Results:
If computing the next date through the Calendar.nextDate API, it will use the system's time zone
----
Playground:
import Foundation
let calendar = Calendar(identifier: .gregorian)
// *************************************************************
// Pre-condition: Local machine is set to Europe/London timezone
// *************************************************************
print("-- America/Edmonton --")
let edmontonComponents = DateComponents(calendar: calendar, timeZone: TimeZone(identifier: "America/Edmonton"), year: 2016, month: 12, day: 25, hour: 5, minute: 0, second: 0)
let edmontonDate = calendar.date(from: edmontonComponents)
print("2016/12/25, 5AM in Edmonton: \(edmontonDate!.timeIntervalSince1970)") // 1482667200.0
print(edmontonComponents.timeZone!.identifier) // America/Edmonton
let desiredEdmontonComponents = DateComponents(timeZone: TimeZone(identifier: "America/Edmonton"), hour: 5, minute: 0, second: 0)
let nextEdmonton5AM = calendar.nextDate(after: edmontonDate!, matching: desiredEdmontonComponents, matchingPolicy: .strict)
let extractedEdmontonComponents = calendar.dateComponents([.hour, .minute, .timeZone], from: nextEdmonton5AM!)
print("2016/12/26, 5AM in Edmonton: \(nextEdmonton5AM!.timeIntervalSince1970)") // 1482728400.0 (incorrect!)
print(extractedEdmontonComponents.hour!) // 5 (incorrect!)
print(extractedEdmontonComponents.timeZone!.identifier) // Europe/London
print("")
print("-- Europe/London --")
let londonComponents = DateComponents(calendar: calendar, timeZone: TimeZone(identifier: "Europe/London"), year: 2016, month: 12, day: 25, hour: 5, minute: 0, second: 0)
let londonDate = calendar.date(from: londonComponents)
print("2016/12/25, 5AM in London: \(londonDate!.timeIntervalSince1970)") // 1482642000.0
print(londonComponents.timeZone!.identifier) // Europe/London
let desiredLondonComponents = DateComponents(timeZone: TimeZone(identifier: "Europe/London"), hour: 5, minute: 0, second: 0)
let nextLondon5AM = calendar.nextDate(after: londonDate!, matching: desiredLondonComponents, matchingPolicy: .strict)
let extractedLondonComponents = calendar.dateComponents([.hour, .minute, .timeZone], from: nextLondon5AM!)
print("2016/12/26, 5AM in London: \(nextLondon5AM!.timeIntervalSince1970)") // 1482728400.0 (same as line 21)
print(extractedLondonComponents.hour!) // 5
print(extractedLondonComponents.timeZone!.identifier) // Europe/London
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!