From d773c318db293650041ea37ce35a9bf61781d661 Mon Sep 17 00:00:00 2001 From: Joseph Ivie Date: Mon, 5 Aug 2024 13:01:31 -0600 Subject: [PATCH] Time fixes --- .../kotlin/com/lightningkite/ZonedDateTime.kt | 12 ++++++++---- shared/src/commonTest/kotlin/TimeTest.kt | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/lightningkite/ZonedDateTime.kt b/shared/src/commonMain/kotlin/com/lightningkite/ZonedDateTime.kt index 339da488..423dc4ff 100644 --- a/shared/src/commonMain/kotlin/com/lightningkite/ZonedDateTime.kt +++ b/shared/src/commonMain/kotlin/com/lightningkite/ZonedDateTime.kt @@ -17,10 +17,12 @@ data class ZonedDateTime(val dateTime: LocalDateTime, val zone: TimeZone) { override fun toString(): String = "$dateTime${zone.offsetAt(dateTime.toInstant(zone))}[${zone.id}]" companion object { fun parse(string: String): ZonedDateTime { - val dateTimeFinishedIndex = string.indexOfAny(charArrayOf('Z', '[', '+', '-'), 19) + var dateTimeFinishedIndex = string.indexOfAny(charArrayOf('Z', '[', '+', '-'), 17) + if(dateTimeFinishedIndex == -1) dateTimeFinishedIndex = string.length return ZonedDateTime( LocalDateTime.parse(string.substring(0, dateTimeFinishedIndex)), - if(string.contains('[')) TimeZone.of(string.substringAfterLast('[').substringBefore(']')) + if(dateTimeFinishedIndex == string.length) TimeZone.UTC + else if(string.contains('[')) TimeZone.of(string.substringAfterLast('[').substringBefore(']')) else if(string[dateTimeFinishedIndex] == 'Z') TimeZone.UTC else FixedOffsetTimeZone(UtcOffset.parse(string.substring(dateTimeFinishedIndex, string.length))) ) @@ -56,10 +58,12 @@ data class OffsetDateTime(val dateTime: LocalDateTime, val offset: UtcOffset) { override fun toString(): String = "$dateTime$offset" companion object { fun parse(string: String): OffsetDateTime { - val dateTimeFinishedIndex = string.indexOfAny(charArrayOf('Z', '[', '+', '-'), 19) + var dateTimeFinishedIndex = string.indexOfAny(charArrayOf('Z', '[', '+', '-'), 19) + if(dateTimeFinishedIndex == -1) dateTimeFinishedIndex = string.length return OffsetDateTime( LocalDateTime.parse(string.substring(0, dateTimeFinishedIndex)), - if(string.contains('[')) TimeZone.of(string.substringAfterLast('[').substringBefore(']')).offsetAt(now()) + if(dateTimeFinishedIndex == string.length) UtcOffset.ZERO + else if(string.contains('[')) TimeZone.of(string.substringAfterLast('[').substringBefore(']')).offsetAt(now()) else if(string[dateTimeFinishedIndex] == 'Z') UtcOffset.ZERO else UtcOffset.parse(string.substring(dateTimeFinishedIndex, string.length)) ) diff --git a/shared/src/commonTest/kotlin/TimeTest.kt b/shared/src/commonTest/kotlin/TimeTest.kt index 037f7183..06665862 100644 --- a/shared/src/commonTest/kotlin/TimeTest.kt +++ b/shared/src/commonTest/kotlin/TimeTest.kt @@ -3,16 +3,26 @@ package com.lightningkite.lightningdb import com.lightningkite.OffsetDateTime import com.lightningkite.ZonedDateTime import com.lightningkite.nowLocal +import kotlinx.datetime.TimeZone +import kotlinx.datetime.UtcOffset import kotlin.test.Test import kotlin.test.assertEquals class TimeTest { @Test fun testZoned() { val x = nowLocal() - assertEquals(x, ZonedDateTime.parse(x.toString().also { println(it) })) + TimeZone.availableZoneIds.forEach { zoneId -> + val zone = TimeZone.of(zoneId) + val y = x.copy(zone = zone) + assertEquals(y, ZonedDateTime.parse(y.toString().also { println(it) })) + } } @Test fun testOffset() { val x = nowLocal().toOffsetDateTime() - assertEquals(x, OffsetDateTime.parse(x.toString().also { println(it) })) + (-12..12).forEach { hours -> + val offset = UtcOffset(hours) + val y = x.copy(offset = offset) + assertEquals(y, OffsetDateTime.parse(y.toString().also { println(it) })) + } } } \ No newline at end of file