diff --git a/Foundation/DateComponents.swift b/Foundation/DateComponents.swift index aa167234cd..9cde6aba10 100644 --- a/Foundation/DateComponents.swift +++ b/Foundation/DateComponents.swift @@ -189,7 +189,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab /// Set to true if these components represent a leap month. public var isLeapMonth: Bool? { - get { return _handle.map { $0.isLeapMonth } } + get { return _handle.map { $0.leapMonthSet ? $0.isLeapMonth : nil } } set { _applyMutation { // Technically, the underlying class does not support setting isLeapMonth to nil, but it could - so we leave the API consistent. diff --git a/Foundation/NSCalendar.swift b/Foundation/NSCalendar.swift index d6d0d4feb5..624b103408 100644 --- a/Foundation/NSCalendar.swift +++ b/Foundation/NSCalendar.swift @@ -461,7 +461,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { _convert(comps.weekday, type: "E", vector: &vector, compDesc: &compDesc) _convert(comps.weekdayOrdinal, type: "F", vector: &vector, compDesc: &compDesc) _convert(comps.month, type: "M", vector: &vector, compDesc: &compDesc) - _convert(comps.isLeapMonth, type: "L", vector: &vector, compDesc: &compDesc) + _convert(comps.isLeapMonth, type: "l", vector: &vector, compDesc: &compDesc) _convert(comps.day, type: "d", vector: &vector, compDesc: &compDesc) _convert(comps.hour, type: "H", vector: &vector, compDesc: &compDesc) _convert(comps.minute, type: "m", vector: &vector, compDesc: &compDesc) @@ -579,7 +579,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { open func date(byAdding comps: DateComponents, to date: Date, options opts: Options = []) -> Date? { var (vector, compDesc) = _convert(comps) - var at: CFAbsoluteTime = 0.0 + var at: CFAbsoluteTime = date.timeIntervalSinceReferenceDate let res: Bool = withUnsafeMutablePointer(to: &at) { t in return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer) in diff --git a/TestFoundation/TestNSCalendar.swift b/TestFoundation/TestNSCalendar.swift index 71e2ec4ad0..3b11faa6f9 100644 --- a/TestFoundation/TestNSCalendar.swift +++ b/TestFoundation/TestNSCalendar.swift @@ -24,6 +24,7 @@ class TestNSCalendar: XCTestCase { ("test_gettingDatesOnHebrewCalendar", test_gettingDatesOnHebrewCalendar ), ("test_gettingDatesOnChineseCalendar", test_gettingDatesOnChineseCalendar), ("test_copy",test_copy), + ("test_addingDates", test_addingDates) // Disabled because this fails on linux https://bugs.swift.org/browse/SR-320 // ("test_currentCalendarRRstability", test_currentCalendarRRstability), ] @@ -89,6 +90,18 @@ class TestNSCalendar: XCTestCase { XCTAssertEqual(copy.firstWeekday, 2) XCTAssertEqual(copy.minimumDaysInFirstWeek, 2) } + + func test_addingDates() { + let calendar = Calendar(identifier: .gregorian) + let thisDay = calendar.date(from: DateComponents(year: 2016, month: 10, day: 4))! + let diffComponents = DateComponents(day: 1) + let dayAfter = calendar.date(byAdding: diffComponents, to: thisDay) + + let dayAfterComponents = calendar.dateComponents([.year, .month, .day], from: dayAfter!) + XCTAssertEqual(dayAfterComponents.year, 2016) + XCTAssertEqual(dayAfterComponents.month, 10) + XCTAssertEqual(dayAfterComponents.day, 5) + } } class TestNSDateComponents: XCTestCase {