aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt
blob: f3526beb984fc778cfe9402fcaa8178ec38777f8 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.util

import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource

@OptIn(ExperimentalTime::class)
class TimeMark private constructor(private val timeMark: TimeSource.Monotonic.ValueTimeMark?) : Comparable<TimeMark> {
    fun passedTime() = timeMark?.elapsedNow() ?: Duration.INFINITE

    companion object {
        fun now() = TimeMark(TimeSource.Monotonic.markNow())
        fun farPast() = TimeMark(null)
    }

    override fun compareTo(other: TimeMark): Int {
        if (this.timeMark == other.timeMark) return 0
        if (this.timeMark == null) return -1
        if (other.timeMark == null) return -1
        return this.timeMark.compareTo(other.timeMark)
    }
}