aboutsummaryrefslogtreecommitdiff
path: root/taler-kotlin-android
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
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')
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/Time.kt68
-rw-r--r--taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt8
-rw-r--r--taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt10
3 files changed, 31 insertions, 55 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)
}
}
diff --git a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
index f7b83a9..3e1ebc4 100644
--- a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
+++ b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
@@ -37,14 +37,14 @@ class ContractTermsTest {
},
"fulfillment_url":"https://shop.test.taler.net/essay/1._The_Free_Software_Definition",
"summary":"Essay: 1. The Free Software Definition",
- "refund_deadline":{"t_ms":"never"},
- "wire_transfer_deadline":{"t_ms":1596128564000},
+ "refund_deadline":{"t_s":"never"},
+ "wire_transfer_deadline":{"t_s":1596128564},
"products":[],
"h_wire":"KV40K023N8EC1F5100TYNS23C4XN68Y1Z3PTJSWFGTMCNYD54KT4S791V2VQ91SZANN86VDAA369M4VEZ0KR6DN71EVRRZA71K681M0",
"wire_method":"x-taler-bank",
"order_id":"2020.212-01M9VKEAPF76C",
- "timestamp":{"t_ms":1596128114000},
- "pay_deadline":{"t_ms":"never"},
+ "timestamp":{"t_s":1596128114},
+ "pay_deadline":{"t_s":"never"},
"max_wire_fee":"TESTKUDOS:1",
"max_fee":"TESTKUDOS:1",
"wire_fee_amortization":3,
diff --git a/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt b/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
index beda621..4049940 100644
--- a/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
+++ b/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
@@ -18,9 +18,9 @@ package net.taler.common
import kotlinx.serialization.json.Json.Default.decodeFromString
import kotlinx.serialization.json.Json.Default.encodeToString
-import kotlin.random.Random
import org.junit.Assert.assertEquals
import org.junit.Test
+import kotlin.random.Random
// TODO test other functionality of Timestamp and Duration
class TimeTest {
@@ -29,18 +29,18 @@ class TimeTest {
fun testSerialize() {
for (i in 0 until 42) {
val t = Random.nextLong()
- assertEquals("""{"t_ms":$t}""", encodeToString(Timestamp.serializer(), Timestamp(t)))
+ assertEquals("""{"t_s":$t}""", encodeToString(Timestamp.serializer(), Timestamp(t)))
}
- assertEquals("""{"t_ms":"never"}""", encodeToString(Timestamp.serializer(), Timestamp.never()))
+ assertEquals("""{"t_s":"never"}""", encodeToString(Timestamp.serializer(), Timestamp.never()))
}
@Test
fun testDeserialize() {
for (i in 0 until 42) {
val t = Random.nextLong()
- assertEquals(Timestamp(t), decodeFromString(Timestamp.serializer(), """{ "t_ms": $t }"""))
+ assertEquals(Timestamp(t), decodeFromString(Timestamp.serializer(), """{ "t_s": $t }"""))
}
- assertEquals(Timestamp.never(), decodeFromString(Timestamp.serializer(), """{ "t_ms": "never" }"""))
+ assertEquals(Timestamp.never(), decodeFromString(Timestamp.serializer(), """{ "t_s": "never" }"""))
}
}