Hello, does format ISO8601:2004 is supported ?
# kotlinx-datetime
r
Hello, does format ISO8601:2004 is supported ?
d
ISO 8601 is a collection of many formats. Some of them are provided by the library, some aren't.
r
Is there a list of format supported?
d
Nope. Please take a look at the list of classes provided by the library (https://kotlinlang.org/api/kotlinx-datetime/kotlinx-datetime/kotlinx.datetime/): most of them have `parse`/`toString` functions compatible with ISO 8601. Additionally, you can create custom formats (https://kotlinlang.org/api/kotlinx-datetime/kotlinx-datetime/kotlinx.datetime.format/-date-time-format-builder/) to suit your requirements.
r
Thanks, to fully understand the
InstantIso8601Serializer
doesn't support all format of the ISO 8601 ?
d
It only supports the extended ISO 8601 format suitable for
kotlinx.datetime.Instant
. It would be meaningless to try to parse
P15DT12H
as an
Instant
(
DateTimePeriod
should be used for this format); likewise,
2024-01-16
is not an
Instant
(it's a
LocalDate
), and neither is something like
2024-10-23T21:59/2025-01-16T12:00
(which is a time interval, we don't provide those, as there doesn't seem to be any demand).
Also, there isn't a lot of sense in explicitly specifying `InstantIso8601Serializer`:
Instant
is
@Serializable
by default, and it uses the ISO 8601 strings as the format. Please see https://kotlinlang.org/api/kotlinx-datetime/kotlinx-datetime/kotlinx.datetime.format[…]omponents/-formats/-i-s-o_-d-a-t-e_-t-i-m-e_-o-f-f-s-e-t.html
r
I was searching for format without a year like
--01-13T12:00
. Thank you very much
d
It's not provided out of the box (because there is no entity like
MonthDay
in the library, everything includes the year), but you can still define it:
Copy code
import kotlinx.datetime.format.*
import kotlinx.datetime.*

fun main() {
    val format = DateTimeComponents.Format {
        chars("--"); monthNumber(); char('-'); dayOfMonth()
        alternativeParsing({ char('t') }) { char('T') }
        time(LocalTime.Formats.ISO)
    }
    format.parse("--01-13T12:00").let {
        println("Month is ${it.month}")
        println("Day is ${it.dayOfMonth}")
        println("Time is ${it.toLocalTime()}")
    }
}
Prints
Copy code
Month is JANUARY
Day is 13
Time is 12:00
Runnable: https://pl.kotl.in/_Lo55HPq8
r
Nice! Thank you, I knew this lib was very powerful !