Millwork
    Preparing search index...

    Module @millwork-ts/temporal

    @millwork-ts/temporal

    Millwork codecs for the Temporal API. Each codec reads a wire value into a Temporal object, and writes it back. Temporal keeps nanoseconds, so this package does not drop digits.

    pnpm add @millwork-ts/core @millwork-ts/temporal
    

    The codecs use the global Temporal. Firefox 139+ and Chrome 144+ have it. For other browsers and for Node 22, install the polyfill and import it one time, before the app reads data:

    pnpm add temporal-polyfill
    
    import 'temporal-polyfill/global'
    

    The package does not import the polyfill. Each codec reads globalThis.Temporal when it runs, not when the module loads. So the import stays sync, and the polyfill can load first or last. If there is no global Temporal, hydrate gives undefined with a report, and isValue is false.

    The types come from the TypeScript lib esnext.temporal. The package adds a reference to it, so you do not have to change your tsconfig.json. TypeScript 6.0 has this lib. Older versions are not tested and can fail to find it.

    Each codec is a named export. The codecs record has each codec two times: with its kind as the key (date) and with the adapter prefix (temporal:date). The name of each codec has the prefix. If your app registers two date families, use the prefixed keys.

    Export Key Wire Value
    date date "2026-12-01" Temporal.PlainDate
    datetime datetime RFC 3339, "2026-12-01T14:30:00-05:00" or "…Z" Temporal.Instant
    epoch epoch a number, or a string for large values Temporal.Instant
    time time "14:30", "14:30:00.123456789" Temporal.PlainTime
    localDateTime local-datetime "2026-12-01T14:30:00" Temporal.PlainDateTime
    zonedDateTime zoned-datetime "2026-12-01T14:30:00-05:00[America/Toronto]", { at, zone } Temporal.ZonedDateTime
    yearMonth year-month "2026-12" Temporal.PlainYearMonth
    duration duration ISO 8601 "PT1H30M", or a number Temporal.Duration
    interval interval "a/b", "a/PT1H", "P1D/b", { start, end } frozen { start, end } of Instant

    Options:

    • datetime: an Instant has no offset, so serialize always writes Z. The keepOffset option has no effect. Use zonedDateTime to keep a zone.
    • epoch: unit is 's', 'ms' (the default), 'us' or 'ns'. serialize writes a number when it is a safe integer. It writes 'ns' as a string, such as "1796153400123456789".
    • zonedDateTime: wire: 'object' writes { at, zone }. The default writes RFC 9557. Both are read. The wire offset must be the offset of the zone at that instant, or the value is bad. A [u-ca=…] calendar annotation is kept.
    • duration: unit is 'iso' (the default), 's' or 'ms'. A duration with years, months or weeks cannot become a number, so serialize throws a RangeError. A day is 24 hours.
    • interval: wire: 'object' writes { start, end }. A duration is added in UTC. An open interval (..) is bad input. The interval is half-open: [start, end).

    Bad wire input gives undefined and a report to onInvalid. A value of the wrong type makes serialize throw a TypeError.

    Temporal objects do not survive structuredClone. Keep the raw string in the cache, and hydrate it when you read the field.

    • date, time, local-datetime, year-month, datetime and epoch use Temporal equals.
    • zonedDateTime compares the instant and the zone. temporalFamily.zonedDateTime({ strict: true }) gives a codec that uses Temporal equals, which also compares the calendar.
    • interval compares the two instants.
    • duration compares years, months and weeks one by one, and the other units by length. PT1H equals PT60M. P1M does not equal P30D.

    Register the codecs before the store calls transformations(), because transformations() reads the registry when it runs. See the @millwork-ts/warp-drive README.

    import 'temporal-polyfill/global'
    import { codecs } from '@millwork-ts/temporal'
    import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
    import { withDefaults } from '@warp-drive/core/reactive'

    registerCodecs(codecs)

    export const EventSchema = withDefaults({
    type: 'event',
    fields: [
    valueField('startsOn', 'temporal:date'),
    valueField('startsAt', 'temporal:zoned-datetime', { wire: 'object' }),
    valueField('length', 'temporal:duration'),
    ],
    })
    IntervalOptions
    StrictOptions
    TemporalInterval
    TemporalTypes
    ZonedOptions
    IntervalRaw
    ZonedRaw
    codecs
    date
    datetime
    duration
    epoch
    interval
    localDateTime
    temporalFamily
    time
    yearMonth
    zonedDateTime