-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix DateFormatter TimeZone Setter #1704
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ class TestDateFormatter: XCTestCase { | |
("test_dateFormatString", test_dateFormatString), | ||
("test_setLocaleToNil", test_setLocaleToNil), | ||
("test_setTimeZoneToNil", test_setTimeZoneToNil), | ||
("test_setTimeZone", test_setTimeZone), | ||
("test_ExpectedTimeZone", test_ExpectedTimeZone), | ||
] | ||
} | ||
|
||
|
@@ -356,4 +358,54 @@ class TestDateFormatter: XCTestCase { | |
// Time zone should go back to the system one. | ||
XCTAssertEqual(f.timeZone, NSTimeZone.system) | ||
} | ||
|
||
func test_setTimeZone() { | ||
// Test two different time zones. Should ensure that if one | ||
// happens to be TimeZone.current, we still get a valid test. | ||
let newYork = TimeZone(identifier: "America/New_York")! | ||
let losAngeles = TimeZone(identifier: "America/Los_Angeles")! | ||
|
||
XCTAssertNotEqual(newYork, losAngeles) | ||
|
||
// Case 1: New York | ||
let f = DateFormatter() | ||
f.timeZone = newYork | ||
XCTAssertEqual(f.timeZone, newYork) | ||
|
||
// Case 2: Los Angeles | ||
f.timeZone = losAngeles | ||
XCTAssertEqual(f.timeZone, losAngeles) | ||
} | ||
|
||
func test_ExpectedTimeZone() { | ||
let gmt = TimeZone(abbreviation: DEFAULT_TIMEZONE) | ||
let newYork = TimeZone(identifier: "America/New_York")! | ||
let losAngeles = TimeZone(identifier: "America/Los_Angeles")! | ||
|
||
XCTAssertNotEqual(newYork, losAngeles) | ||
|
||
let now = Date() | ||
|
||
let f = DateFormatter() | ||
Kaiede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f.dateFormat = "z" | ||
|
||
// Case 1: TimeZone.current | ||
f.timeZone = TimeZone.current | ||
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation()) | ||
|
||
// Case 2: New York | ||
f.timeZone = newYork | ||
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation()) | ||
Kaiede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Case 3: Los Angeles | ||
f.timeZone = losAngeles | ||
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation()) | ||
|
||
guard gmt != TimeZone.current else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% happy with the way this test works, which pretty much is trying to make sure that we can use "TimeZone.current" for zones other than GMT and get what we expect when we then run it through DateFormatter. If this is too much, I can try to write a more targeted test, although I'm not entirely sure that would be any less fragile.
Kaiede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Inconclusive: This test checks to see if the formatter produces the same TZ as TimeZone.current") | ||
print("When it fails, TimeZone.current formats as GMT instead of normal.") | ||
print("Unfortunately, we can't use GMT as TimeZone.current for this test to be conclusive.") | ||
return | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.