summaryrefslogtreecommitdiff
path: root/common/src/commonMain/kotlin/net/taler/lib/common/Time.kt
blob: 1ed54b5edade2609913d7f2841734562ff793077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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.lib.common

import com.soywiz.klock.DateTime
import kotlinx.serialization.Contextual
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.builtins.nullable
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.jsonPrimitive
import kotlinx.serialization.json.longOrNull
import net.taler.lib.common.Duration.Companion.FOREVER
import kotlin.math.max

@Serializable
public data class Timestamp(
    @SerialName("t_ms")
    @Serializable(NeverSerializer::class)
    public val old_ms: Long? = null,
    @SerialName("t_s")
    @Serializable(NeverSerializer::class)
    private val s: Long? = null,
) : Comparable<Timestamp> {

    public constructor(ms: Long) : this(ms, null) {}

    public companion object {
        private const val NEVER: Long = -1
        public fun now(): Timestamp = Timestamp(DateTime.nowUnixLong())
        public fun never(): Timestamp = Timestamp(NEVER)
    }

    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.
     */
    public fun truncateSeconds(): Timestamp {
        if (ms == NEVER) return Timestamp(ms)
        return Timestamp((ms / 1000L) * 1000L)
    }

    public 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)
    }

    public 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
public data class Duration(
    @SerialName("d_ms")
    @Serializable(ForeverSerializer::class)
    public val old_ms: Long? = null,
    @SerialName("d_s")
    @Serializable(ForeverSerializer::class)
    private val s: Long? = null,
) {
    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")
    };
    public constructor(ms: Long) : this(ms, null) {}

    public companion object {
        internal const val FOREVER: Long = -1
        public fun forever(): Duration = Duration(FOREVER)
    }
}

internal abstract class MinusOneSerializer(private val keyword: String) :
    JsonTransformingSerializer<Long>(Long.serializer()) {

    override fun transformDeserialize(element: JsonElement): JsonElement {
        return if (element.jsonPrimitive.contentOrNull == keyword) return JsonPrimitive(-1)
        else super.transformDeserialize(element)
    }

    override fun transformSerialize(element: JsonElement): JsonElement {
        return if (element.jsonPrimitive.longOrNull == -1L) return JsonPrimitive(keyword)
        else element
    }
}

internal object NeverSerializer : MinusOneSerializer("never")
internal object ForeverSerializer : MinusOneSerializer("forever")