Skip to content

Commit 582b366

Browse files
mbvreddyparkera
authored andcommitted
implementation for NSCalendar.copy() (#409)
1 parent 11df1ec commit 582b366

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Foundation/NSCalendar.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ public class Calendar: NSObject, NSCopying, NSSecureCoding {
167167
}
168168

169169
public func copy(with zone: NSZone? = nil) -> AnyObject {
170-
NSUnimplemented()
170+
let copy = Calendar(calendarIdentifier: calendarIdentifier)!
171+
copy.locale = locale
172+
copy.timeZone = timeZone
173+
copy.firstWeekday = firstWeekday
174+
copy.minimumDaysInFirstWeek = minimumDaysInFirstWeek
175+
copy._startDate = _startDate
176+
return copy
171177
}
172178

173179

TestFoundation/TestNSCalendar.swift

Lines changed: 16 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_initializingWithInvalidIdentifier", test_initializingWithInvalidIdentifier),
2626
("test_gettingDatesOnChineseCalendar", test_gettingDatesOnChineseCalendar),
27+
("test_copy",test_copy),
2728
// Disabled because this fails on linux https://bugs.swift.org/browse/SR-320
2829
// ("test_currentCalendarRRstability", test_currentCalendarRRstability),
2930
]
@@ -88,4 +89,19 @@ class TestNSCalendar: XCTestCase {
8889

8990
XCTAssertEqual(AMSymbols.count, 10, "Accessing current calendar should work over multiple callouts")
9091
}
92+
93+
func test_copy() {
94+
let calendar = Calendar.currentCalendar()
95+
96+
//Mutate below fields and check if change is being reflected in copy.
97+
calendar.firstWeekday = 2
98+
calendar.minimumDaysInFirstWeek = 2
99+
100+
let copy = calendar.copy() as! Calendar
101+
XCTAssertTrue(copy.isEqual(calendar))
102+
103+
//verify firstWeekday and minimumDaysInFirstWeek of 'copy'.
104+
XCTAssertEqual(copy.firstWeekday, 2)
105+
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
106+
}
91107
}

0 commit comments

Comments
 (0)