Skip to content

Commit 6b24e5e

Browse files
committed
Use the correct tzdb path on the iOS Simulator devices
1 parent 83ef4e9 commit 6b24e5e

File tree

11 files changed

+74
-54
lines changed

11 files changed

+74
-54
lines changed

core/build.gradle.kts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,28 @@ kotlin {
5555
target("androidNativeX64")
5656
*/
5757
common("darwin") {
58-
// Tier 1
59-
target("macosX64")
60-
target("macosArm64")
61-
target("iosSimulatorArm64")
62-
target("iosX64")
63-
// Tier 2
64-
target("watchosSimulatorArm64")
65-
target("watchosX64")
66-
target("watchosArm32")
67-
target("watchosArm64")
68-
target("tvosSimulatorArm64")
69-
target("tvosX64")
70-
target("tvosArm64")
71-
target("iosArm64")
72-
// Tier 3
73-
target("watchosDeviceArm64")
58+
common("darwinDevices") {
59+
// Tier 1
60+
target("macosX64")
61+
target("macosArm64")
62+
// Tier 2
63+
target("watchosX64")
64+
target("watchosArm32")
65+
target("watchosArm64")
66+
target("tvosX64")
67+
target("tvosArm64")
68+
target("iosArm64")
69+
// Tier 3
70+
target("watchosDeviceArm64")
71+
}
72+
common("darwinSimulator") {
73+
// Tier 1
74+
target("iosSimulatorArm64")
75+
target("iosX64")
76+
// Tier 2
77+
target("watchosSimulatorArm64")
78+
target("tvosSimulatorArm64")
79+
}
7480
}
7581
}
7682
// Tier 3
@@ -155,12 +161,7 @@ kotlin {
155161
// do nothing special
156162
}
157163
konanTarget.family.isAppleFamily -> {
158-
compilations["main"].cinterops {
159-
create("declarations") {
160-
defFile("$projectDir/darwin/cinterop/definitions.def")
161-
headers("$projectDir/darwin/cinterop/definitions.h")
162-
}
163-
}
164+
// do nothing special
164165
}
165166
else -> {
166167
throw IllegalArgumentException("Unknown native target ${this@withType}")

core/darwin/cinterop/definitions.def

Lines changed: 0 additions & 1 deletion
This file was deleted.

core/darwin/cinterop/definitions.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

core/darwin/src/Converters.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public fun NSDate.toKotlinInstant(): Instant {
4444
* to [TimeZone.offset]) and the offset is not given in even minutes but also includes seconds, this method throws
4545
* [IllegalArgumentException] to denote that lossy conversion would happen, as Darwin internally rounds the offsets to the
4646
* nearest minute.
47+
*
48+
* If the time zone is unknown to the Foundation framework, [IllegalArgumentException] will be thrown.
4749
*/
4850
public fun TimeZone.toNSTimeZone(): NSTimeZone = if (this is FixedOffsetTimeZone) {
4951
require (offset.totalSeconds % 60 == 0) {

core/darwin/src/TimeZoneNative.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
package kotlinx.datetime
99

10-
import kotlinx.datetime.internal.*
1110
import platform.Foundation.*
1211

13-
// iOS simulator needs a different path, hence the check. See https://github.com/HowardHinnant/date/pull/577
14-
internal actual val systemTzdb: TimezoneDatabase = TzdbOnFilesystem(Path.fromString(
15-
if (kotlinxDatetimeRunningInSimulator) "/usr/share/zoneinfo" else "/var/db/timezone/zoneinfo"))
16-
1712
internal actual fun currentSystemDefaultZone(): RegionTimeZone {
1813
/* The framework has its own cache of the system timezone. Calls to
1914
[NSTimeZone systemTimeZone] do not reflect changes to the system timezone

core/darwin/test/ConvertersTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class ConvertersTest {
5151
if (timeZone is FixedOffsetTimeZone) {
5252
continue
5353
}
54-
// TODO: investigate failure
5554
val nsTimeZone = try { timeZone.toNSTimeZone() } catch (e: IllegalArgumentException) { continue }
5655
assertEquals(normalizedId, nsTimeZone.name)
5756
assertEquals(timeZone, nsTimeZone.toKotlinTimeZone())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2019-2023 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
7+
8+
import kotlinx.datetime.internal.*
9+
10+
internal actual fun getTzdbPath(): Path = Path.fromString("/var/db/timezone/zoneinfo")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2019-2023 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
7+
8+
import kotlinx.datetime.internal.*
9+
10+
internal actual fun getTzdbPath(): Path = Path.fromString("/usr/share/zoneinfo.default")

core/linux/src/TimeZoneNative.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@ package kotlinx.datetime
77

88
import kotlinx.datetime.internal.*
99

10-
internal actual val systemTzdb: TimezoneDatabase = TzdbOnFilesystem(Path.fromString("/usr/share/zoneinfo"))
11-
1210
internal actual fun currentSystemDefaultZone(): RegionTimeZone {
1311
val zoneId = pathToSystemDefault()?.second?.toString()
1412
?: throw IllegalStateException("Failed to get the system timezone")
1513
return RegionTimeZone(systemTzdb.rulesForId(zoneId), zoneId)
1614
}
15+
16+
internal actual fun getTzdbPath(): Path {
17+
val defaultPath = Path.fromString("/usr/share/zoneinfo")
18+
return defaultPath.check()?.let { defaultPath }
19+
?: pathToSystemDefault()?.first ?: throw IllegalStateException("Could not find the path to the timezone database")
20+
}
21+
22+
private fun pathToSystemDefault(): Pair<Path, Path>? {
23+
val info = Path(true, listOf("etc", "localtime")).readLink() ?: return null
24+
val i = info.components.indexOf("zoneinfo")
25+
if (!info.isAbsolute || i == -1 || i == info.components.size - 1) return null
26+
return Pair(
27+
Path(true, info.components.subList(0, i + 1)),
28+
Path(false, info.components.subList(i + 1, info.components.size))
29+
)
30+
}

core/nix/src/TimeZoneNative.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright 2019-2023 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
7+
8+
import kotlinx.datetime.internal.*
9+
10+
internal expect fun getTzdbPath(): Path
11+
12+
internal actual val systemTzdb: TimezoneDatabase = TzdbOnFilesystem(getTzdbPath())

core/nix/src/internal/TzdbOnFilesystem.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.datetime.internal
77

88
import kotlinx.datetime.*
99

10-
internal class TzdbOnFilesystem(defaultTzdbPath: Path): TimezoneDatabase {
10+
internal class TzdbOnFilesystem(val tzdbPath: Path): TimezoneDatabase {
1111

1212
override fun rulesForId(id: String): TimeZoneRules =
1313
readTzFile(tzdbPath.resolve(Path.fromString(id)).readBytes()).toTimeZoneRules()
@@ -16,19 +16,6 @@ internal class TzdbOnFilesystem(defaultTzdbPath: Path): TimezoneDatabase {
1616
tzdbPath.traverseDirectory(exclude = tzdbUnneededFiles) { add(it.toString()) }
1717
}
1818

19-
private val tzdbPath = defaultTzdbPath.check()?.let { defaultTzdbPath }
20-
?: pathToSystemDefault()?.first ?: throw IllegalStateException("Could not find the path to the timezone database")
21-
22-
}
23-
24-
internal fun pathToSystemDefault(): Pair<Path, Path>? {
25-
val info = Path(true, listOf("etc", "localtime")).readLink() ?: return null
26-
val i = info.components.indexOf("zoneinfo")
27-
if (!info.isAbsolute || i == -1 || i == info.components.size - 1) return null
28-
return Pair(
29-
Path(true, info.components.subList(0, i + 1)),
30-
Path(false, info.components.subList(i + 1, info.components.size))
31-
)
3219
}
3320

3421
private val tzdbUnneededFiles = setOf(

0 commit comments

Comments
 (0)