Skip to content

Commit

Permalink
Add Duration.toMicros() / toRoundedMicros() / toRoundedMillis().
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanLennox committed Sep 4, 2024
1 parent d37ff41 commit a623d00
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/kotlin/org/jitsi/utils/Duration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,55 @@ operator fun Duration.times(x: Number): Duration = this.multipliedBy(x.toLong())
operator fun Number.times(x: Duration): Duration = x.multipliedBy(this.toLong())

operator fun Duration.div(other: Duration): Double = toNanos().toDouble() / other.toNanos()

/** Converts this duration to the total length in milliseconds, rounded to nearest. */
fun Duration.toRoundedMillis(): Long {
var millis = this.toMillis()
val remainder = nano % NANOS_PER_MILLI
if (remainder > 499_999) {
millis++
}
return millis
}

/**
* Converts this duration to the total length in microseconds.
*
* If this duration is too large to fit in a `long` microseconds, then an
* exception is thrown.
*
* If this duration has greater than microseconds precision, then the conversion
* will drop any excess precision information as though the amount in nanoseconds
* was subject to integer division by one thousand.
*
* @return the total length of the duration in microseconds
* @throws ArithmeticException if numeric overflow occurs
*/
fun Duration.toMicros(): Long {
var tempSeconds: Long = seconds
var tempNanos: Long = nano.toLong()
if (tempSeconds < 0) {
// change the seconds and nano value to
// handle Long.MIN_VALUE case
tempSeconds += 1
tempNanos -= NANOS_PER_SECOND
}
var micros = Math.multiplyExact(tempSeconds, MICROS_PER_SECOND)
micros = Math.addExact(micros, tempNanos / NANOS_PER_MICRO)
return micros
}

/** Converts this duration to the total length in microseconds, rounded to nearest. */
fun Duration.toRoundedMicros(): Long {
var micros = this.toMicros()
val remainder = nano % NANOS_PER_MICRO
if (remainder > 499) {
micros++
}
return micros
}

private const val NANOS_PER_MICRO = 1_000
private const val NANOS_PER_MILLI = 1_000_000
private const val MICROS_PER_SECOND = 1_000_000
private const val NANOS_PER_SECOND = 1_000_000_000
12 changes: 12 additions & 0 deletions src/test/kotlin/org/jitsi/utils/DurationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class DurationTest : ShouldSpec() {
2 * 4.hours shouldBe Duration.ofHours(8)
10.days / 2.days shouldBe 5.0
5.hours / 2.hours shouldBe 2.5

4.ms.toMicros() shouldBe 4000
2200.nanos.toMicros() shouldBe 2
2900.nanos.toMicros() shouldBe 2

2200.nanos.toRoundedMicros() shouldBe 2
2500.nanos.toRoundedMicros() shouldBe 3
2900.nanos.toRoundedMicros() shouldBe 3

2200.micros.toRoundedMillis() shouldBe 2
2500.micros.toRoundedMillis() shouldBe 3
2900.micros.toRoundedMillis() shouldBe 3
}
}
}

0 comments on commit a623d00

Please sign in to comment.