Skip to content

Commit 1bc603e

Browse files
authored
Merge pull request #3009 from YOCKOW/SR-14933
SR-14933: Implement `copy()` in `ISO8601DateFormatter`.
2 parents bb8e7d2 + f34a2f3 commit 1bc603e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Sources/Foundation/ISO8601DateFormatter.swift

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ open class ISO8601DateFormatter : Formatter, NSSecureCoding {
112112
aCoder.encode(timeZone._nsObject, forKey: "NS.timeZone")
113113
}
114114
}
115+
116+
open override func copy(with zone: NSZone? = nil) -> Any {
117+
let copied = ISO8601DateFormatter()
118+
copied.timeZone = timeZone
119+
copied.formatOptions = formatOptions
120+
return copied
121+
}
115122

116123
public static var supportsSecureCoding: Bool { return true }
117124

Tests/Foundation/Tests/TestISO8601DateFormatter.swift

+23
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,28 @@ class TestISO8601DateFormatter: XCTestCase {
326326
try fixture.assertLoadedValuesMatch(areEqual(_:_:))
327327
}
328328
}
329+
330+
func test_copy() throws {
331+
let original = ISO8601DateFormatter()
332+
original.timeZone = try XCTUnwrap(TimeZone(identifier: "GMT"))
333+
original.formatOptions = [
334+
.withInternetDateTime,
335+
.withDashSeparatorInDate,
336+
.withColonSeparatorInTime,
337+
.withColonSeparatorInTimeZone,
338+
]
339+
340+
let copied = try XCTUnwrap(original.copy() as? ISO8601DateFormatter)
341+
XCTAssertEqual(copied.timeZone, original.timeZone)
342+
XCTAssertEqual(copied.formatOptions, original.formatOptions)
343+
344+
copied.timeZone = try XCTUnwrap(TimeZone(identifier: "JST"))
345+
copied.formatOptions.insert(.withFractionalSeconds)
346+
XCTAssertNotEqual(copied.timeZone, original.timeZone)
347+
XCTAssertNotEqual(copied.formatOptions, original.formatOptions)
348+
XCTAssertFalse(original.formatOptions.contains(.withFractionalSeconds))
349+
XCTAssertTrue(copied.formatOptions.contains(.withFractionalSeconds))
350+
}
329351

330352
static var allTests : [(String, (TestISO8601DateFormatter) -> () throws -> Void)] {
331353

@@ -335,6 +357,7 @@ class TestISO8601DateFormatter: XCTestCase {
335357
("test_stringFromDateClass", test_stringFromDateClass),
336358
("test_codingRoundtrip", test_codingRoundtrip),
337359
("test_loadingFixtures", test_loadingFixtures),
360+
("test_copy", test_copy),
338361
]
339362
}
340363
}

0 commit comments

Comments
 (0)