Skip to content

DateTimeComponents.Format parse throws an exception when timeZoneId() is UTC #444

Closed
@LotuxPunk

Description

@LotuxPunk

I have this piece of code to handle a Long date (20241016153100) to an ISO8106 date, it works for most of the cases, but if tz is UTC, the id will be Z, and throw an exception.

DateTimeComponents.Format {
    year()
    monthNumber()
    dayOfMonth()
    hour()
    minute()
    second()
    char('|')
    timeZoneId()
}.parse("$date|${(tz.id)}").let {
    buildString {
        append(it.year.toString().padStart(2, '0'))
        append('-')
        append(it.monthNumber.toString().padStart(2, '0'))
        append('-')
        append(it.dayOfMonth.toString().padStart(2, '0'))
        append('T')
        append(it.hour.toString().padStart(2, '0'))
        append(':')
        append(it.minute.toString().padStart(2, '0'))
        append(':')
        append(it.second.toString().padStart(2, '0'))
        append(tz.offsetAt(it.toInstantUsingOffset()).asTimeZone().offset.toString())
    }
}
Errors: position 15: 'Expected timeZoneId but got ', position 0: 'Expected - but got 2', position 0: 'Expected + but got 2'kotlinx.datetime.DateTimeFormatException: Failed to parse value from '20231013000000|Z'

The workaround I have now is to change Z to Europe/London and it seems to works, but it would seems fair that timeZoneId could recognise Z as a valid timeZoneId.

Metadata

Metadata

Labels

formattersRelated to parsing and formatting

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions