Skip to content

Commit 2d714e6

Browse files
authored
Merge pull request #3010 from YOCKOW/swift-5.5/SR-14108
[5.5] SR-14108: Implement `copy()` in `DateFormatter`.
2 parents a1991ab + 173cb18 commit 2d714e6

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Sources/Foundation/DateFormatter.swift

+45
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,51 @@ open class DateFormatter : Formatter {
3636
super.init(coder: coder)
3737
}
3838

39+
open override func copy(with zone: NSZone? = nil) -> Any {
40+
let copied = DateFormatter()
41+
42+
func __copy<T>(_ keyPath: ReferenceWritableKeyPath<DateFormatter, T>) {
43+
copied[keyPath: keyPath] = self[keyPath: keyPath]
44+
}
45+
46+
__copy(\.formattingContext)
47+
__copy(\.dateStyle)
48+
__copy(\.timeStyle)
49+
__copy(\._locale)
50+
__copy(\.generatesCalendarDates)
51+
__copy(\._timeZone)
52+
__copy(\._calendar)
53+
__copy(\.isLenient)
54+
__copy(\._twoDigitStartDate)
55+
__copy(\._eraSymbols)
56+
__copy(\._monthSymbols)
57+
__copy(\._shortMonthSymbols)
58+
__copy(\._weekdaySymbols)
59+
__copy(\._shortWeekdaySymbols)
60+
__copy(\._amSymbol)
61+
__copy(\._pmSymbol)
62+
__copy(\._longEraSymbols)
63+
__copy(\._veryShortMonthSymbols)
64+
__copy(\._standaloneMonthSymbols)
65+
__copy(\._shortStandaloneMonthSymbols)
66+
__copy(\._veryShortStandaloneMonthSymbols)
67+
__copy(\._veryShortWeekdaySymbols)
68+
__copy(\._standaloneWeekdaySymbols)
69+
__copy(\._shortStandaloneWeekdaySymbols)
70+
__copy(\._veryShortStandaloneWeekdaySymbols)
71+
__copy(\._quarterSymbols)
72+
__copy(\._shortQuarterSymbols)
73+
__copy(\._standaloneQuarterSymbols)
74+
__copy(\._shortStandaloneQuarterSymbols)
75+
__copy(\._gregorianStartDate)
76+
__copy(\.doesRelativeDateFormatting)
77+
78+
// The last is `_dateFormat` because setting `dateStyle` and `timeStyle` make it `nil`.
79+
__copy(\._dateFormat)
80+
81+
return copied
82+
}
83+
3984
open var formattingContext: Context = .unknown // default is NSFormattingContextUnknown
4085

4186
@available(*, unavailable, renamed: "date(from:)")

Tests/Foundation/Tests/TestDateFormatter.swift

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TestDateFormatter: XCTestCase {
2929
("test_dateFrom", test_dateFrom),
3030
("test_dateParseAndFormatWithJapaneseCalendar", test_dateParseAndFormatWithJapaneseCalendar),
3131
("test_orderOfPropertySetters", test_orderOfPropertySetters),
32+
("test_copy_sr14108", test_copy_sr14108),
3233
]
3334
}
3435

@@ -527,4 +528,29 @@ class TestDateFormatter: XCTestCase {
527528
}
528529
}
529530
}
531+
532+
// Confirm that https://bugs.swift.org/browse/SR-14108 is fixed.
533+
func test_copy_sr14108() throws {
534+
let date = Date(timeIntervalSinceReferenceDate: 0)
535+
536+
let original = DateFormatter()
537+
original.timeZone = TimeZone(identifier: DEFAULT_TIMEZONE)
538+
original.locale = Locale(identifier: DEFAULT_LOCALE)
539+
original.dateFormat = "yyyy-MM-dd HH:mm:ss z"
540+
XCTAssertEqual(original.string(from: date), "2001-01-01 00:00:00 GMT")
541+
542+
let copied = try XCTUnwrap(original.copy() as? DateFormatter)
543+
XCTAssertEqual(original.timeZone, copied.timeZone)
544+
XCTAssertEqual(original.locale, copied.locale)
545+
XCTAssertEqual(copied.string(from: date), "2001-01-01 00:00:00 GMT")
546+
547+
copied.timeZone = TimeZone(abbreviation: "JST")
548+
copied.locale = Locale(identifier: "ja_JP")
549+
copied.dateFormat = "yyyy/MM/dd hh:mm:ssxxxxx"
550+
551+
XCTAssertNotEqual(original.timeZone, copied.timeZone)
552+
XCTAssertNotEqual(original.locale, copied.locale)
553+
XCTAssertEqual(original.string(from: date), "2001-01-01 00:00:00 GMT")
554+
XCTAssertEqual(copied.string(from: date), "2001/01/01 09:00:00+09:00")
555+
}
530556
}

0 commit comments

Comments
 (0)