Skip to content

Commit 81611f1

Browse files
committed
Support Android devices with API level < 24
1 parent d3238e9 commit 81611f1

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

fake-kotlinx-time/jvm/src/Platform.kt

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,22 @@
55

66
package kotlinx.time
77

8-
internal actual fun currentTime(): Instant = java.time.Instant.now().toKotlinInstant()
8+
internal actual fun currentTime(): Instant = if (javaTimeAvailable) {
9+
java.time.Instant.now().toKotlinInstant()
10+
} else {
11+
/* After experimenting in Android Studio, it seems like on Android with API < 24, only millisecond precision
12+
is available in `Instant.now()` with core library desugaring enabled. Because of that, `currentTimeMillis`
13+
is good enough + suggesting that our users enable core library desugaring isn't going to bring any benefits,
14+
so the KDoc for [Clock] does not mention any of this. */
15+
Instant.fromEpochMilliseconds(System.currentTimeMillis())
16+
}
17+
18+
/**
19+
* `false` for Android devices with API level < 24, where java.time is not available.
20+
*/
21+
private val javaTimeAvailable = try {
22+
java.time.Instant.now()
23+
true
24+
} catch (e: NoClassDefFoundError) {
25+
false
26+
}

0 commit comments

Comments
 (0)