diff options
author | Linnea Gräf <nea@nea.moe> | 2024-01-15 00:32:43 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-01-17 21:10:51 +0100 |
commit | ac151c8ebc4c5546795cdbf5b0c179183e2c71d1 (patch) | |
tree | 52141110008ba6809d0dde5bc4456fc37e6a665a /src/main/kotlin/moe/nea/firmament/util/TimeMark.kt | |
parent | c49b65835d37266508561e60782bda36275fb8ae (diff) | |
download | firmament-ac151c8ebc4c5546795cdbf5b0c179183e2c71d1.tar.gz firmament-ac151c8ebc4c5546795cdbf5b0c179183e2c71d1.tar.bz2 firmament-ac151c8ebc4c5546795cdbf5b0c179183e2c71d1.zip |
Add Pristine Profit Tracker
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util/TimeMark.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/TimeMark.kt | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt b/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt index f3526be..41a196d 100644 --- a/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt +++ b/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt @@ -7,22 +7,42 @@ package moe.nea.firmament.util import kotlin.time.Duration -import kotlin.time.ExperimentalTime -import kotlin.time.TimeSource +import kotlin.time.Duration.Companion.milliseconds -@OptIn(ExperimentalTime::class) -class TimeMark private constructor(private val timeMark: TimeSource.Monotonic.ValueTimeMark?) : Comparable<TimeMark> { - fun passedTime() = timeMark?.elapsedNow() ?: Duration.INFINITE +class TimeMark private constructor(private val timeMark: Long) : Comparable<TimeMark> { + fun passedTime() = if (timeMark == 0L) Duration.INFINITE else (System.currentTimeMillis() - timeMark).milliseconds + + operator fun minus(other: TimeMark): Duration { + if (other.timeMark == timeMark) + return 0.milliseconds + if (other.timeMark == 0L) + return Duration.INFINITE + if (timeMark == 0L) + return -Duration.INFINITE + return (timeMark - other.timeMark).milliseconds + } companion object { - fun now() = TimeMark(TimeSource.Monotonic.markNow()) - fun farPast() = TimeMark(null) + fun now() = TimeMark(System.currentTimeMillis()) + fun farPast() = TimeMark(0L) + fun ago(timeDelta: Duration): TimeMark { + if (timeDelta.isFinite()) { + return TimeMark(System.currentTimeMillis() - timeDelta.inWholeMilliseconds) + } + require(timeDelta.isPositive()) + return farPast() + } + } + + override fun hashCode(): Int { + return timeMark.hashCode() + } + + override fun equals(other: Any?): Boolean { + return other is TimeMark && other.timeMark == timeMark } 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) } } |