Getting dateComponents out of date produces wrong results
| Originator: | kirill.pahnev | ||
| Number: | rdar://30776971 | Date Originated: | 01-Mar-2017 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Version 8.2.1 (8C1002) |
| Classification: | Reproducible: | Always |
Summary:
When getting dateComponents from past date and the current date crosses February 28th, the dateComponents are calculated incorrectly.
Steps to Reproduce:
1. Set the computer's date to February 28th 2017
2. Open the attached project and run the test
3. Change computer's date to March 1st 2017
4. Run the test again
The test is essentially this code snippet and can be run in the Playgrounds as well:
import Foundation
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "y-MM-dd HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let february28th = dateFormatter.date(from: "2017-02-28 15:18:00 +0000")!
let march3rd = dateFormatter.date(from: "2017-03-03 15:18:00 +0000")!
let testDateStrings =
[
"2017-01-31 16:51:00 +0000",
"2017-01-30 16:51:00 +0000",
"2017-01-29 16:51:00 +0000",
"2017-01-28 16:51:00 +0000",
]
let testDates = testDateStrings.map { dateFormatter.date(from: $0)! }.sorted {$0.compare($1) == .orderedDescending}
testDates.forEach { (date) in
var dateComponents = Calendar.current.dateComponents([.second, .minute, .hour, .day, .month, .year], from: date, to: february28th)
dateComponents.timeZone = TimeZone(secondsFromGMT: 0)
print(date)
print(dateComponents)
}
print("----------------")
print("----------------")
print("----------------")
testDates.forEach { (date) in
var dateComponents = Calendar.current.dateComponents([.second, .minute, .hour, .day, .month, .year], from: date, to: march3rd)
dateComponents.timeZone = TimeZone(secondsFromGMT: 0)
print(date)
print(dateComponents)
}
Expected Results:
Expect both tests to pass
Actual Results:
The second test fails without changing anything but the "current date"
Version:
Version 8.2.1 (8C1002)
Notes:
I encountered this bug when our NSFetchedResultController started crashing on February 28th, 2017. We were using it to divide the objects into sections based on their timestamps, which was working fine until that date (and by changing the date of the device also after).
The problem was that we were sorting the objects by the timestamp and then creating more readable format for the sectionTittle, using dateComponents. But when the sectionTitles were being created the app crashed with an error message "The fetched object at index 2885 has an out of order section name '4 weeks ago. Objects must be sorted by section name’", and after investigation the dateComponent was indeed out of the sorted order.
The issue might not be exactly the same, but this test was the simplest way to show that there is a serious issue with dateComponents around February and March.
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!