|
17 | 17 | package org.jitsi.utils
|
18 | 18 |
|
19 | 19 | import java.time.Duration
|
| 20 | +import java.time.temporal.ChronoUnit |
20 | 21 |
|
21 | 22 | /**
|
22 |
| - * Helpers to create instance of [Duration] more easily |
| 23 | + * Helpers to create instances of [Duration] more easily, and Kotlin operators for it |
23 | 24 | */
|
24 | 25 |
|
25 |
| -val Number.nanos: Duration |
| 26 | +val Int.nanos: Duration |
26 | 27 | get() = Duration.ofNanos(this.toLong())
|
27 | 28 |
|
28 |
| -val Number.ms: Duration |
| 29 | +val Int.micros: Duration |
| 30 | + get() = Duration.of(this.toLong(), ChronoUnit.MICROS) |
| 31 | + |
| 32 | +val Int.ms: Duration |
29 | 33 | get() = Duration.ofMillis(this.toLong())
|
30 | 34 |
|
31 |
| -val Number.secs: Duration |
| 35 | +val Int.secs: Duration |
32 | 36 | get() = Duration.ofSeconds(this.toLong())
|
33 | 37 |
|
34 |
| -val Number.hours: Duration |
| 38 | +val Int.hours: Duration |
35 | 39 | get() = Duration.ofHours(this.toLong())
|
36 | 40 |
|
37 |
| -val Number.mins: Duration |
| 41 | +val Int.mins: Duration |
38 | 42 | get() = Duration.ofMinutes(this.toLong())
|
39 | 43 |
|
40 |
| -val Number.days: Duration |
| 44 | +val Int.days: Duration |
41 | 45 | get() = Duration.ofDays(this.toLong())
|
42 | 46 |
|
43 |
| -operator fun Duration.times(x: Int): Duration = Duration.ofNanos(toNanos() * x) |
| 47 | +val Long.nanos: Duration |
| 48 | + get() = Duration.ofNanos(this) |
| 49 | + |
| 50 | +val Long.micros: Duration |
| 51 | + get() = Duration.of(this, ChronoUnit.MICROS) |
| 52 | + |
| 53 | +val Long.ms: Duration |
| 54 | + get() = Duration.ofMillis(this) |
| 55 | + |
| 56 | +val Long.secs: Duration |
| 57 | + get() = Duration.ofSeconds(this) |
| 58 | + |
| 59 | +val Long.hours: Duration |
| 60 | + get() = Duration.ofHours(this) |
| 61 | + |
| 62 | +val Long.mins: Duration |
| 63 | + get() = Duration.ofMinutes(this) |
| 64 | + |
| 65 | +val Long.days: Duration |
| 66 | + get() = Duration.ofDays(this) |
| 67 | + |
| 68 | +operator fun Duration.times(x: Int): Duration = this.multipliedBy(x.toLong()) |
| 69 | +operator fun Duration.times(x: Long): Duration = this.multipliedBy(x) |
| 70 | + |
| 71 | +operator fun Int.times(x: Duration): Duration = x.multipliedBy(this.toLong()) |
| 72 | +operator fun Long.times(x: Duration): Duration = x.multipliedBy(this) |
| 73 | + |
44 | 74 | operator fun Duration.div(other: Duration): Double = toNanos().toDouble() / other.toNanos()
|
| 75 | + |
| 76 | +/** Converts this duration to the total length in milliseconds, rounded to nearest. */ |
| 77 | +fun Duration.toRoundedMillis(): Long { |
| 78 | + var millis = this.toMillis() |
| 79 | + val remainder = nano % NANOS_PER_MILLI |
| 80 | + if (remainder > 499_999) { |
| 81 | + millis++ |
| 82 | + } |
| 83 | + return millis |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Converts this duration to the total length in microseconds. |
| 88 | + * |
| 89 | + * If this duration is too large to fit in a `long` microseconds, then an |
| 90 | + * exception is thrown. |
| 91 | + * |
| 92 | + * If this duration has greater than microseconds precision, then the conversion |
| 93 | + * will drop any excess precision information as though the amount in nanoseconds |
| 94 | + * was subject to integer division by one thousand. |
| 95 | + * |
| 96 | + * @return the total length of the duration in microseconds |
| 97 | + * @throws ArithmeticException if numeric overflow occurs |
| 98 | + */ |
| 99 | +fun Duration.toMicros(): Long { |
| 100 | + var tempSeconds: Long = seconds |
| 101 | + var tempNanos: Long = nano.toLong() |
| 102 | + if (tempSeconds < 0) { |
| 103 | + // change the seconds and nano value to |
| 104 | + // handle Long.MIN_VALUE case |
| 105 | + tempSeconds += 1 |
| 106 | + tempNanos -= NANOS_PER_SECOND |
| 107 | + } |
| 108 | + var micros = Math.multiplyExact(tempSeconds, MICROS_PER_SECOND) |
| 109 | + micros = Math.addExact(micros, tempNanos / NANOS_PER_MICRO) |
| 110 | + return micros |
| 111 | +} |
| 112 | + |
| 113 | +/** Converts this duration to the total length in microseconds, rounded to nearest. */ |
| 114 | +fun Duration.toRoundedMicros(): Long { |
| 115 | + var micros = this.toMicros() |
| 116 | + val remainder = nano % NANOS_PER_MICRO |
| 117 | + if (remainder > 499) { |
| 118 | + micros++ |
| 119 | + } |
| 120 | + return micros |
| 121 | +} |
| 122 | + |
| 123 | +private const val NANOS_PER_MICRO = 1_000 |
| 124 | +private const val NANOS_PER_MILLI = 1_000_000 |
| 125 | +private const val MICROS_PER_SECOND = 1_000_000 |
| 126 | +private const val NANOS_PER_SECOND = 1_000_000_000 |
0 commit comments