From 370bc5d1d8b3411844f64625f19c15847fda20cc Mon Sep 17 00:00:00 2001 From: Renee Vandervelde Date: Sat, 4 Jan 2025 08:58:18 -0600 Subject: [PATCH] Add zoned datetime conversion from instants --- CHANGELOG.md | 11 +++++++++++ datetime/api/datetime.api | 1 + .../com/inkapplications/datetime/ZonedDateTime.kt | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2adb9bf..a37687e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ Change Log ========== + +1.9.0 +----- + +### Added + + - DateTime + - Added `Instant.toZonedDateTime` extension to convert an `Instant` to a + `ZonedDateTime` in a specific timezone without needing to do multiple + conversions. + 1.8.1 ----- diff --git a/datetime/api/datetime.api b/datetime/api/datetime.api index 3607a21..b75c9ea 100644 --- a/datetime/api/datetime.api +++ b/datetime/api/datetime.api @@ -101,6 +101,7 @@ public final class com/inkapplications/datetime/ZonedDateTime : java/lang/Compar public final class com/inkapplications/datetime/ZonedDateTimeKt { public static final fun atZone (Lkotlinx/datetime/LocalDateTime;Lkotlinx/datetime/TimeZone;)Lcom/inkapplications/datetime/ZonedDateTime; + public static final fun toZonedDateTime (Lkotlinx/datetime/Instant;Lkotlinx/datetime/TimeZone;)Lcom/inkapplications/datetime/ZonedDateTime; } public final class com/inkapplications/datetime/ZonedTime { diff --git a/datetime/src/commonMain/kotlin/com/inkapplications/datetime/ZonedDateTime.kt b/datetime/src/commonMain/kotlin/com/inkapplications/datetime/ZonedDateTime.kt index 95f8ddd..b7a66dc 100644 --- a/datetime/src/commonMain/kotlin/com/inkapplications/datetime/ZonedDateTime.kt +++ b/datetime/src/commonMain/kotlin/com/inkapplications/datetime/ZonedDateTime.kt @@ -132,3 +132,8 @@ fun LocalDateTime.atZone(zone: TimeZone) = ZonedDateTime( localDateTime = this, zone = zone, ) + +/** + * Create a [ZonedDateTime] from an [Instant] and [TimeZone]. + */ +fun Instant.toZonedDateTime(zone: TimeZone) = toLocalDateTime(zone).atZone(zone)