Change Duration value storage unit to ns

This commit is contained in:
Ilya Gorbunov
2019-04-06 03:13:10 +03:00
parent 690049c082
commit b9c56af9ba
2 changed files with 11 additions and 10 deletions
+8 -6
View File
@@ -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<Duration> {
@@ -91,18 +91,20 @@ public inline class Duration internal constructor(internal val _value: Double) :
// perhaps better name would be 'letComponents'
inline fun <T> 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 <T> 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
+3 -4
View File
@@ -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)
}
}
}