to Iso String
Returns an ISO-8601 based string representation of this duration.
The returned value is presented in the format PThHmMs.fS
, where h
, m
, s
are the integer components of this duration (see toComponents) and f
is a fractional part of second. Depending on the roundness of the value the fractional part can be formatted with either 0, 3, 6, or 9 decimal digits.
If the hours component absolute value of this duration is greater than Int.MAX_VALUE, it is replaced with Int.MAX_VALUE, so the infinite duration is formatted as `"PT2147483647H".
Negative durations are indicated with the sign -
in the beginning of the returned string, for example, "-PT5M30S"
.
Samples
import samples.*
import kotlin.time.*
fun main() {
//sampleStart
assertPrints(Duration.nanoseconds(25).toIsoString(), "PT0.000000025S")
assertPrints(Duration.milliseconds(120.3).toIsoString(), "PT0.120300S")
assertPrints(Duration.seconds(30.5).toIsoString(), "PT30.500S")
assertPrints(Duration.minutes(30.5).toIsoString(), "PT30M30S")
assertPrints(Duration.seconds(86420).toIsoString(), "PT24H0M20S")
assertPrints(Duration.days(2).toIsoString(), "PT48H")
assertPrints(Duration.ZERO.toIsoString(), "PT0S")
assertPrints(Duration.INFINITE.toIsoString(), "PT2147483647H")
//sampleEnd
}