Skip to content

SR-14108: Implement copy() in DateFormatter. #3008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Sources/Foundation/DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ keyPath: ReferenceWritableKeyPath<DateFormatter, T>) {
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:)")
Expand Down
26 changes: 26 additions & 0 deletions Tests/Foundation/Tests/TestDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}

Expand Down Expand Up @@ -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")
}
}