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 kotlin.time.*
fun main() {
//sampleStart
println(Duration.nanoseconds(25).toIsoString()) // PT0.000000025S
println(Duration.milliseconds(120.3).toIsoString()) // PT0.120300S
println(Duration.seconds(30.5).toIsoString()) // PT30.500S
println(Duration.minutes(30.5).toIsoString()) // PT30M30S
println(Duration.seconds(86420).toIsoString()) // PT24H0M20S
println(Duration.days(2).toIsoString()) // PT48H
println(Duration.ZERO.toIsoString()) // PT0S
println(Duration.INFINITE.toIsoString()) // PT2147483647H
//sampleEnd
}