Skip to content

Commit ab531cc

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

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,21 @@
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 `java.time.Instant` 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+
Instant.fromEpochMilliseconds(System.currentTimeMillis())
15+
}
16+
17+
/**
18+
* `false` for Android devices with API level < 24, where java.time is not available.
19+
*/
20+
private val javaTimeAvailable = try {
21+
java.time.Instant.now()
22+
true
23+
} catch (e: NoClassDefFoundError) {
24+
false
25+
}

0 commit comments

Comments
 (0)