diff --git a/libraries/stdlib/src/kotlin/time/Duration.kt b/libraries/stdlib/src/kotlin/time/Duration.kt index 538419e9667..7f5c01924cc 100644 --- a/libraries/stdlib/src/kotlin/time/Duration.kt +++ b/libraries/stdlib/src/kotlin/time/Duration.kt @@ -5,7 +5,7 @@ package kotlin.time -private val storageUnit = DurationUnit.MICROSECONDS +private val storageUnit = DurationUnit.NANOSECONDS @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") public inline class Duration internal constructor(internal val _value: Double) : Comparable { @@ -91,18 +91,20 @@ public inline class Duration internal constructor(internal val _value: Double) : // perhaps better name would be 'letComponents' inline fun withComponents(action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = - action(inHours.toInt(), minutesComponent.toInt(), secondsComponent.toInt(), (inNanoseconds % 1e9).toInt()) + action(inHours.toInt(), minutesComponent, secondsComponent, nanosecondsComponent) inline fun withComponents(action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = - action(inDays.toInt(), hoursComponent.toInt(), minutesComponent.toInt(), secondsComponent.toInt(), (inNanoseconds % 1e9).toInt()) + action(inDays.toInt(), hoursComponent, minutesComponent, secondsComponent, nanosecondsComponent) @PublishedApi - internal val hoursComponent: Double get() = inHours % 24 + internal val hoursComponent: Int get() = (inHours % 24).toInt() @PublishedApi - internal val minutesComponent: Double get() = inMinutes % 60 + internal val minutesComponent: Int get() = (inMinutes % 60).toInt() @PublishedApi - internal val secondsComponent: Double get() = inSeconds % 60 + internal val secondsComponent: Int get() = (inSeconds % 60).toInt() + @PublishedApi + internal val nanosecondsComponent: Int get() = (inNanoseconds % 1e9).toInt() // conversion to units diff --git a/libraries/stdlib/test/time/DurationTest.kt b/libraries/stdlib/test/time/DurationTest.kt index 0325b7f7d70..448ff732ee2 100644 --- a/libraries/stdlib/test/time/DurationTest.kt +++ b/libraries/stdlib/test/time/DurationTest.kt @@ -6,12 +6,11 @@ package test.time import test.numbers.assertAlmostEquals -import kotlin.math.* import kotlin.test.* import kotlin.time.* import kotlin.random.* -private val expectStorageUnit = DurationUnit.MICROSECONDS +private val expectStorageUnit = DurationUnit.NANOSECONDS private val units = DurationUnit.values() class DurationTest { @@ -134,14 +133,14 @@ class DurationTest { assertEquals(h, hours) assertEquals(m, minutes) assertEquals(s, seconds) - assertTrue(abs(ns - nanoseconds) < 2, "ns component of duration ${this.toIsoString()} differs too much, expected: $ns, actual: $nanoseconds") + assertEquals(ns, nanoseconds, "ns component of duration ${this.toIsoString()} differs too much, expected: $ns, actual: $nanoseconds") } withComponents { days, hours, minutes, seconds, nanoseconds -> assertEquals(0, days) assertEquals(h, hours) assertEquals(m, minutes) assertEquals(s, seconds) - assertTrue(abs(ns - nanoseconds) < 2) + assertEquals(ns, nanoseconds) } } }