toLong
Returns the value of this duration expressed as a Long number of the specified unit.
The part of this duration that is smaller than the specified unit becomes a fractional part of the result and then is truncated (rounded towards zero).
If the result doesn't fit in the range of Long type, it is coerced into that range:
Long.MIN_VALUE is returned if it's less than
Long.MIN_VALUE,Long.MAX_VALUE is returned if it's greater than
Long.MAX_VALUE.
An infinite duration value is converted either to Long.MAX_VALUE or Long.MIN_VALUE depending on its sign.
Since Kotlin
1.6Samples
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
fun main() {
//sampleStart
println(2900.milliseconds.toLong(DurationUnit.SECONDS)) // 2
println(3.hours.toLong(DurationUnit.MINUTES)) // 180
println(1.minutes.toLong(DurationUnit.NANOSECONDS)) // 60000000000
println("(365 * 300).days.toLong(DurationUnit.NANOSECONDS) == Long.MAX_VALUE is ${(365 * 300).days.toLong(DurationUnit.NANOSECONDS) == Long.MAX_VALUE}") // true
println("(-Duration.INFINITE).toLong(DurationUnit.DAYS) == Long.MIN_VALUE is ${(-Duration.INFINITE).toLong(DurationUnit.DAYS) == Long.MIN_VALUE}") // true
//sampleEnd
}