toInt
Returns the value of this duration expressed as an Int 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 Int type, it is coerced into that range:
Int.MIN_VALUE is returned if it's less than
Int.MIN_VALUE,Int.MAX_VALUE is returned if it's greater than
Int.MAX_VALUE.
An infinite duration value is converted either to Int.MAX_VALUE or Int.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.toInt(DurationUnit.SECONDS)) // 2
println(3.hours.toInt(DurationUnit.MINUTES)) // 180
println("1.minutes.toInt(DurationUnit.NANOSECONDS) == Int.MAX_VALUE is ${1.minutes.toInt(DurationUnit.NANOSECONDS) == Int.MAX_VALUE}") // true
println("(-Duration.INFINITE).toInt(DurationUnit.DAYS) == Int.MIN_VALUE is ${(-Duration.INFINITE).toInt(DurationUnit.DAYS) == Int.MIN_VALUE}") // true
//sampleEnd
}