From 487e15888e31b3412ac1c7a1ab4efaa73dae7e07 Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Fri, 16 Jul 2021 14:31:57 +0900 Subject: [PATCH 1/2] Add tests for SR-14933. --- .../Tests/TestISO8601DateFormatter.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tests/Foundation/Tests/TestISO8601DateFormatter.swift b/Tests/Foundation/Tests/TestISO8601DateFormatter.swift index 6054794b7c..e1fdd2d614 100644 --- a/Tests/Foundation/Tests/TestISO8601DateFormatter.swift +++ b/Tests/Foundation/Tests/TestISO8601DateFormatter.swift @@ -326,6 +326,28 @@ class TestISO8601DateFormatter: XCTestCase { try fixture.assertLoadedValuesMatch(areEqual(_:_:)) } } + + func test_copy() throws { + let original = ISO8601DateFormatter() + original.timeZone = try XCTUnwrap(TimeZone(identifier: "GMT")) + original.formatOptions = [ + .withInternetDateTime, + .withDashSeparatorInDate, + .withColonSeparatorInTime, + .withColonSeparatorInTimeZone, + ] + + let copied = try XCTUnwrap(original.copy() as? ISO8601DateFormatter) + XCTAssertEqual(copied.timeZone, original.timeZone) + XCTAssertEqual(copied.formatOptions, original.formatOptions) + + copied.timeZone = try XCTUnwrap(TimeZone(identifier: "JST")) + copied.formatOptions.insert(.withFractionalSeconds) + XCTAssertNotEqual(copied.timeZone, original.timeZone) + XCTAssertNotEqual(copied.formatOptions, original.formatOptions) + XCTAssertFalse(original.formatOptions.contains(.withFractionalSeconds)) + XCTAssertTrue(copied.formatOptions.contains(.withFractionalSeconds)) + } static var allTests : [(String, (TestISO8601DateFormatter) -> () throws -> Void)] { @@ -335,6 +357,7 @@ class TestISO8601DateFormatter: XCTestCase { ("test_stringFromDateClass", test_stringFromDateClass), ("test_codingRoundtrip", test_codingRoundtrip), ("test_loadingFixtures", test_loadingFixtures), + ("test_copy", test_copy), ] } } From da36592118540335c3745c497a14fd1f7f0cbb7b Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Fri, 16 Jul 2021 14:53:29 +0900 Subject: [PATCH 2/2] Implement `copy()` in `ISO8601DateFormatter`. --- Sources/Foundation/ISO8601DateFormatter.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/Foundation/ISO8601DateFormatter.swift b/Sources/Foundation/ISO8601DateFormatter.swift index 0764ca89fd..54de843b52 100644 --- a/Sources/Foundation/ISO8601DateFormatter.swift +++ b/Sources/Foundation/ISO8601DateFormatter.swift @@ -112,6 +112,13 @@ open class ISO8601DateFormatter : Formatter, NSSecureCoding { aCoder.encode(timeZone._nsObject, forKey: "NS.timeZone") } } + + open override func copy(with zone: NSZone? = nil) -> Any { + let copied = ISO8601DateFormatter() + copied.timeZone = timeZone + copied.formatOptions = formatOptions + return copied + } public static var supportsSecureCoding: Bool { return true }