summaryrefslogtreecommitdiff
path: root/taler-kotlin-android/src/main
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2022-11-17 13:53:48 -0300
committerTorsten Grote <t@grobox.de>2023-01-03 08:25:05 -0300
commit02ad9bde3e671a3566d68a6b968efe288e67d910 (patch)
tree9ff3cfc86c31def600fbdad0bba9bf103e65368f /taler-kotlin-android/src/main
parentec0738e633076e2d601a624eb8c4b06b6ebf22d8 (diff)
downloadtaler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.tar.gz
taler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.tar.bz2
taler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.zip
Adapt to new seconds based time format
Diffstat (limited to 'taler-kotlin-android/src/main')
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/Time.kt68
1 files changed, 22 insertions, 46 deletions
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/Time.kt b/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
index 61bbce8..f280d5f 100644
--- a/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
+++ b/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
@@ -29,50 +29,31 @@ import kotlin.math.max
@Serializable
data class Timestamp(
- @SerialName("t_ms")
- @Serializable(NeverSerializer::class)
- val old_ms: Long? = null,
@SerialName("t_s")
@Serializable(NeverSerializer::class)
- private val s: Long? = null,
+ private val s: Long,
) : Comparable<Timestamp> {
- constructor(ms: Long) : this(ms, null)
-
companion object {
private const val NEVER: Long = -1
- fun now(): Timestamp = Timestamp(System.currentTimeMillis())
+ fun now(): Timestamp = fromMillis(System.currentTimeMillis())
fun never(): Timestamp = Timestamp(NEVER)
+ fun fromMillis(ms: Long) = Timestamp(ms / 1000L)
}
- val ms: Long = if (s != null) {
- s * 1000L
- } else if (old_ms !== null) {
- old_ms
- } else {
- throw Exception("timestamp didn't have t_s or t_ms")
- }
-
-
- /**
- * Returns a copy of this [Timestamp] rounded to seconds.
- */
- fun truncateSeconds(): Timestamp {
- if (ms == NEVER) return Timestamp(ms)
- return Timestamp((ms / 1000L) * 1000L)
- }
+ val ms: Long = s * 1000L
- operator fun minus(other: Timestamp): Duration = when {
- ms == NEVER -> Duration(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: Timestamp): RelativeTime = when {
+ ms == NEVER -> RelativeTime.fromMillis(RelativeTime.FOREVER)
+ other.ms == NEVER -> throw Error("Invalid argument for timestamp comparison")
+ ms < other.ms -> RelativeTime.fromMillis(0)
+ else -> RelativeTime.fromMillis(ms - other.ms)
}
- operator fun minus(other: Duration): Timestamp = when {
+ operator fun minus(other: RelativeTime): Timestamp = when {
ms == NEVER -> this
- other.ms == Duration.FOREVER -> Timestamp(0)
- else -> Timestamp(max(0, ms - other.ms))
+ other.ms == RelativeTime.FOREVER -> fromMillis(0)
+ else -> fromMillis(max(0, ms - other.ms))
}
override fun compareTo(other: Timestamp): Int {
@@ -88,26 +69,21 @@ data class Timestamp(
}
@Serializable
-data class Duration(
- @SerialName("d_ms")
- @Serializable(ForeverSerializer::class) val old_ms: Long? = null,
- @SerialName("d_s")
+data class RelativeTime(
+ /**
+ * Duration in microseconds or "forever" to represent an infinite duration.
+ * Numeric values are capped at 2^53 - 1 inclusive.
+ */
+ @SerialName("d_us")
@Serializable(ForeverSerializer::class)
- private val s: Long? = null,
+ private val s: Long,
) {
- val ms: Long = if (s != null) {
- s * 1000L
- } else if (old_ms !== null) {
- old_ms
- } else {
- throw Exception("duration didn't have d_s or d_ms")
- }
-
- constructor(ms: Long) : this(ms, null)
+ val ms: Long = s * 1000L
companion object {
internal const val FOREVER: Long = -1
- fun forever(): Duration = Duration(FOREVER)
+ fun forever(): RelativeTime = fromMillis(FOREVER)
+ fun fromMillis(ms: Long) = RelativeTime(ms / 100L)
}
}