Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions core/jvm/src/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ 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 = this.let {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use let? Just remove it and return the object directly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks. Fixed. 😄

object : Clock {
override fun now() = it.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())
}
}