Add shortcuts to convert duration to milliseconds and nanoseconds
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user