Date & Time

Slumber supports three datetime libraries out of the box: java.time, kotlinx.datetime, and Ultra's multiplatform MpDateTime.

The format

All datetime types serialize to the same structure:

{
    "ts": 1649116800000,         // epoch milliseconds (machine-readable)
    "timezone": "Europe/Berlin", // timezone ID
    "human": "2022-04-05T..."    // ISO-8601 string (human-readable)
}

This format stores both the precise timestamp and the timezone, so no information is lost. The human field is for debugging — deserialization uses only ts and timezone.

java.time types

import java.time.*

val codec = Codec.default

// Instant — absolute point in time
val instant = Instant.ofEpochMilli(1649116800000L)
codec.slumber(instant)
// → { "ts": 1649116800000, "timezone": "UTC", "human": "2022-04-05T00:00:00Z" }

// ZonedDateTime — instant + timezone
val zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("Europe/Berlin"))
codec.slumber(zdt)
// → { "ts": 1649116800000, "timezone": "Europe/Berlin", "human": "2022-04-05T02:00+02:00[Europe/Berlin]" }

// LocalDate — date without time, stored as start-of-day UTC
val date = LocalDate.of(2022, 4, 5)
codec.slumber(date)
// → { "ts": 1649116800000, "timezone": "UTC", "human": "2022-04-05T00:00Z[UTC]" }

// LocalDateTime — date + time without timezone, stored as UTC
val dateTime = LocalDateTime.of(2022, 4, 5, 10, 30, 0)
codec.slumber(dateTime)
// → { "ts": 1649154600000, "timezone": "UTC", "human": "2022-04-05T10:30Z[UTC]" }

// LocalTime — time of day, stored as second-of-day
val time = LocalTime.of(14, 30, 15)
codec.slumber(time)
// → 52215 (seconds since midnight)

Supported java.time types: Instant, ZonedDateTime, LocalDate, LocalDateTime, LocalTime, ZoneId, and java.util.Date.

kotlinx.datetime types

import kotlinx.datetime.*

val codec = Codec.default

// LocalDate
val date = LocalDate(2022, Month.APRIL, 5)
codec.slumber(date)
// → { "ts": 1649116800000, "timezone": "UTC", "human": "2022-04-05T00:00:00Z" }

// LocalDateTime
val dateTime = LocalDateTime(2022, Month.APRIL, 5, 10, 30, 0)
codec.slumber(dateTime)
// → { "ts": ..., "timezone": "UTC", "human": "..." }

MpDateTime types (Ultra multiplatform)

Ultra's own multiplatform datetime types follow the same format:

import io.peekandpoke.ultra.common.datetime.*

val codec = Codec.default

val instant = MpLocalDate.of(2022, kotlinx.datetime.Month.APRIL, 5)
    .atStartOfDay(kotlinx.datetime.TimeZone.UTC)
    .toInstant()

codec.slumber(instant)
// → { "ts": 1649116800000, "timezone": "UTC", "human": "2022-04-05T00:00:00.000Z" }

Supported MpDateTime types: MpInstant, MpZonedDateTime, MpLocalDate, MpLocalDateTime, MpLocalTime, MpTimezone.

DateTime in data classes

data class Event(
    val name: String,
    val startTime: Instant,
    val endTime: Instant?,
)

val event = Event(
    name = "Conference",
    startTime = Instant.ofEpochMilli(1649116800000L),
    endTime = null,
)

codec.slumber(event)
// → {
//   "name": "Conference",
//   "startTime": { "ts": 1649116800000, "timezone": "UTC", "human": "..." },
//   "endTime": null
// }

Timezone preservation

ZonedDateTime preserves the original timezone through serialization:

val berlin = ZonedDateTime.ofInstant(
    Instant.ofEpochMilli(1649116800000L),
    ZoneId.of("Europe/Berlin"),
)

val restored = codec.awake<ZonedDateTime>(codec.slumber(berlin))
// restored.zone == ZoneId.of("Europe/Berlin") ✓