https://kotlinlang.org logo
#kotlinx-datetime
Title
# kotlinx-datetime
d

Dmitry Khalanskiy [JB]

02/29/2024, 12:48 PM
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!
💥 3
🎉 18
j

John O'Reilly

02/29/2024, 1:13 PM
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

Dmitry Khalanskiy [JB]

02/29/2024, 1:15 PM
Sorry, you'll have to clarify the question. How do you want to use it?
j

John O'Reilly

02/29/2024, 1:18 PM
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

Dmitry Khalanskiy [JB]

03/01/2024, 9:48 AM
Yep. If you want English month names specifically, please use
monthName(MonthNames.ENGLISH_ABBREVIATED)
.
j

John O'Reilly

03/01/2024, 10:08 AM
where is that?
d

Dmitry Khalanskiy [JB]

03/01/2024, 10:12 AM
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

John O'Reilly

03/01/2024, 10:27 AM
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

Dmitry Khalanskiy [JB]

03/01/2024, 10:31 AM
chars(", ")
👍 1
j

John O'Reilly

03/01/2024, 11:28 AM
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

florent

03/01/2024, 1:46 PM
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

Dmitry Khalanskiy [JB]

03/01/2024, 1:55 PM
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

florent

03/01/2024, 1:58 PM
Perfect thanks, you rock! Maybe we should add this to the readme?
d

Dmitry Khalanskiy [JB]

03/01/2024, 2:01 PM
f

florent

03/01/2024, 2:03 PM
I was thinking just to mention the existence of
timeZoneId()
d

Dmitry Khalanskiy [JB]

03/01/2024, 2:06 PM
Sure, we can do that. There's more urgent feedback right now, but I've written this suggestion down. Thanks!
🙏 1
j

Joel Denke

03/01/2024, 5:43 PM
Is the intention add Locale support as well, or strictly invariant Locale? :)
d

Dmitry Khalanskiy [JB]

03/02/2024, 9:41 AM
2 Views