scoop:scoop.platform:0.1.0
src/time/monotonic_instant.scoop
Selected production source · src/time/monotonic_instant.scoop
1
package scoop.platform.time3
import scoop.time.Duration5
/**6
* A point on the system monotonic clock, expressed as seconds plus a signed nanosecond adjustment.7
* Clock reads use a fraction in 0..999999999. Arithmetic and comparison normalize other8
* adjustments and throw ArithmeticException if the resulting seconds are outside Long range.9
*/10
public struct MonotonicInstant(val seconds: Long, val nanosecondAdjustment: Int)11
: Equals<MonotonicInstant>, Compare<MonotonicInstant>, Hash,12
Add<Duration, MonotonicInstant>, Sub<Duration, MonotonicInstant> {13
/** Creates a timestamp from a normalized reading in the system monotonic clock domain. */14
public constructor(reading: Duration): this(15
reading.floorSeconds,16
reading.nanosecondsOfSecond17
) {}19
public override operator fun equals(other: MonotonicInstant): Boolean = compareTo(other) == 0L21
public override operator fun compareTo(other: MonotonicInstant): Long {22
val left = normalized()23
val right = other.normalized()24
if (left.seconds != right.seconds)25
return left.seconds.compareTo(right.seconds)26
return left.nanosecondAdjustment.compareTo(right.nanosecondAdjustment)27
}29
public override fun hash(): Long {30
val value = normalized()31
return value.seconds.hash() * 31L + value.nanosecondAdjustment.hash()32
}34
public override operator fun plus(other: Duration): MonotonicInstant =35
MonotonicInstant(reading() + other)37
public override operator fun minus(other: Duration): MonotonicInstant =38
MonotonicInstant(reading() - other)40
public operator fun minus(other: MonotonicInstant): Duration = reading() - other.reading()42
internal fun reading(): Duration = Duration.seconds(seconds, nanosecondAdjustment.toLong())44
/** Returns the same instant with a fractional adjustment in 0..999999999. */45
public fun normalized(): MonotonicInstant =46
if (nanosecondAdjustment >= 0 && nanosecondAdjustment < 1000000000)47
this48
else49
MonotonicInstant(reading())50
}