Library

std/time

Wall-clock and monotonic time, plus arithmetic and ISO 8601.

2 files lib/std/time/stopwatch.vader, lib/std/time/time.vader 23 exports

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.

Implements Add<Duration, Instant> add :: fn(self, other: Duration) -> Instant
Implements Sub<Instant, Duration> sub :: fn(self, other: Instant) -> Duration
Implements Display to_string :: fn(self) -> string
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.

Implements Add<Duration, Monotonic> add :: fn(self, other: Duration) -> Monotonic
Duration :: i64

Signed time interval with nanosecond precision. May be negative (e.g. b - a when b < a). Range : ±~292 years (i64 ns).

Implements Add add :: fn(self, other: Duration) -> Duration
Implements Sub sub :: fn(self, other: Duration) -> Duration
TimeError :: struct

Error returned by parse_iso8601 when the input doesn't conform to the supported subset of ISO 8601.

Fields

msg: string
No description.
Implements Error message :: fn(self) -> string

Functions

start :: fn() -> StopWatch

Start a stopwatch from the current monotonic instant.

Returns

StopWatch a StopWatch anchored at monotonic().
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

selfStopWatch the running stopwatch.

Returns

Duration a non-negative Duration in normal operation.
nanos :: fn(n: i64) -> Duration

Construct a Duration from a count of nanoseconds.

Parameters

ni64 No description.

Returns

micros :: fn(n: i64) -> Duration

Construct a Duration from a count of microseconds.

Parameters

ni64 No description.

Returns

millis :: fn(n: i64) -> Duration

Construct a Duration from a count of milliseconds.

Parameters

ni64 No description.

Returns

seconds :: fn(n: i64) -> Duration

Construct a Duration from a count of seconds.

Parameters

ni64 No description.

Returns

now :: fn() -> Instant

Current wall-clock time. Reads the host clock: successive calls may observe non-monotonic jumps (NTP, manual adjustments).

Returns

monotonic :: fn() -> Monotonic

Current monotonic instant. Never decreases across calls within a single process.

Returns

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

nsi64 nanoseconds since the Unix epoch.

Returns

to_unix_nanos :: fn(self: Instant) -> i64

Unix-epoch nanoseconds for this Instant (since 1970-01-01 UTC ; negative for pre-epoch). The inverse of from_unix_nanos.

Parameters

selfInstant the instant to read.

Returns

i64
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

selfMonotonic No description.

Returns

to_nanos :: fn(self: Duration) -> i64

Whole nanoseconds.

Parameters

selfDuration No description.

Returns

i64
to_micros :: fn(self: Duration) -> i64

Whole microseconds (truncated towards zero).

Parameters

selfDuration No description.

Returns

i64
to_millis :: fn(self: Duration) -> i64

Whole milliseconds (truncated towards zero).

Parameters

selfDuration No description.

Returns

i64
to_seconds :: fn(self: Duration) -> i64

Whole seconds (truncated towards zero).

Parameters

selfDuration No description.

Returns

i64
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

selfDuration 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

selfInstant 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

sstring the timestamp to parse.

Returns

Instant | TimeError the parsed Instant.

Errors

  • TimeError when s doesn't fit the accepted shape.
Written by vader doc. Icons: Codicons, CC BY 4.0. Fonts: Atkinson Hyperlegible, JetBrains Mono, OFL.