W3cubDocs

/Kotlin

Duration

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
@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

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

absoluteValue

Returns the absolute value of this value. The returned value is always non-negative.

val absoluteValue: Duration
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inDays

The value of this duration expressed as a Double number of days.

val inDays: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inHours

The value of this duration expressed as a Double number of hours.

val inHours: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inMicroseconds

The value of this duration expressed as a Double number of microseconds.

val inMicroseconds: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inMilliseconds

The value of this duration expressed as a Double number of milliseconds.

val inMilliseconds: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inMinutes

The value of this duration expressed as a Double number of minutes.

val inMinutes: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inNanoseconds

The value of this duration expressed as a Double number of nanoseconds.

val inNanoseconds: Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

inSeconds

The value of this duration expressed as a Double number of seconds.

val inSeconds: Double

Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

compareTo

fun compareTo(other: Duration): Int
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

div

Returns a duration whose value is this duration value divided by the given scale number.

operator fun div(scale: Int): Duration
operator fun div(scale: Double): Duration

Returns a number that is the ratio of this and other duration values.

operator fun div(other: Duration): Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

isFinite

Returns true, if the duration value is finite.

fun isFinite(): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

isInfinite

Returns true, if the duration value is infinite.

fun isInfinite(): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

isNegative

Returns true, if the duration value is less than zero.

fun isNegative(): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

isPositive

Returns true, if the duration value is greater than zero.

fun isPositive(): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

minus

Returns a duration whose value is the difference between this and other duration values.

operator fun minus(other: Duration): Duration
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

plus

Returns a duration whose value is the sum of this and other duration values.

operator fun plus(other: Duration): Duration
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

times

Returns a duration whose value is this duration value multiplied by the given scale number.

operator fun times(scale: Int): Duration
operator fun times(scale: Double): Duration
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toComponents

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
): T

Splits 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
): T

Splits this duration into 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: (minutes: Int, seconds: Int, nanoseconds: Int) -> T
): T

Splits this duration into 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: (seconds: Long, nanoseconds: Int) -> T
): T
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toDouble

Returns the value of this duration expressed as a Double number of the specified unit.

fun toDouble(unit: DurationUnit): Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toInt

Returns the value of this duration expressed as an Int number of the specified unit.

fun toInt(unit: DurationUnit): Int
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toIsoString

Returns an ISO-8601 based string representation of this duration.

fun toIsoString(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toLong

Returns the value of this duration expressed as a Long number of the specified unit.

fun toLong(unit: DurationUnit): Long
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toLongMilliseconds

Returns the value of this duration expressed as a Long number of milliseconds.

fun toLongMilliseconds(): Long
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toLongNanoseconds

Returns the value of this duration expressed as a Long number of nanoseconds.

fun toLongNanoseconds(): Long
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toString

Returns a string representation of this duration value expressed in the unit which yields the most compact and readable number value.

fun toString(): String

Returns 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): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

unaryMinus

Returns the negative of this value.

operator fun unaryMinus(): Duration

Companion Object Properties

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

INFINITE

The duration whose value is positive infinity. It is useful for representing timeouts that should never expire.

val INFINITE: Duration
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

ZERO

The duration equal to exactly 0 seconds.

val ZERO: Duration

Companion Object Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

convert

Converts the given time duration value expressed in the specified sourceUnit into the specified targetUnit.

fun convert(
    value: Double, 
    sourceUnit: DurationUnit, 
    targetUnit: DurationUnit
): Double

Extension Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

coerceAtLeast

Ensures that this value is not less than the specified minimumValue.

fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): T
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

coerceAtMost

Ensures that this value is not greater than the specified maximumValue.

fun <T : Comparable<T>> T.coerceAtMost(maximumValue: T): T
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

coerceIn

Ensures that this value lies in the specified range minimumValue..maximumValue.

fun <T : Comparable<T>> T.coerceIn(
    minimumValue: T?, 
    maximumValue: T?
): T

Ensures that this value lies in the specified range.

fun <T : Comparable<T>> T.coerceIn(
    range: ClosedFloatingPointRange<T>
): T
fun <T : Comparable<T>> T.coerceIn(range: ClosedRange<T>): T
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

rangeTo

Creates a range from this Comparable value to the specified that value.

operator fun <T : Comparable<T>> T.rangeTo(
    that: T
): ClosedRange<T>
Platform and version requirements: JVM (1.3), JRE8 (1.3)

toJavaDuration

Converts kotlin.time.Duration value to java.time.Duration value.

fun Duration.toJavaDuration(): Duration

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/-duration/index.html