diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-12 18:24:15 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-12 18:24:15 +0200 |
commit | 6ea600defefbe16e59c421785d9a09989e672083 (patch) | |
tree | 7f3344b56f9c3fe1f8c65bf67195539a149af15f /kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft | |
parent | bcf7504392baccf1568e740c1d453eac7080fb5b (diff) | |
download | kvision-6ea600defefbe16e59c421785d9a09989e672083.tar.gz kvision-6ea600defefbe16e59c421785d9a09989e672083.tar.bz2 kvision-6ea600defefbe16e59c421785d9a09989e672083.zip |
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.
Diffstat (limited to 'kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft')
-rw-r--r-- | kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt | 58 |
1 files changed, 55 insertions, 3 deletions
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<Date> { +internal object JsonDateSerializer : KSerializer<Date> { 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" +} |