Duration
@ExperimentalTime inline class Duration :
Comparable<Duration>Represents the amount of time one instant of time is away from another instant.
A negative duration is possible in a situation when the second instant is earlier than the first one. An infinite duration value Duration.INFINITE can be used to represent infinite timeouts.
To construct a duration use either the extension function toDuration, or the extension properties hours, minutes, seconds, and so on, available on Int, Long, and Double numeric types.
To get the value of this duration expressed in a particular duration units use the functions toInt, toLong, and toDouble or the properties inHours, inMinutes, inSeconds, inNanoseconds, and so on.
Properties
absoluteValue
Returns the absolute value of this value. The returned value is always non-negative.
val absoluteValue: DurationinMicroseconds
The value of this duration expressed as a Double number of microseconds.
val inMicroseconds: DoubleinMilliseconds
The value of this duration expressed as a Double number of milliseconds.
val inMilliseconds: DoubleinNanoseconds
The value of this duration expressed as a Double number of nanoseconds.
val inNanoseconds: DoubleFunctions
div
isFinite
Returns true, if the duration value is finite.
fun isFinite(): BooleanisInfinite
Returns true, if the duration value is infinite.
fun isInfinite(): BooleanisNegative
Returns true, if the duration value is less than zero.
fun isNegative(): BooleanisPositive
Returns true, if the duration value is greater than zero.
fun isPositive(): BooleantoComponents
Splits this duration into days, hours, minutes, seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.
fun <T> toComponents(
action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T
): TSplits this duration into hours, minutes, seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.
fun <T> toComponents(
action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T
): TtoDouble
Returns the value of this duration expressed as a Double number of the specified unit.
fun toDouble(unit: DurationUnit): DoubletoInt
Returns the value of this duration expressed as an Int number of the specified unit.
fun toInt(unit: DurationUnit): InttoIsoString
Returns an ISO-8601 based string representation of this duration.
fun toIsoString(): StringtoLong
Returns the value of this duration expressed as a Long number of the specified unit.
fun toLong(unit: DurationUnit): LongtoLongMilliseconds
Returns the value of this duration expressed as a Long number of milliseconds.
fun toLongMilliseconds(): LongtoLongNanoseconds
Returns the value of this duration expressed as a Long number of nanoseconds.
fun toLongNanoseconds(): LongtoString
Returns a string representation of this duration value expressed in the unit which yields the most compact and readable number value.
fun toString(): StringReturns a string representation of this duration value expressed in the given unit and formatted with the specified decimals number of digits after decimal point.
fun toString(unit: DurationUnit, decimals: Int = 0): StringunaryMinus
Returns the negative of this value.
operator fun unaryMinus(): DurationCompanion Object Properties
Companion Object Functions
convert
Converts the given time duration value expressed in the specified sourceUnit into the specified targetUnit.
fun convert(
value: Double,
sourceUnit: DurationUnit,
targetUnit: DurationUnit
): DoubleExtension Functions
coerceAtLeast
Ensures that this value is not less than the specified minimumValue.
fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): TcoerceAtMost
Ensures that this value is not greater than the specified maximumValue.
fun <T : Comparable<T>> T.coerceAtMost(maximumValue: T): TcoerceIn
Ensures that this value lies in the specified range minimumValue..maximumValue.
fun <T : Comparable<T>> T.coerceIn(
minimumValue: T?,
maximumValue: T?
): TEnsures that this value lies in the specified range.
fun <T : Comparable<T>> T.coerceIn(
range: ClosedFloatingPointRange<T>
): Tfun <T : Comparable<T>> T.coerceIn(range: ClosedRange<T>): TrangeTo
Creates a range from this Comparable value to the specified that value.
operator fun <T : Comparable<T>> T.rangeTo(
that: T
): ClosedRange<T>toJavaDuration
Converts kotlin.time.Duration value to value java.time.Duration.