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 } 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), ] } }