Add shortcuts to convert duration to milliseconds and nanoseconds

This commit is contained in:
Ilya Gorbunov
2019-05-23 21:20:13 +03:00
parent 8004c34e44
commit 796ad29637
2 changed files with 10 additions and 0 deletions
@@ -100,6 +100,10 @@ public inline class Duration internal constructor(internal val _value: Double) :
val inMicroseconds: Double get() = toDouble(DurationUnit.MICROSECONDS)
val inNanoseconds: Double get() = toDouble(DurationUnit.NANOSECONDS)
// shortcuts
fun toLongNanoseconds(): Long = toLong(DurationUnit.NANOSECONDS)
fun toLongMilliseconds(): Long = toLong(DurationUnit.MILLISECONDS)
override fun toString(): String = buildString {
if (isInfinite()) {
@@ -113,6 +113,12 @@ class DurationTest {
assertEquals(0.0, Duration.ZERO.inNanoseconds)
assertEquals(10500, 10.5.seconds.toLongMilliseconds())
assertEquals(11, 11.5.milliseconds.toLongMilliseconds())
assertEquals(-11, (-11.5).milliseconds.toLongMilliseconds())
assertEquals(252_000_000, 252.milliseconds.toLongNanoseconds())
assertEquals(Long.MAX_VALUE, (365.days * 293).toLongNanoseconds()) // clamping overflowed value
repeat(100) {
val value = Random.nextLong(1000)
val unit = units.random()