Skip to content

Commit 147f034

Browse files
committed
add MonotonicInstant.elapsed()
1 parent f658a12 commit 147f034

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/kotlin/ru/kontur/kinfra/commons/time/MonotonicInstant.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ public class MonotonicInstant private constructor(
2727
return Duration.ofNanos(nanoOffset - other.nanoOffset)
2828
}
2929

30+
public fun elapsedNow(): Duration {
31+
return Duration.ofNanos(nowNanos() - nanoOffset)
32+
}
33+
3034
override fun compareTo(other: MonotonicInstant): Int {
31-
// implies that nanoOffset is monotonic
35+
// implies that nanoOffset is monotonic, that is it cannot overflow
3236
return nanoOffset.compareTo(other.nanoOffset)
3337
}
3438

@@ -49,7 +53,12 @@ public class MonotonicInstant private constructor(
4953
private val ORIGIN_NANOS = rawNow()
5054

5155
public fun now(): MonotonicInstant {
52-
return MonotonicInstant(rawNow() - ORIGIN_NANOS)
56+
return MonotonicInstant(nowNanos())
57+
}
58+
59+
private fun nowNanos(): Long {
60+
// raw value is not used for correctness of compareTo()
61+
return rawNow() - ORIGIN_NANOS
5362
}
5463

5564
private fun rawNow(): Long {

0 commit comments

Comments
 (0)