Skip to content

Commit 1681a38

Browse files
author
Daphne
authored
Merge pull request #668 from UberJason/master
Fixes for Date Manipulations
2 parents da2fdd9 + 87063ed commit 1681a38

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Foundation/DateComponents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab
189189

190190
/// Set to true if these components represent a leap month.
191191
public var isLeapMonth: Bool? {
192-
get { return _handle.map { $0.isLeapMonth } }
192+
get { return _handle.map { $0.leapMonthSet ? $0.isLeapMonth : nil } }
193193
set {
194194
_applyMutation {
195195
// Technically, the underlying class does not support setting isLeapMonth to nil, but it could - so we leave the API consistent.

Foundation/NSCalendar.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
461461
_convert(comps.weekday, type: "E", vector: &vector, compDesc: &compDesc)
462462
_convert(comps.weekdayOrdinal, type: "F", vector: &vector, compDesc: &compDesc)
463463
_convert(comps.month, type: "M", vector: &vector, compDesc: &compDesc)
464-
_convert(comps.isLeapMonth, type: "L", vector: &vector, compDesc: &compDesc)
464+
_convert(comps.isLeapMonth, type: "l", vector: &vector, compDesc: &compDesc)
465465
_convert(comps.day, type: "d", vector: &vector, compDesc: &compDesc)
466466
_convert(comps.hour, type: "H", vector: &vector, compDesc: &compDesc)
467467
_convert(comps.minute, type: "m", vector: &vector, compDesc: &compDesc)
@@ -579,7 +579,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
579579

580580
open func date(byAdding comps: DateComponents, to date: Date, options opts: Options = []) -> Date? {
581581
var (vector, compDesc) = _convert(comps)
582-
var at: CFAbsoluteTime = 0.0
582+
var at: CFAbsoluteTime = date.timeIntervalSinceReferenceDate
583583

584584
let res: Bool = withUnsafeMutablePointer(to: &at) { t in
585585
return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer<Int32>) in

TestFoundation/TestNSCalendar.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TestNSCalendar: XCTestCase {
2424
("test_gettingDatesOnHebrewCalendar", test_gettingDatesOnHebrewCalendar ),
2525
("test_gettingDatesOnChineseCalendar", test_gettingDatesOnChineseCalendar),
2626
("test_copy",test_copy),
27+
("test_addingDates", test_addingDates)
2728
// Disabled because this fails on linux https://bugs.swift.org/browse/SR-320
2829
// ("test_currentCalendarRRstability", test_currentCalendarRRstability),
2930
]
@@ -89,6 +90,18 @@ class TestNSCalendar: XCTestCase {
8990
XCTAssertEqual(copy.firstWeekday, 2)
9091
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
9192
}
93+
94+
func test_addingDates() {
95+
let calendar = Calendar(identifier: .gregorian)
96+
let thisDay = calendar.date(from: DateComponents(year: 2016, month: 10, day: 4))!
97+
let diffComponents = DateComponents(day: 1)
98+
let dayAfter = calendar.date(byAdding: diffComponents, to: thisDay)
99+
100+
let dayAfterComponents = calendar.dateComponents([.year, .month, .day], from: dayAfter!)
101+
XCTAssertEqual(dayAfterComponents.year, 2016)
102+
XCTAssertEqual(dayAfterComponents.month, 10)
103+
XCTAssertEqual(dayAfterComponents.day, 5)
104+
}
92105
}
93106

94107
class TestNSDateComponents: XCTestCase {

0 commit comments

Comments
 (0)