std/time
Wall-clock and monotonic time, plus arithmetic and ISO 8601.
The two clocks use distinct types so a wall-clock value can never
be silently mixed with a monotonic one (their epochs are unrelated).
For benchmarking use Monotonic ; for display / persistence use
Instant.
Types
StopWatch :: struct A monotonic stopwatch. Construct with start(), read elapsed time with
stop(). Cheap to copy (it holds a single zero-cost Monotonic).
Instant :: i64 Wall-clock instant: nanoseconds since the Unix epoch (1970-01-01 UTC).
Suitable for formatting, persistence, and absolute-time comparisons.
NOT suitable for measuring elapsed time : the wall clock can jump
forwards or backwards (NTP corrections, manual clock adjustments,
daylight-saving when the OS exposes local time). Use Monotonic for
elapsed measurements.
Monotonic :: i64 Monotonic instant: nanoseconds since an opaque, process-stable epoch. Guaranteed never to decrease across observations within a single process. Use for micro-benchmarks, timeouts, deadlines, and any elapsed-time measurement. NOT comparable across processes ; NOT convertible to wall-clock time.
Duration :: i64 Signed time interval with nanosecond precision. May be negative
(e.g. b - a when b < a). Range : ±~292 years (i64 ns).
TimeError :: struct Error returned by parse_iso8601 when the input doesn't conform to
the supported subset of ISO 8601.
Fields
msg: string Functions
stop :: fn(self: StopWatch) -> Duration Elapsed Duration since the stopwatch was started. Re-reads the monotonic
clock on every call, so repeated stop()s report time since start(),
not since the previous stop() ; the stopwatch is never consumed.
Parameters
| self | StopWatch | the running stopwatch. |
Returns
Duration a non-negative Duration in normal operation.from_unix_nanos :: fn(ns: i64) -> Instant Construct a wall-clock Instant from a Unix-epoch nanosecond count
(nanoseconds since 1970-01-01 UTC ; negative for pre-epoch instants).
The inverse of to_unix_nanos. Use for deterministic timestamps that
don't come from the host clock.
Parameters
| ns | i64 | nanoseconds since the Unix epoch. |
Returns
since :: fn(self: Monotonic) -> Duration Elapsed time since self, measured against the current monotonic
clock. The contract is one-directional : self must come from
monotonic(). Returns a non-negative Duration in normal operation.
UFCS-callable as start.since().
Parameters
| self | Monotonic | No description. |
Returns
format :: fn(self: Duration) -> string Format a Duration in the most readable unit (ns, µs, ms, or s)
such that the integer part is non-empty and < 1000. Picks the largest
unit that fits. Negative durations are formatted with a leading -.
Parameters
| self | Duration | the duration to render. |
Returns
string Example
nanos(500).format() // "500 ns"
nanos(1500).format() // "1 µs", truncated
millis(42).format() // "42 ms"
seconds(-3).format() // "-3 s"format_iso8601 :: fn(self: Instant) -> string Format an Instant as an ISO 8601 timestamp in UTC with millisecond
precision, e.g. "2026-05-15T12:34:56.789Z". Years before 0001 or
after 9999 are still formatted but the year field widens beyond four
digits (no truncation).
Sub-millisecond nanosecond detail is intentionally dropped: the MVP host clock only provides millisecond resolution for the wall clock, and emitting trailing zero-noise would mislead the reader.
Parameters
| self | Instant | No description. |
Returns
string parse_iso8601 :: fn(s: string) -> Instant | TimeError Parse an ISO 8601 wall-clock timestamp into an Instant. The
accepted shape is YYYY-MM-DDTHH:MM:SS[.fff]Z (UTC only ; the
trailing Z is required, fractional seconds are optional and
truncated to milliseconds when present). The mirror of
format_iso8601.
Parameters
| s | string | the timestamp to parse. |
Errors
TimeErrorwhensdoesn't fit the accepted shape.