Skip to main content

Module time

Module time 

Source
Expand description

Portable monotonic time.

PortableInstant is a plain u64 — milliseconds elapsed since an implementation-chosen origin (typically application start). The runtime never asks the platform “what time is it now”; instead, the host emits a PortableInstant per frame (or per event) drawn from whatever monotonic clock it prefers, and the runtime uses it for comparisons / deadlines.

Desktop hosts derive PortableInstant from std::time::Instant (monotonic by construction). WASM hosts derive it from performance.now() (also monotonic). Because the origin is host-chosen, values from different hosts are not comparable — this is a host-bounded monotonic clock, not a global wall clock. Use UNIX_EPOCH-relative timestamps for anything user-visible or persisted; use PortableInstant for ephemeral things like animation timers and request deadlines.

Design choice: why a newtype instead of a plain u64?

  • Prevents accidental mixing of “ms since epoch” (a u64) with “ms-as-duration” (also u64). A deadline plus a duration yields a new deadline; a deadline minus another deadline yields a duration. The arithmetic is encoded in the method signatures.
  • Makes the portability intent explicit at type-checking time: call sites that still reach for std::time::Instant::now() will fail to compile against a PortableInstant field, surfacing the host-boundary violation.

WASM portability: no std::time::Instant, no std::time::SystemTime::now(), no std::thread. PortableInstant is pure u64 arithmetic; the wall-clock helpers use web_time.

Structs§

PortableInstant
Host-provided monotonic timestamp, measured in milliseconds from a host-chosen origin.