Skip to content

SR-10619: Order in which DateFormatter settings are set up matters on Linux #2219

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 1 commit into from
May 7, 2019
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
10 changes: 2 additions & 8 deletions Foundation/DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class DateFormatter : Formatter {
let dateStyle = CFDateFormatterStyle(self.dateStyle.rawValue)
let timeStyle = CFDateFormatterStyle(self.timeStyle.rawValue)
#endif

let obj = CFDateFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, dateStyle, timeStyle)!
_setFormatterAttributes(obj)
if let dateFormat = _dateFormat {
Expand Down Expand Up @@ -144,7 +144,7 @@ open class DateFormatter : Formatter {
open var dateFormat: String! {
get {
guard let format = _dateFormat else {
return __cfObject.map { CFDateFormatterGetFormat($0)._swiftObject } ?? ""
return CFDateFormatterGetFormat(_cfObject)._swiftObject
}
return format
}
Expand All @@ -157,18 +157,12 @@ open class DateFormatter : Formatter {
willSet {
_dateFormat = nil
}
didSet {
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
}
}

open var timeStyle: Style = .none {
willSet {
_dateFormat = nil
}
didSet {
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
}
}

/*@NSCopying*/ internal var _locale: Locale? { willSet { _reset() } }
Expand Down
61 changes: 61 additions & 0 deletions TestFoundation/TestDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestDateFormatter: XCTestCase {
("test_expectedTimeZone", test_expectedTimeZone),
("test_dateFrom", test_dateFrom),
("test_dateParseAndFormatWithJapaneseCalendar", test_dateParseAndFormatWithJapaneseCalendar),
("test_orderOfPropertySetters", test_orderOfPropertySetters),
]
}

Expand Down Expand Up @@ -443,4 +444,64 @@ class TestDateFormatter: XCTestCase {
let dateString = formatter.string(from: Date(timeIntervalSince1970: 1556633400)) // April 30, 2019, 11:10 PM (JST)
XCTAssertEqual(dateString, "平成31年4月30日 23:10")
}

func test_orderOfPropertySetters() throws {

// This produces a .count factorial number of arrays
func combinations<T>(of a: [T]) -> [[T]] {
precondition(!a.isEmpty)
if a.count == 1 { return [a] }
if a.count == 2 { return [ [a[0], a[1]], [ a[1], a[0]] ] }

var result: [[T]] = []

for idx in a.startIndex..<a.endIndex {
let x = a[idx]
var b: [T] = a
b.remove(at: idx)

for var c in combinations(of: b) {
c.append(x)
result.append(c)
}
}
return result
}

let formatter = DateFormatter()
formatter.timeZone = TimeZone(identifier: "CET")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let date = try formatter.date(from: "2019-05-05T12:52:10").unwrapped()

let applySettings: [(String, (DateFormatter) -> Void)] =
[(".timeZone", {
$0.timeZone = TimeZone(identifier: "Europe/Oslo")
}),
(".calendar", {
$0.calendar = Calendar(identifier: .gregorian)
}),
(".locale", {
$0.locale = Locale(identifier: "nb")
}),
(".dateStyle", {
$0.dateStyle = .medium
}),
(".timeStyle", {
$0.timeStyle = .medium
})
]

// Test all of the combinations of setting the properties produces the same output
let expected = "5. mai 2019, 12:52:10"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be like other tests in that we do not hardcode an answer, so we avoid ICU changes from causing breakage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this against Darwin Foundation and it worked find so I don't think this string is ICU version dependant. The PR actually fixes all of the current tests that are disabled due to not working correctly versus Darwin and will be enabled in a followup PR.

for settings in combinations(of: applySettings) {
let f = DateFormatter()
settings.forEach { $0.1(f) }
XCTAssertEqual(f.dateFormat, "d. MMM y, HH:mm:ss")
let formattedString = f.string(from: date)
if formattedString != expected {
let applied = settings.map { $0.0 }.joined(separator: ",")
XCTFail("\(formattedString) != \(expected) using settings applied in order \(applied)")
}
}
}
}