Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

instant

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Absolute point-in-time primitive with precision contract.

Syntax: instant | instant<ms> | instant<us> | instant<ns>

Category#

conversion

Examples#

radix/corpus/reference/conversio/instans/instans.fab (canonical · conversio)#

Conversio arms for instant precision family and string datetime provenance.

# =============================================================================
# instant — Conversio arms for instant precision family and string datetime provenance.
# =============================================================================
#
# What this teaches:
#   • Precision family — `instant<ms>`, `instant<us>`, `instant<ns>` let you choose sub-second precision on datetime conversion.
#   • Widening vs narrowing — coarse→fine widening is lossless; fine→coarse narrowing truncates sub-second bits.
#   • RFC3339 roundtrip — text→instant parses, instant→string emits at the declared precision without fabricated digits.
#
# Common mistakes:
#   • Converting `instant<ns>` to `instant` and expecting sub-second precision in the text output — fine→coarse narrowing truncates sub-second bits.
#
# See also: ↦, ⊥, string
# =============================================================================

# conversion — instant precision family and value provenance
#
# WHY: `string ↦ instant<N>` restores datetime provenance from wire carriers;
# `instant ↔ instant` re-tags precision; `instant ↦ string` emits RFC3339 UTC
# at the declared precision contract (no fabricated sub-precision digits).
#
# string ↦ instant<N>    — RFC3339 wire per target precision
# instant ↦ instant<ms>  — coarse→fine widening (lossless at storage)
# instant<ns> ↦ instant    — fine→coarse narrowing (truncates sub-second bits)
# instant ↦ string        — precision-honoring RFC3339 `Z` emit
# string ↦ instant ⊥ …     — explicit recovery when wire is not a datetime

main {
    let string utc ← "1979-05-27T07:32:00.123456Z"
    let string offset ← "1979-05-27T16:32:00+0900"
    let instant seconds ← utc ↦ instant
    let instant<ms> millis ← seconds ↦ instant<ms>
    let instant<us> micros ← utc ↦ instant<us>
    let instant<ns> nanos ← utc ↦ instant<ns>
    let instant normalized ← offset ↦ instant
    assert (millis ↦ string) ≡ "1979-05-27T07:32:00.000Z"
    assert (micros ↦ string) ≡ "1979-05-27T07:32:00.123456Z"
    assert normalized ≡ seconds panic "offset ingest lands on same UTC instant"
    let instant<ns> fine ← nanos
    let instant narrowed ← fine ↦ instant
    assert (narrowed ↦ string) ≡ "1979-05-27T07:32:00Z"
    let string bad ← "not-a-datetime"
    let instant recovered ← bad ↦ instant ⊥ seconds
    assert recovered ≡ seconds
    print seconds ↦ string
    print millis ↦ string
    print micros ↦ string
    print nanos ↦ string
    print narrowed ↦ string
}

Expected output:

1979-05-27T07:32:00Z
1979-05-27T07:32:00.000Z
1979-05-27T07:32:00.123456Z
1979-05-27T07:32:00.123456000Z
1979-05-27T07:32:00Z

radix/corpus/reference/keyword/instans/instans.fab (canonical · keyword)#

Absolute point-in-time primitive with precision contract.

# =============================================================================
# instant — Absolute point-in-time primitive with precision contract
# =============================================================================
#
# What this teaches:
#   • Absolute point-in-time primitive with precision contract.
#   • Related keywords: ↦, ⊥, string
#
# Common mistakes:
#   • Using `instant ∷ T` (static ascription) instead of `instant ↦ T` (runtime conversion) — use `↦` for runtime datetime parsing and extraction (SEM016).
#
# See also: ↦, ⊥, string
# =============================================================================

# instant — wall clock, precision contract, offset ingest, value extraction
#
# GRAMMAR:
#   instant | instant<ms> | instant<us> | instant<ns>
#   tempus.now() → instant<ns>; downconvert at use site
#   value ↦ instant<N> | instant ↦ string
#
# EXPECTED OUTPUT:
#   Smoke asserts exit 0 only.

import from "norma:time" tempus

fn epoch_wire(instant t) → string {
    return t ↦ string
}

main {
    # --- precision declarations from canonical nanosecond wall clock ---
    let instant<ns> nanos ← tempus.now()
    let instant secunda ← nanos ↦ instant
    let instant<ms> millis ← nanos ↦ instant<ms>
    let instant<us> micros ← nanos ↦ instant<us>

    # --- value ↦ instant: UTC wire and numeric offset normalize to same instant ---
    let value utc ← "1979-05-27T07:32:00Z"
    let value offset ← "1979-05-27T03:32:00-04:00"
    let instant parsed ← utc ↦ instant
    let instant normalized ← offset ↦ instant
    assert parsed ≡ normalized panic "offset ingest normalizes to UTC instant"

    # --- microsecond wire extracted from a value at declared precision ---
    let value carrier ← "1979-05-27T07:32:00.123456Z"
    let instant<us> fromToml ← carrier ↦ instant<us>
    assert (fromToml ↦ string) ≡ "1979-05-27T07:32:00.123456Z" panic "emit honors micros contract"

    # --- precision-tagged equality: distinct at ns, equal at ms ---
    let value leftWire ← "1979-05-27T07:32:00.123456Z"
    let value rightWire ← "1979-05-27T07:32:00.123999Z"
    let instant<ns> left ← leftWire ↦ instant<ns>
    let instant<ns> right ← rightWire ↦ instant<ns>
    assert left ≢ right panic "sub-millisecond bits differ at nanosecond precision"
    let instant<ms> leftMs ← left ↦ instant<ms>
    let instant<ms> rightMs ← right ↦ instant<ms>
    assert leftMs ≡ rightMs panic "same millisecond at declared ms precision"

    # --- cross-precision conversion: widen and narrow with observable truncation ---
    let instant<ns> fine ← parsed ↦ instant<ns>
    let instant<ms> coarse ← fine ↦ instant<ms>
    let instant seconds ← fine ↦ instant
    assert (seconds ↦ string) ≡ "1979-05-27T07:32:00Z" panic "narrow to seconds strips sub-second wire"
    let instant<ms> wider ← secunda ↦ instant<ms>
    assert wider ≤ millis panic "cross-precision comparison at coarser operand"

    # --- wire emit and parse-back through a value (same instant) ---
    let string wire ← epoch_wire(parsed)
    let value replayCarrier ← "1979-05-27T07:32:00Z"
    let instant roundtrip ← replayCarrier ↦ instant
    assert roundtrip ≡ parsed panic "wire parse-back matches UTC valor extract"
    assert wire ≡ "1979-05-27T07:32:00Z" panic "seconds-precision wire emit"
    print wire, secunda, millis, micros, nanos, fromToml, coarse
}