Skip to content

Commit 2219bd1

Browse files
committed
Cleanup SystemTimeZone tests
Cleans up the DateFormatter test, improves the information about how it doesn’t catch issues when the system time zone is already GMT, and adds a more focused test for the SystemTimeZone name issue that can catch it when GMT is the system time zone.
1 parent 827e842 commit 2219bd1

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

TestFoundation/TestDateFormatter.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestDateFormatter: XCTestCase {
2525
("test_setLocaleToNil", test_setLocaleToNil),
2626
("test_setTimeZoneToNil", test_setTimeZoneToNil),
2727
("test_setTimeZone", test_setTimeZone),
28-
("test_ExpectedTimeZone", test_ExpectedTimeZone),
28+
("test_expectedTimeZone", test_expectedTimeZone),
2929
]
3030
}
3131

@@ -377,7 +377,7 @@ class TestDateFormatter: XCTestCase {
377377
XCTAssertEqual(f.timeZone, losAngeles)
378378
}
379379

380-
func test_ExpectedTimeZone() {
380+
func test_expectedTimeZone() {
381381
let gmt = TimeZone(abbreviation: DEFAULT_TIMEZONE)
382382
let newYork = TimeZone(identifier: "America/New_York")!
383383
let losAngeles = TimeZone(identifier: "America/Los_Angeles")!
@@ -388,24 +388,24 @@ class TestDateFormatter: XCTestCase {
388388

389389
let f = DateFormatter()
390390
f.dateFormat = "z"
391+
f.locale = Locale(identifier: "en_US_POSIX")
391392

392393
// Case 1: TimeZone.current
394+
// This case can catch some issues that cause TimeZone.current to be
395+
// treated like GMT, but it doesn't work if TimeZone.current is GMT.
396+
// If you do find an issue like this caused by this first case,
397+
// it would benefit from a more specific test that fails when
398+
// TimeZone.current is GMT as well.
399+
// (ex. TestTimeZone.test_systemTimeZoneName)
393400
f.timeZone = TimeZone.current
394-
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation())
401+
XCTAssertEqual(f.string(from: now), TimeZone.current.abbreviation())
395402

396403
// Case 2: New York
397404
f.timeZone = newYork
398-
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation())
405+
XCTAssertEqual(f.string(from: now), newYork.abbreviation())
399406

400407
// Case 3: Los Angeles
401408
f.timeZone = losAngeles
402-
XCTAssertEqual(f.string(from: now), f.timeZone.abbreviation())
403-
404-
guard gmt != TimeZone.current else {
405-
print("Inconclusive: This test checks to see if the formatter produces the same TZ as TimeZone.current")
406-
print("When it fails, TimeZone.current formats as GMT instead of normal.")
407-
print("Unfortunately, we can't use GMT as TimeZone.current for this test to be conclusive.")
408-
return
409-
}
409+
XCTAssertEqual(f.string(from: now), losAngeles.abbreviation())
410410
}
411411
}

TestFoundation/TestTimeZone.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
11+
1012
class TestTimeZone: XCTestCase {
1113

1214
static var allTests: [(String, (TestTimeZone) -> () throws -> Void)] {
@@ -29,6 +31,7 @@ class TestTimeZone: XCTestCase {
2931

3032
("test_customMirror", test_tz_customMirror),
3133
("test_knownTimeZones", test_knownTimeZones),
34+
("test_systemTimeZoneName", test_systemTimeZoneName),
3235
]
3336
}
3437

@@ -198,4 +201,17 @@ class TestTimeZone: XCTestCase {
198201
XCTAssertNotNil(TimeZone(identifier: tz), "Cant instantiate valid timeZone: \(tz)")
199202
}
200203
}
204+
205+
func test_systemTimeZoneName() {
206+
// Ensure that the system time zone creates names the same way as creating them with an identifier.
207+
// If it isn't the same, bugs in DateFormat can result, but in this specific case, the bad length
208+
// is only visible to CoreFoundation APIs, and the Swift versions hide it, making it hard to detect.
209+
let timeZone = CFTimeZoneCopySystem()
210+
let timeZoneName = CFTimeZoneGetName(timeZone)
211+
212+
let createdTimeZone = TimeZone(identifier: TimeZone.current.identifier)!
213+
214+
XCTAssertEqual(CFStringGetLength(timeZoneName), TimeZone.current.identifier.count)
215+
XCTAssertEqual(CFStringGetLength(timeZoneName), createdTimeZone.identifier.count)
216+
}
201217
}

0 commit comments

Comments
 (0)