From aa91d367e756d69e125b7fcea888692e01db4a95 Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Tue, 13 Jul 2021 16:29:43 +0900 Subject: [PATCH 1/2] Add tests for SR-14108. --- .../Foundation/Tests/TestDateFormatter.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Tests/Foundation/Tests/TestDateFormatter.swift b/Tests/Foundation/Tests/TestDateFormatter.swift index e8a97e035e..f5220f3988 100644 --- a/Tests/Foundation/Tests/TestDateFormatter.swift +++ b/Tests/Foundation/Tests/TestDateFormatter.swift @@ -29,6 +29,7 @@ class TestDateFormatter: XCTestCase { ("test_dateFrom", test_dateFrom), ("test_dateParseAndFormatWithJapaneseCalendar", test_dateParseAndFormatWithJapaneseCalendar), ("test_orderOfPropertySetters", test_orderOfPropertySetters), + ("test_copy_sr14108", test_copy_sr14108), ] } @@ -527,4 +528,29 @@ class TestDateFormatter: XCTestCase { } } } + + // Confirm that https://bugs.swift.org/browse/SR-14108 is fixed. + func test_copy_sr14108() throws { + let date = Date(timeIntervalSinceReferenceDate: 0) + + let original = DateFormatter() + original.timeZone = TimeZone(identifier: DEFAULT_TIMEZONE) + original.locale = Locale(identifier: DEFAULT_LOCALE) + original.dateFormat = "yyyy-MM-dd HH:mm:ss z" + XCTAssertEqual(original.string(from: date), "2001-01-01 00:00:00 GMT") + + let copied = try XCTUnwrap(original.copy() as? DateFormatter) + XCTAssertEqual(original.timeZone, copied.timeZone) + XCTAssertEqual(original.locale, copied.locale) + XCTAssertEqual(copied.string(from: date), "2001-01-01 00:00:00 GMT") + + copied.timeZone = TimeZone(abbreviation: "JST") + copied.locale = Locale(identifier: "ja_JP") + copied.dateFormat = "yyyy/MM/dd hh:mm:ssxxxxx" + + XCTAssertNotEqual(original.timeZone, copied.timeZone) + XCTAssertNotEqual(original.locale, copied.locale) + XCTAssertEqual(original.string(from: date), "2001-01-01 00:00:00 GMT") + XCTAssertEqual(copied.string(from: date), "2001/01/01 09:00:00+09:00") + } } From 8e0e90ffa504420a044fb5bbf12142e7fa4b6185 Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Wed, 14 Jul 2021 17:40:22 +0900 Subject: [PATCH 2/2] Implement `copy()` in `DateFormatter`. --- Sources/Foundation/DateFormatter.swift | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Sources/Foundation/DateFormatter.swift b/Sources/Foundation/DateFormatter.swift index c92f356ab0..7131242647 100644 --- a/Sources/Foundation/DateFormatter.swift +++ b/Sources/Foundation/DateFormatter.swift @@ -36,6 +36,51 @@ open class DateFormatter : Formatter { super.init(coder: coder) } + open override func copy(with zone: NSZone? = nil) -> Any { + let copied = DateFormatter() + + func __copy(_ keyPath: ReferenceWritableKeyPath) { + copied[keyPath: keyPath] = self[keyPath: keyPath] + } + + __copy(\.formattingContext) + __copy(\.dateStyle) + __copy(\.timeStyle) + __copy(\._locale) + __copy(\.generatesCalendarDates) + __copy(\._timeZone) + __copy(\._calendar) + __copy(\.isLenient) + __copy(\._twoDigitStartDate) + __copy(\._eraSymbols) + __copy(\._monthSymbols) + __copy(\._shortMonthSymbols) + __copy(\._weekdaySymbols) + __copy(\._shortWeekdaySymbols) + __copy(\._amSymbol) + __copy(\._pmSymbol) + __copy(\._longEraSymbols) + __copy(\._veryShortMonthSymbols) + __copy(\._standaloneMonthSymbols) + __copy(\._shortStandaloneMonthSymbols) + __copy(\._veryShortStandaloneMonthSymbols) + __copy(\._veryShortWeekdaySymbols) + __copy(\._standaloneWeekdaySymbols) + __copy(\._shortStandaloneWeekdaySymbols) + __copy(\._veryShortStandaloneWeekdaySymbols) + __copy(\._quarterSymbols) + __copy(\._shortQuarterSymbols) + __copy(\._standaloneQuarterSymbols) + __copy(\._shortStandaloneQuarterSymbols) + __copy(\._gregorianStartDate) + __copy(\.doesRelativeDateFormatting) + + // The last is `_dateFormat` because setting `dateStyle` and `timeStyle` make it `nil`. + __copy(\._dateFormat) + + return copied + } + open var formattingContext: Context = .unknown // default is NSFormattingContextUnknown @available(*, unavailable, renamed: "date(from:)")