Skip to content

Commit 38650e6

Browse files
committed
Use the same currentTime implementation for all Native targets
It seems like the only reason to use NSDate.date() is to obtain an object. The POSIX implementation is more accurate.
1 parent e25cd72 commit 38650e6

File tree

5 files changed

+28
-62
lines changed

5 files changed

+28
-62
lines changed

core/darwin/src/internal/TimeZoneNative.kt

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
package kotlinx.datetime.internal
88

99
import kotlinx.cinterop.*
10-
import kotlinx.datetime.Instant
11-
import kotlinx.datetime.toKotlinInstant
1210
import kotlinx.datetime.internal.*
1311
import platform.Foundation.*
1412

@@ -65,9 +63,5 @@ internal actual fun currentSystemDefaultZone(): Pair<String, TimeZoneRules?> {
6563
that's hard to avoid.
6664
*/
6765
NSTimeZone.resetSystemTimeZone()
68-
val zone = NSTimeZone.systemTimeZone
69-
val zoneId = zone.name
70-
return zoneId to null
66+
return NSTimeZone.systemTimeZone.name to null
7167
}
72-
73-
internal actual fun currentTime(): Instant = NSDate.date().toKotlinInstant()

core/linux/src/internal/TimeZoneNative.kt

-24
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,12 @@
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

6-
@file:OptIn(ExperimentalForeignApi::class)
7-
86
package kotlinx.datetime.internal
97

10-
import kotlinx.cinterop.*
11-
import kotlinx.datetime.Instant
12-
import platform.posix.*
13-
148
internal actual val systemTzdb: TimeZoneDatabase = TzdbOnFilesystem()
159

1610
internal actual fun currentSystemDefaultZone(): Pair<String, TimeZoneRules?> {
1711
val zoneId = pathToSystemDefault()?.second?.toString()
1812
?: throw IllegalStateException("Failed to get the system timezone")
1913
return zoneId to null
2014
}
21-
22-
@OptIn(UnsafeNumber::class)
23-
internal actual fun currentTime(): Instant = memScoped {
24-
val tm = alloc<timespec>()
25-
val error = clock_gettime(CLOCK_REALTIME, tm.ptr)
26-
if (error != 0) {
27-
val errorStr: String = strerror(errno)?.toKString() ?: "Unknown error"
28-
throw IllegalStateException("Could not obtain the current clock readings from the system: $errorStr")
29-
}
30-
val seconds: Long = tm.tv_sec.convert<Long>()
31-
val nanoseconds: Int = tm.tv_nsec.convert<Int>()
32-
try {
33-
require(nanoseconds in 0 until NANOS_PER_ONE)
34-
return Instant(seconds, nanoseconds)
35-
} catch (e: IllegalArgumentException) {
36-
throw IllegalStateException("The readings from the system clock are not representable as an Instant")
37-
}
38-
}

core/native/src/internal/Platform.kt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019-2024 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.internal
7+
8+
import kotlinx.cinterop.*
9+
import kotlinx.datetime.*
10+
import platform.posix.*
11+
12+
internal expect val systemTzdb: TimeZoneDatabase
13+
14+
internal expect fun currentSystemDefaultZone(): Pair<String, TimeZoneRules?>
15+
16+
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
17+
internal fun currentTime(): Instant = memScoped {
18+
val tm = alloc<timespec>()
19+
val error = clock_gettime(CLOCK_REALTIME.convert(), tm.ptr)
20+
check(error == 0) { "Error when reading the system clock: ${strerror(errno)?.toKString() ?: "Unknown error"}" }
21+
try {
22+
require(tm.tv_nsec in 0 until NANOS_PER_ONE)
23+
Instant(tm.tv_sec.convert(), tm.tv_nsec.convert())
24+
} catch (e: IllegalArgumentException) {
25+
throw IllegalStateException("The readings from the system clock (${tm.tv_sec} seconds, ${tm.tv_nsec} nanoseconds) are not representable as an Instant")
26+
}
27+
}

core/native/src/internal/TimeZoneNative.kt

-14
This file was deleted.

core/windows/src/internal/TimeZoneNative.kt

-17
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,10 @@
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

6-
@file:OptIn(ExperimentalForeignApi::class)
76
package kotlinx.datetime.internal
87

9-
import kotlinx.cinterop.*
10-
import kotlinx.datetime.*
11-
import platform.posix.*
12-
138
internal actual val systemTzdb: TimeZoneDatabase get() = tzdbInRegistry
149

1510
internal actual fun currentSystemDefaultZone(): Pair<String, TimeZoneRules?> = tzdbInRegistry.currentSystemDefault()
1611

17-
internal actual fun currentTime(): Instant = memScoped {
18-
val tm = alloc<timespec>()
19-
val error = clock_gettime(CLOCK_REALTIME, tm.ptr)
20-
check(error == 0) { "Error when reading the system clock: ${strerror(errno)}" }
21-
try {
22-
require(tm.tv_nsec in 0 until NANOS_PER_ONE)
23-
Instant(tm.tv_sec, tm.tv_nsec)
24-
} catch (e: IllegalArgumentException) {
25-
throw IllegalStateException("The readings from the system clock (${tm.tv_sec} seconds, ${tm.tv_nsec} nanoseconds) are not representable as an Instant")
26-
}
27-
}
28-
2912
private val tzdbInRegistry = TzdbInRegistry()

0 commit comments

Comments
 (0)