How can I refactor this to instead use `kotlin.tim...
# stdlib
z
How can I refactor this to instead use
kotlin.time.Duration
? I thought this would be simple (it probably still is) but my first attempt didnt result in the behavior I was expecting, and Im not 100% clear on why that is (code for that in 🧵).
Copy code
fun round(
    duration: Long,
    period: Long = 1000,
): Long {
    val half = period / 2
    return (duration + half) / period * period
}
This does not produce the same results:
Copy code
fun round(
    duration: Duration,
    period: Duration = 1.seconds,
): Duration {
    val half = period / 2
    return (duration + half) / period * period
}
s
in your original function with duration as Long,
(duration + half) / period
uses Long division meaning that entire decimal section is cut out, so that for example
(9800 + 500) / 1000
gives 10, whereas
Duration / Duration
uses double division meaning above example would give 10.3. If you want version with
Duration
to give same results as the one with
Long
you may for example use
floor
to ignore decimal part of division:
floor((duration + half) / period) * period
k
It uses Long (not double) division, but represents the durations in nanoseconds. So, one way to do it is this:
Copy code
val periodNanos = period.inWholeNanoseconds
    return ((duration + period / 2).inWholeNanoseconds / periodNanos * periodNanos).nanoseconds
s
k
@Szymon Jeziorski you're right. I was looking at
Duration.div(Int)
instead. So it uses Long division for
val half = period / 2
but indeed it uses Double division for dividing by
period
.
z
Thank you both, that makes perfect sense! 🙏🏽