Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/jvm/src/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
10 changes: 10 additions & 0 deletions core/jvm/test/ConvertersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}