Skip to content

Commit

Permalink
Time fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Aug 5, 2024
1 parent 8ace0af commit d773c31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 8 additions & 4 deletions shared/src/commonMain/kotlin/com/lightningkite/ZonedDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)
Expand Down Expand Up @@ -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))
)
Expand Down
14 changes: 12 additions & 2 deletions shared/src/commonTest/kotlin/TimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }))
}
}
}

0 comments on commit d773c31

Please sign in to comment.