Skip to content

Commit 5df2250

Browse files
authored
Merge pull request #879 from alblue/calendar-components
Calendar components from:to: date fix
2 parents 2e34ea2 + 31c107a commit 5df2250

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Foundation/NSCalendar.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
603603
}
604604

605605
return vector.withUnsafeMutableBufferPointer { (vecBuffer: inout UnsafeMutableBufferPointer<UnsafeMutablePointer<Int32>>) in
606-
_CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, Int32(vector.count))
607-
return false
606+
return _CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, Int32(vector.count))
608607
}
609608
}
610609
if res {

TestFoundation/TestNSCalendar.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,29 @@ class TestNSCalendar: XCTestCase {
6363

6464
var calendar = Calendar(identifier: .gregorian)
6565
calendar.timeZone = TimeZone(identifier: "UTC")!
66-
let components = calendar.dateComponents([.year, .month, .day], from: date)
66+
let components = calendar.dateComponents([.year, .month, .day], from: date)
6767

6868
XCTAssertEqual(components.year, 2015)
6969
XCTAssertEqual(components.month, 12)
7070
XCTAssertEqual(components.day, 5)
71+
72+
// Test for problem reported by Malcolm Barclay via swift-corelibs-dev
73+
// https://lists.swift.org/pipermail/swift-corelibs-dev/Week-of-Mon-20161128/001031.html
74+
let fromDate = Date()
75+
let interval = 200
76+
let toDate = Date(timeInterval: TimeInterval(interval), since: fromDate)
77+
let fromToComponents = calendar.dateComponents([.second], from: fromDate, to: toDate)
78+
XCTAssertEqual(fromToComponents.second, interval);
79+
80+
// Issue with 32-bit CF calendar vector on Linux
81+
// Crashes on macOS 10.12.2/Foundation 1349.25
82+
// (Possibly related) rdar://24384757
83+
/*
84+
let interval2 = Int(INT32_MAX) + 1
85+
let toDate2 = Date(timeInterval: TimeInterval(interval2), since: fromDate)
86+
let fromToComponents2 = calendar.dateComponents([.second], from: fromDate, to: toDate2)
87+
XCTAssertEqual(fromToComponents2.second, interval2);
88+
*/
7189
}
7290

7391
func test_gettingDatesOnISO8601Calendar() {

0 commit comments

Comments
 (0)