ISO
ISO 8601 extended format.
Examples: 12:34
, 12:34:56
, 12:34:56.789
, 12:34:56.1234
.
When formatting, seconds are always included, even if they are zero. Fractional parts of the second are included if non-zero.
Guaranteed to parse all strings that LocalTime.toString produces.
See ISO-8601-1:2019. Any of the extended formats in 5.3.1.2b), 5.3.1.4a), and 5.3.1.3a) can be used, depending on whether seconds and fractional seconds are non-zero. The length of the fractional part is flexible between one and nine digits. The only allowed separator between seconds and fractional seconds is the dot .
. We forbid using the time designator T
to allow for a predictable composition of formats: see the note at the end of rule 5.3.5.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() {
//sampleStart
// Parsing and formatting LocalTime values using the ISO format
val timeWithNanoseconds = LocalTime(hour = 8, minute = 30, second = 15, nanosecond = 160_000_000)
val timeWithSeconds = LocalTime(hour = 8, minute = 30, second = 15)
val timeWithoutSeconds = LocalTime(hour = 8, minute = 30)
check(LocalTime.Formats.ISO.parse("08:30:15.16") == timeWithNanoseconds)
check(LocalTime.Formats.ISO.parse("08:30:15") == timeWithSeconds)
check(LocalTime.Formats.ISO.parse("08:30") == timeWithoutSeconds)
check(LocalTime.Formats.ISO.format(timeWithNanoseconds) == "08:30:15.16")
check(LocalTime.Formats.ISO.format(timeWithSeconds) == "08:30:15")
check(LocalTime.Formats.ISO.format(timeWithoutSeconds) == "08:30:00")
//sampleEnd
}