Skip to content

Commit 3a0ea83

Browse files
committed
Add tests and benchmark
1 parent 1fce2b9 commit 3a0ea83

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Benchmarks/Benchmarks/Internationalization/BenchmarkTimeZone.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ func timeZoneBenchmarks() {
7171
blackHole(t)
7272
}
7373
}
74+
75+
guard let gmtPlus8 = TimeZone(identifier: "GMT+8") else {
76+
fatalError("unexpected failure when creating time zone")
77+
}
78+
79+
let locale = Locale(identifier: "jp_JP")
80+
81+
Benchmark("GMTOffsetTimeZoneAPI", configuration: .init(scalingFactor: .mega)) { benchmark in
82+
for d in testDates {
83+
let secondsFromGMT = gmtPlus8.secondsFromGMT(for: d)
84+
blackHole(secondsFromGMT)
85+
86+
let abbreviation = gmtPlus8.abbreviation(for: d)
87+
blackHole(abbreviation)
88+
89+
let isDST = gmtPlus8.isDaylightSavingTime(for: d)
90+
blackHole(isDST)
91+
92+
let nextDST = gmtPlus8.nextDaylightSavingTimeTransition(after: d)
93+
blackHole(nextDST)
94+
}
95+
}
96+
97+
Benchmark("GMTOffsetTimeZone-localizedNames", configuration: .init(scalingFactor: .mega)) { benchmark in
98+
for style in [TimeZone.NameStyle.generic, .standard, .shortGeneric, .shortStandard, .daylightSaving, .shortDaylightSaving] {
99+
let localizedName = gmtPlus8.localizedName(for: style, locale: locale)
100+
blackHole(localizedName)
101+
}
102+
}
103+
74104
}
75105

76106

Tests/FoundationInternationalizationTests/TimeZoneTests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,63 @@ private struct TimeZoneTests {
138138
try testAbbreviation("UTC+0900", 32400, "GMT+0900")
139139
}
140140

141+
@Test func timeZoneGMTOffset() throws {
142+
func testName(_ name: String, _ expectedOffset: Int, sourceLocation: SourceLocation = #_sourceLocation) throws {
143+
let tz = try #require(TimeZone(identifier: name))
144+
let secondsFromGMT = tz.secondsFromGMT()
145+
#expect(secondsFromGMT == expectedOffset)
146+
#expect(tz.isDaylightSavingTime() == false)
147+
#expect(tz.nextDaylightSavingTimeTransition == nil)
148+
}
149+
150+
try testName("GMT+8", 8*3600)
151+
try testName("GMT+08", 8*3600)
152+
try testName("GMT+0800", 8*3600)
153+
try testName("GMT+08:00", 8*3600)
154+
try testName("GMT+8:00", 8*3600)
155+
try testName("UTC+9", 9*3600)
156+
try testName("UTC+09", 9*3600)
157+
try testName("UTC+0900", 9*3600)
158+
try testName("UTC+09:00", 9*3600)
159+
try testName("UTC+9:00", 9*3600)
160+
}
161+
162+
@Test(arguments: ["en_001", "en_US", "ja_JP"])
163+
func timeZoneGMTOffset_localizedNames(localeIdentifier: String) throws {
164+
let locale = Locale(identifier: localeIdentifier)
165+
func testNames(
166+
_ names: [String],
167+
_ expectedStandardName: String,
168+
_ expectedShortStandardName: String,
169+
_ expectedDaylightSavingName: String,
170+
_ expectedShortDaylightSavingName: String,
171+
_ expectedGenericName: String,
172+
_ expectedShortGenericName: String,
173+
sourceLocation: SourceLocation = #_sourceLocation) throws {
174+
for name in names {
175+
let tz = try #require(TimeZone(identifier: name))
176+
let standardName = tz.localizedName(for: .standard, locale: locale)
177+
let shortStandardName = tz.localizedName(for: .shortStandard, locale: locale)
178+
let daylightSavingName = tz.localizedName(for: .daylightSaving, locale: locale)
179+
let shortDaylightSavingName = tz.localizedName(for: .shortDaylightSaving, locale: locale)
180+
let generic = tz.localizedName(for: .generic, locale: locale)
181+
let shortGeneric = tz.localizedName(for: .shortGeneric, locale: locale)
182+
183+
#expect(expectedStandardName == standardName)
184+
#expect(expectedShortStandardName == shortStandardName)
185+
#expect(expectedDaylightSavingName == daylightSavingName)
186+
#expect(expectedShortDaylightSavingName == shortDaylightSavingName)
187+
#expect(expectedGenericName == generic)
188+
#expect(expectedShortGenericName == shortGeneric)
189+
}
190+
}
191+
192+
try testNames(["GMT+8", "GMT+08", "GMT+0800", "GMT+08:00", "GMT+8:00"],
193+
"GMT+08:00", "GMT+8", "GMT+08:00", "GMT+8", "GMT+08:00", "GMT+8")
194+
try testNames(["UTC+9", "UTC+09", "UTC+0900", "UTC+09:00", "UTC+9:00"],
195+
"GMT+09:00", "GMT+9", "GMT+09:00", "GMT+9", "GMT+09:00", "GMT+9")
196+
}
197+
141198
@Test func secondsFromGMT_RemoteDates() {
142199
let date = Date(timeIntervalSinceReferenceDate: -5001243627) // "1842-07-09T05:39:33+0000"
143200
let europeRome = TimeZone(identifier: "Europe/Rome")!

0 commit comments

Comments
 (0)