diff options
author | Torsten Grote <t@grobox.de> | 2020-08-18 11:35:28 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-08-18 11:36:07 -0300 |
commit | 612876e44de35cdbd563ac2ce40dcd1d4e397bc5 (patch) | |
tree | b2127aff7376b7d2304e300d216d838a2e1d2595 /taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt | |
parent | 451157d992a4c771e3bdf80b6563f80fb1582c77 (diff) | |
download | taler-android-612876e44de35cdbd563ac2ce40dcd1d4e397bc5.tar.gz taler-android-612876e44de35cdbd563ac2ce40dcd1d4e397bc5.tar.bz2 taler-android-612876e44de35cdbd563ac2ce40dcd1d4e397bc5.zip |
Include Taler common Kotlin library as a submodule
Diffstat (limited to 'taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt')
-rw-r--r-- | taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt b/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt deleted file mode 100644 index 37b6606..0000000 --- a/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt +++ /dev/null @@ -1,106 +0,0 @@ -/* - * This file is part of GNU Taler - * (C) 2020 Taler Systems S.A. - * - * GNU Taler is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 3, or (at your option) any later version. - * - * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> - */ - -package net.taler.common - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable -import kotlinx.serialization.builtins.serializer -import kotlinx.serialization.json.JsonElement -import kotlinx.serialization.json.JsonPrimitive -import kotlinx.serialization.json.JsonTransformingSerializer -import kotlinx.serialization.json.contentOrNull -import kotlinx.serialization.json.longOrNull -import net.taler.common.Duration.Companion.FOREVER -import kotlin.math.max - -expect fun nowMillis(): Long - -@Serializable -data class Timestamp( - @SerialName("t_ms") - @Serializable(NeverSerializer::class) - val ms: Long -) : Comparable<Timestamp> { - - companion object { - const val NEVER: Long = -1 - fun now(): Timestamp = Timestamp(nowMillis()) - } - - /** - * Returns a copy of this [Timestamp] rounded to seconds. - */ - fun truncateSeconds(): Timestamp { - if (ms == NEVER) return Timestamp(ms) - return Timestamp((ms / 1000L) * 1000L) - } - - operator fun minus(other: Timestamp): Duration = when { - ms == NEVER -> Duration(FOREVER) - other.ms == NEVER -> throw Error("Invalid argument for timestamp comparision") - ms < other.ms -> Duration(0) - else -> Duration(ms - other.ms) - } - - operator fun minus(other: Duration): Timestamp = when { - ms == NEVER -> this - other.ms == FOREVER -> Timestamp(0) - else -> Timestamp(max(0, ms - other.ms)) - } - - override fun compareTo(other: Timestamp): Int { - return if (ms == NEVER) { - if (other.ms == NEVER) 0 - else 1 - } else { - if (other.ms == NEVER) -1 - else ms.compareTo(other.ms) - } - } - -} - -@Serializable -data class Duration( - /** - * Duration in milliseconds. - */ - @SerialName("d_ms") - @Serializable(ForeverSerializer::class) - val ms: Long -) { - companion object { - const val FOREVER: Long = -1 - } -} - -abstract class MinusOneSerializer(private val keyword: String) : - JsonTransformingSerializer<Long>(Long.serializer(), keyword) { - - override fun readTransform(element: JsonElement): JsonElement { - return if (element.contentOrNull == keyword) return JsonPrimitive(-1) - else super.readTransform(element) - } - - override fun writeTransform(element: JsonElement): JsonElement { - return if (element.longOrNull == -1L) return JsonPrimitive(keyword) - else element - } -} - -object NeverSerializer : MinusOneSerializer("never") -object ForeverSerializer : MinusOneSerializer("forever") |