Skip to content

Commit 2105e9c

Browse files
mamabusiparkera
authored andcommitted
Implementation of NSDateComponents.copy(with: zone) (#564)
1 parent ff652d6 commit 2105e9c

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Foundation/NSCalendar.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,27 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
14281428
}
14291429

14301430
open func copy(with zone: NSZone? = nil) -> Any {
1431-
NSUnimplemented()
1431+
let newObj = NSDateComponents()
1432+
newObj.calendar = calendar
1433+
newObj.timeZone = timeZone
1434+
newObj.era = era
1435+
newObj.year = year
1436+
newObj.month = month
1437+
newObj.day = day
1438+
newObj.hour = hour
1439+
newObj.minute = minute
1440+
newObj.second = second
1441+
newObj.nanosecond = nanosecond
1442+
newObj.weekOfYear = weekOfYear
1443+
newObj.weekOfMonth = weekOfMonth
1444+
newObj.yearForWeekOfYear = yearForWeekOfYear
1445+
newObj.weekday = weekday
1446+
newObj.weekdayOrdinal = weekdayOrdinal
1447+
newObj.quarter = quarter
1448+
if leapMonthSet {
1449+
newObj.isLeapMonth = isLeapMonth
1450+
}
1451+
return newObj
14321452
}
14331453

14341454
/*@NSCopying*/ open var calendar: Calendar? {

TestFoundation/TestNSCalendar.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,34 @@ class TestNSCalendar: XCTestCase {
9090
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
9191
}
9292
}
93+
94+
class TestNSDateComponents: XCTestCase {
95+
96+
static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
97+
return [
98+
("test_copyNSDateComponents", test_copyNSDateComponents),
99+
]
100+
}
101+
102+
func test_copyNSDateComponents() {
103+
let components = NSDateComponents()
104+
components.year = 1987
105+
components.month = 3
106+
components.day = 17
107+
components.hour = 14
108+
components.minute = 20
109+
components.second = 0
110+
let copy = components.copy(with: nil) as! NSDateComponents
111+
XCTAssertTrue(components.isEqual(copy))
112+
XCTAssertTrue(components == copy)
113+
XCTAssertFalse(components === copy)
114+
XCTAssertEqual(copy.year, 1987)
115+
XCTAssertEqual(copy.month, 3)
116+
XCTAssertEqual(copy.day, 17)
117+
XCTAssertEqual(copy.isLeapMonth, false)
118+
//Mutate NSDateComponents and verify that it does not reflect in the copy
119+
components.hour = 12
120+
XCTAssertEqual(components.hour, 12)
121+
XCTAssertEqual(copy.hour, 14)
122+
}
123+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ XCTMain([
3030
testCase(TestNSCompoundPredicate.allTests),
3131
testCase(TestNSData.allTests),
3232
testCase(TestNSDate.allTests),
33+
testCase(TestNSDateComponents.allTests),
3334
testCase(TestNSDateFormatter.allTests),
3435
testCase(TestNSDictionary.allTests),
3536
testCase(TestNSFileManager.allTests),

0 commit comments

Comments
 (0)