@std/time

Overview

Time sources for measuring elapsed work and for tests that need a clock they control.

The module has two kinds of clock, and they answer different questions. Clock.mono() is monotonic hardware time: it always moves forward, and it is unaffected by system clock adjustments, so it is what you want for timeouts, deadlines, and profiling. Clock.virtual() is a plain Int you advance yourself: it never reads the OS clock, so the same sequence of ticks produces the same timestamps on every run.

Nothing here reports calendar time. A Duration counts nanoseconds from an epoch chosen by its source, so it can measure how long something took but cannot tell you what date it is.

.rnx
import { Clock } from "@std/time";

let clock = Clock.virtual(0);
clock.tick(2500000);
print(clock.now().toMillis());

When to use it

Reach for @std/time in two shapes: Clock.mono for real monotonic durations, and Clock.virtual plus tick() for deterministic virtual time that tests can drive step by step.

Capabilities

  • Monotonic clock
  • VirtualClock control
  • Duration math

Symbols

  • class Clock — Entry point for both clock kinds: virtual clocks you advance yourself, and
  • class Duration — A signed nanosecond count, plus conversions to other units.
  • class VirtualClock — Manually advanced clock for deterministic tests and simulations.