Vita Sokolova
05/16/2024, 9:16 AMkotlinx-datetime
to parse offset dates like: 2020-02-25T130909+01:00. But I can’t find out how to use
DateTimeFormat<DateTimeComponents> to format LocalDateTime. I looked through docs and found several examples for the opposite conversion, but got stuck with formatting LocalDateTime back to string.Dmitry Khalanskiy [JB]
05/16/2024, 9:24 AMLocalDateTime
using this format because this format includes an offset, and LocalDateTime
doesn't include one, so there is nothing to meaningfully format. You can do
offsetDateTimeFormat.format {
setDateTimeOffset(value, someOffset)
}
but you need to choose the offset to use.Vita Sokolova
05/16/2024, 9:33 AMLocalDateTime
. What should I do to have an access to the information about offset and do not ignore/lose it?Vita Sokolova
05/16/2024, 9:36 AMoverride fun deserialize(decoder: Decoder): LocalDateTime {
val instant = offsetDateTimeFormat.parse(decoder.decodeString()).toInstantUsingOffset()
return instant.toLocalDateTime(TimeZone.currentSystemDefault())
}
will this be better solution?CLOVIS
05/17/2024, 7:53 AMLocalDateTime
is just a datetime with no timezone information at all. It can't ever store anything more than 2020-02-25T13:09:09
, because if it did, then by definition it would something else than a LocalDateTime
.
If you gave this to an instant, 2020-02-25T13:09:09+01:00
would be stored as 2020-02-25T12:09:09Z
. If your goal is to compare two events to know which one happened first, that's the correct data structure. However, it doesn't store in which timezone the event happened.
If you want to store the exact timezone, then you essentially want a Pair<LocalDateTime, FixedOffsetTimeZone>
, which I believe is what Java calls a ZonedDateTime
but I don't think exists in KotlinX.Datetime (yet?)Dmitry Khalanskiy [JB]
05/17/2024, 8:22 AMInstant
remembers the moment in time. From its point of view, 2020-02-25T13:09:09+01:00
is exactly the same thing as 2020-02-25T14:09:09+02:00
. Use this if you want to know the moment in time, but don't care in which timezone it happens. This is for things like "the moment this e-mail was sent".
• LocalDateTime
remembers what the clocks say. It can't distinguish between 2020-02-25T13:09:09+01:00
and 2020-02-25T13:09:09-10:00
. Use this if you want to know what the residents of the timezone see on their clocks, but don't care in which timezone it happens. For example, this is ok for local train schedules.
• DateTimeComponents
doesn't have specific semantics, it's just a bunch of values grouped together for parsing and formatting. Use it if you need to know both the offset and the moment in time.Vita Sokolova
05/17/2024, 8:30 AMLocalDateTime
according to my user’s timezone. I believe the code I posted above does it.Dmitry Khalanskiy [JB]
05/17/2024, 8:34 AMInstantIso8601Serializer
and convert to and from Instant
, and then to convert the Instant
to the LocalDateTime
outside of the serializer.Dmitry Khalanskiy [JB]
05/17/2024, 8:39 AMInstant
to a LocalDateTime
until the very last moment when you want to show the datetime to the user. For example, if the user changed the timezone on their device, it doesn't mean that the program should re-request the same data from the server, but the displayed text should update. If you're storing Instant
values throughout your program, this is achieved automatically, whereas with LocalDateTime
, you have to deal with cache invalidation.Vita Sokolova
05/17/2024, 8:42 AMVita Sokolova
05/17/2024, 8:42 AM