From 6ea600defefbe16e59c421785d9a09989e672083 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 12 Oct 2019 18:24:15 +0200 Subject: Redesign Date type handling in server side interfaces. Map js.Date class on the client side to different java.time.* classes on the server side. --- .../main/kotlin/pl/treksoft/kvision/types/Date.kt | 58 ++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) (limited to 'kvision-modules/kvision-remote/src') diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 55025512..947bb62b 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -27,20 +27,72 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialDescriptor import kotlinx.serialization.internal.SerialClassDescImpl import kotlin.js.Date +import kotlin.math.absoluteValue +@Deprecated("Compatibility with KVision 1. Use LocalDateTime or OffsetDateTime instead.") actual typealias Date = Date +actual typealias LocalDateTime = Date + +actual typealias LocalDate = Date + +actual typealias LocalTime = Date + +actual typealias OffsetDateTime = Date + +actual typealias OffsetTime = Date + /** * JSON date serializer. */ -object JsonDateSerializer : KSerializer { +internal object JsonDateSerializer : KSerializer { override val descriptor: SerialDescriptor = SerialClassDescImpl("kotlin.js.Date") override fun deserialize(decoder: Decoder): Date { - return Date(decoder.decodeLong()) + return decoder.decodeString().toDateInternal() } override fun serialize(encoder: Encoder, obj: Date) { - encoder.encodeLong(obj.getTime().toLong()) + encoder.encodeString(obj.toStringInternal()) } } + +internal fun String.toDateInternal(): Date { + val dt = this.split(':', 'T', '-', '+') + val utcCheck = this[length - 1] == 'Z' + val ds = if (utcCheck) dt[5].dropLast(1).split(".") else dt[5].split(".") + val tzCheck = this[length - 6] + return if (!utcCheck && tzCheck != '-' && tzCheck != '+') { + Date( + dt[0].toInt(), + dt[1].toInt() - 1, + dt[2].toInt(), + dt[3].toInt(), + dt[4].toInt(), + ds[0].toInt(), + if (ds.size == 2) ds[1].toInt() else 0 + ) + } else { + val sign = if (utcCheck || tzCheck == '+') 1 else -1 + Date( + Date.UTC( + dt[0].toInt(), + dt[1].toInt() - 1, + dt[2].toInt(), + if (utcCheck) { dt[3].toInt() } else { dt[3].toInt() - sign * dt[6].toInt() }, + dt[4].toInt(), + ds[0].toInt(), + if (ds.size == 2) ds[1].toInt() else 0 + ) + ) + } +} + +internal fun Date.toStringInternal(): String { + val tz = this.getTimezoneOffset() / 60 + val sign = if (tz > 0) "-" else "+" + return "" + this.getFullYear() + "-" + ("0" + (this.getMonth() + 1)).takeLast(2) + "-" + + ("0" + this.getDate()).takeLast(2) + "T" + ("0" + this.getHours()).takeLast(2) + ":" + + ("0" + this.getMinutes()).takeLast(2) + ":" + ("0" + this.getSeconds()).takeLast(2) + "." + + this.getMilliseconds() + sign + ("0${tz.absoluteValue}").takeLast(2) + ":00" +} -- cgit