Standard library / time · on the one-page reference
time
Stable — one clock read each, the same primitive already used elsewhere in the compiler; pure computation, works identically on native and WebAssembly.
Millisecond timestamps as i64, not
int: real epoch milliseconds hugely exceeds a 32-bit range,
so this returns the wider type rather than a truncated, effectively
meaningless value. Note that i64 values can't be compared or
used in arithmetic directly against an int literal yet -- keep both sides
of a comparison as i64.
time.now()
→ i64 — milliseconds since the Unix epoch (wall-clock/real time).
const n: i64 = time.now();
console.log(n);
Date.now()
→ i64 — the familiar JS spelling of
time.now(): milliseconds since the Unix epoch. Promotes to
number in float contexts. The rest of the Date
object is planned.
const start: i64 = Date.now();
// ... work ...
console.log(Date.now() - start >= 0); // true
time.monotonic()
→ i64 — milliseconds from an arbitrary, consistent starting point; never goes backwards, safe for measuring elapsed durations, unaffected by the system clock being changed.
const start: i64 = time.monotonic();
// ... do work ...
const elapsed: i64 = time.monotonic() - start;
console.log(elapsed);