aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-remote/src/main/kotlin/pl
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules/kvision-remote/src/main/kotlin/pl')
-rw-r--r--kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt58
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"
+}