If I understand correctly, `kotlin.time.Instant` a...
# kotlinx-datetime
c
If I understand correctly,
kotlin.time.Instant
and
kotlin.time.Clock
were introduced in Kotlin 2.1.20 Then, kotlinx.serialization added support for these 2 types in v1.9.0, but that requires Kotlin 2.2 What are my options if I want to stay on Kotlin 2.1.20 and want to serialize kotlin.time.Instant types? Is the
compat
artifact useful for this situation?
j
It's single-digit LOC to write a custom serializer for instant.
You're basically just calling
toEpochMilliseconds
to
fromEpochMilliseconds
and you're done.
c
IIRC kotlinx.datetime had 2 separate serializers (one would use the ISO format and the other I think would save epoch seconds and nanoseconds components). I need to be able to deserialize data that has already been stored (using the now-removed serializers from kotlinx.datetime). I can probably look into my persisted data to figure out which one of those is being used and then implement that in my app.
h
The generated serializer uses the iso format. So to keep the same behavior with 2.2.0 you only need the iso serializer, that is incredible easy.
👍🏽 1
n
Copy code
object InstantSerializer : FormattedInstantSerializer(
    "ISO", DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET
)
or such iirc