Not only has it been merged, but now :mega: kotlin...
# kotlinx-datetime
d
Not only has it been merged, but now ๐Ÿ“ฃ kotlinx-datetime 0.6.0-RC was published! Its main change is the new locale-invariant parsing and formatting capabilities. Please try it out and let us know what you think!
๐Ÿ’ฅ 4
๐ŸŽ‰ 20
j
Just looking at converting some expect/actual code to new API....for example
Copy code
private fun getSessionTime(session: SessionDetails, timeZone: TimeZone): String {
    val dateTimeFormat = LocalDateTime.Format {
        byUnicodePattern("HH:mm")
    }
    return session.startsAt.format(dateTimeFormat)
    //return dateService.format(session.startsAt, timeZone, "HH:mm")
}
just to confirm, there isn't way right now to use timeZone?
d
Sorry, you'll have to clarify the question. How do you want to use it?
j
hmm, maybe I don't need time zone in this particular case
is it expected that
MMM
format doesn't work....this is due to the "locale-invariant" part?
d
Yep. If you want English month names specifically, please use
monthName(MonthNames.ENGLISH_ABBREVIATED)
.
j
where is that?
d
Copy code
LocalDateTime.Format {
  dayOfMonth()
  char(' ')
  monthName(MonthNames.ENGLISH_ABBREVIATED)
}
Here's the README about defining custom formats: https://github.com/Kotlin/kotlinx-datetime?tab=readme-ov-file#working-with-other-string-formats Using Java pattern strings is discouraged.
๐Ÿ‘ 1
j
thanks, that worked
one other small thing....if I want to put a space after the comma below do I need to add another
char
or is there some other way of doing that?
Copy code
val confDateFormat = LocalDateTime.Format {
            dayOfMonth()
            char(' ')
            monthName(MonthNames.ENGLISH_ABBREVIATED)
            char(',')
            year()
        }
d
chars(", ")
๐Ÿ‘ 1
j
btw looks like there might be abi compatibility issue with this version.....more details in https://kotlinlang.slack.com/archives/C01A6KM1SBZ/p1709291527353039 cc @bod
๐Ÿ‘ 1
f
Hi, thanks for the new features, I tested today and I got a format stored in DB like that 2023-11-26T232206.428362Z[Europe/London] (it was using the java string version before) Do you support that format as well?
d
Sure:
Copy code
val string = "2023-11-26T23:22:06.428362Z[Europe/London]"
val format = DateTimeComponents.Format {
    dateTimeComponents(DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET)
    char('[')
    timeZoneId()
    char(']')
}
format.parse(string).apply {
    check(toLocalDateTime() == LocalDateTime(2023, 11, 26, 23, 22, 6, 428362000))
    check(toUtcOffset() == UtcOffset.ZERO)
    check(timeZoneId == "Europe/London")
    check(toInstantUsingOffset() == LocalDateTime(2023, 11, 26, 23, 22, 6, 428362000).toInstant(UtcOffset.ZERO))
}
f
Perfect thanks, you rock! Maybe we should add this to the readme?
d
f
I was thinking just to mention the existence of
timeZoneId()
d
Sure, we can do that. There's more urgent feedback right now, but I've written this suggestion down. Thanks!
๐Ÿ™ 1
j
Is the intention add Locale support as well, or strictly invariant Locale? :)
d
๐Ÿ‘๐Ÿผ 1
๐Ÿ‘Œ 1