diff --git a/core/jvm/src/Converters.kt b/core/jvm/src/Converters.kt index 6db8339d3..4a33f08cf 100644 --- a/core/jvm/src/Converters.kt +++ b/core/jvm/src/Converters.kt @@ -93,3 +93,9 @@ public fun UtcOffset.toJavaZoneOffset(): java.time.ZoneOffset = this.zoneOffset */ public fun java.time.ZoneOffset.toKotlinUtcOffset(): UtcOffset = UtcOffset(this) +/** + * Converts this [java.time.Clock][java.time.Clock] to a [kotlinx.datetime.Clock][Clock]. + */ +public fun java.time.Clock.toKotlinClock(): Clock = object : Clock { + override fun now() = instant().toKotlinInstant() +} diff --git a/core/jvm/test/ConvertersTest.kt b/core/jvm/test/ConvertersTest.kt index dccd86f43..4c71cca10 100644 --- a/core/jvm/test/ConvertersTest.kt +++ b/core/jvm/test/ConvertersTest.kt @@ -187,4 +187,14 @@ class ConvertersTest { test("+08") test("-103030") } + + @Test + fun clock() { + val jtInstant = java.time.Instant.parse("2023-02-07T12:46:52.13Z") + val jtClock = java.time.Clock.fixed(jtInstant, ZoneId.of("UTC")) + val ktClock = jtClock.toKotlinClock() + + assertEquals(jtClock.instant().epochSecond, ktClock.now().epochSeconds) + assertEquals(jtClock.instant().toEpochMilli(), ktClock.now().toEpochMilliseconds()) + } }