mirror of
https://github.com/ev-map/EVMap.git
synced 2026-02-25 19:07:00 -05:00
OpenChargeMap ZonedDateTimeAdapter: fallback to UTC if zone not given
This commit is contained in:
@@ -2,12 +2,20 @@ package net.vonforst.evmap.api.openchargemap
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.ZonedDateTime
|
||||
import java.time.format.DateTimeParseException
|
||||
|
||||
internal class ZonedDateTimeAdapter {
|
||||
@FromJson
|
||||
fun fromJson(value: String?): ZonedDateTime? = value?.let {
|
||||
ZonedDateTime.parse(value)
|
||||
try {
|
||||
ZonedDateTime.parse(value)
|
||||
} catch (e: DateTimeParseException) {
|
||||
val dt: LocalDateTime = LocalDateTime.parse(value)
|
||||
dt.atZone(ZoneOffset.UTC)
|
||||
}
|
||||
}
|
||||
|
||||
@ToJson
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.vonforst.evmap.api.openchargemap
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.time.ZoneOffset
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
class OpenChargeMapAdaptersTest {
|
||||
@Test
|
||||
fun testZonedDateTimeAdapter() {
|
||||
val adapter = ZonedDateTimeAdapter()
|
||||
assertEquals(
|
||||
ZonedDateTime.of(2022, 3, 19, 23, 24, 0, 0, ZoneOffset.UTC),
|
||||
adapter.fromJson("2022-03-19T23:24:00Z")
|
||||
)
|
||||
assertEquals(
|
||||
ZonedDateTime.of(2022, 3, 19, 23, 24, 0, 0, ZoneOffset.UTC),
|
||||
adapter.fromJson("2022-03-19T23:24:00")
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user