Skip to content

Commit 9708815

Browse files
#485: Add tests and fallback logic for retrieving available time zone IDs on Darwin
1 parent 3e68613 commit 9708815

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

core/darwin/src/internal/TimeZoneNative.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ internal actual fun timeZoneById(zoneId: String): TimeZone =
1515
RegionTimeZone(tzdb.getOrThrow().rulesForId(zoneId), zoneId)
1616

1717
internal actual fun getAvailableZoneIds(): Set<String> =
18-
tzdb.getOrThrow().availableTimeZoneIds()
18+
runCatching { tzdb.getOrThrow().availableTimeZoneIds() }
19+
.getOrElse { getAvailableZoneIdsFoundation() }
20+
21+
internal fun getAvailableZoneIdsFoundation(): Set<String> =
22+
NSTimeZone.knownTimeZoneNames.mapTo(mutableSetOf()) { it.toString() }
1923

2024
private val tzdb = runCatching { TzdbOnFilesystem(Path.fromString(defaultTzdbPath())) }
2125

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2019-2025 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.internal.getAvailableZoneIds
9+
import kotlinx.datetime.internal.getAvailableZoneIdsFoundation
10+
import kotlin.test.Test
11+
import kotlin.test.assertTrue
12+
13+
class TimeZoneNativeTest {
14+
15+
@Test
16+
fun getAvailableZoneIdsReturnsValidTimezoneSet() {
17+
assertReturnsNonEmptySetOfTimezoneStrings(getAvailableZoneIds())
18+
}
19+
20+
@Test
21+
fun getAvailableZoneIdsFoundationReturnsValidTimezoneSet() {
22+
assertReturnsNonEmptySetOfTimezoneStrings(getAvailableZoneIdsFoundation())
23+
}
24+
25+
@Test
26+
fun getAvailableZoneIdsContainsExpectedTimezoneIDs() {
27+
assertAvailableZoneIdsContainsExpectedTimezoneIDs(getAvailableZoneIds())
28+
}
29+
30+
@Test
31+
fun getAvailableZoneIdsFoundationContainsExpectedTimezoneIDs() {
32+
assertAvailableZoneIdsContainsExpectedTimezoneIDs(getAvailableZoneIdsFoundation())
33+
}
34+
35+
private fun assertReturnsNonEmptySetOfTimezoneStrings(zoneIds: Set<String>) {
36+
assertTrue(zoneIds.isNotEmpty(), "Zone IDs should not be empty")
37+
assertTrue(zoneIds.all { it.isNotBlank() }, "All zone IDs should be non-blank")
38+
assertTrue("UTC" in zoneIds || "GMT" in zoneIds, "Should contain UTC or GMT")
39+
assertTrue(zoneIds.any { it.contains("America") }, "Should contain America timezones")
40+
assertTrue(zoneIds.any { it.contains("Europe") }, "Should contain Europe timezones")
41+
}
42+
43+
private fun assertAvailableZoneIdsContainsExpectedTimezoneIDs(zoneIds: Set<String>) {
44+
val expectedZones = listOf("GMT", "America/New_York", "Europe/London", "Asia/Tokyo", "Australia/Sydney")
45+
assertTrue(expectedZones.all { it in zoneIds }, "Should contain all common timezone")
46+
}
47+
}

0 commit comments

Comments
 (0)