Skip to content

std.time - ZZ

Clocks, sleeps, and calendar dates. Dates are {str: int} dicts (year, month, day, hour, min, sec). Parse/format are UTC RFC3339 (...Z) only; invalid input falls back to epoch so callers stay total.

import std.time

std.time provides wall-clock and monotonic time, sleeps (cooperative — yield green threads), and calendar-date math on dict-based dates. All parse/format ops are UTC RFC3339; invalid input falls back to epoch (total).

import std.time
start := time.now_ms()
time.sleep_ms(1000)
elapsed := time.now_ms() - start
println("slept {elapsed}ms")
d := time.parse_rfc3339("2024-02-29T12:30:45Z")
println(time.format_rfc3339(d)) // 2024-02-29T12:30:45Z
println(time.add_days(d, 1)["day"])
println(time.diff_days(d, time.make_date(2024, 2, 28, 0, 0, 0))) // 1

Spans as integer microseconds:

time.micros(1_000_000) // 1 second in micros
time.millis(1000) // 1000 ms in micros
time.secs(1) // 1 sec in micros
d := time.millis(1000) // duration span
time.to_micros(d) // convert duration span to micros
time.to_millis(d)
time.to_secs(d)
time.to_nanos(d)
time.sleep(d) // sleep a duration span
import std.time
// Measure elapsed cooperatively
start := time.monotonic_nanos()
// ... work ...
elapsed_ms := (time.monotonic_nanos() - start) / 1_000_000
// Validate before using
d := time.parse_rfc3339("2024-02-29T12:30:45Z")
if time.date_valid(d["year"], d["month"], d["day"]) {
// safe to use d
}

28 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.time.add_days add_days(d: {str: int}, n: int) -> {str: int} Shift by N days (leap-correct)
std.time.civil_from_days civil_from_days(z: int) -> {str: int} —
std.time.date_valid date_valid(y: int, m: int, d: int) -> bool Range check
std.time.days_from_civil days_from_civil(y: int, m: int, d: int) -> int —
std.time.days_in_month days_in_month(y: int, m: int) -> int Month length
std.time.diff_days diff_days(a: {str: int}, b: {str: int}) -> int a - b in days
std.time.epoch_fallback epoch_fallback() -> {str: int} Epoch date dict
std.time.format_rfc3339 format_rfc3339(d: {str: int}) -> str Format as UTC RFC3339
std.time.from_epoch_days from_epoch_days(z: int) -> {str: int} Inverse of above
std.time.is_leap is_leap(y: int) -> bool Gregorian leap year
std.time.make_date make_date(y: int, mo: int, d: int, h: int, mi: int, se: int) -> {str: int} Build date dict
std.time.monotonic_nanos monotonic_nanos() -> int Monotonic clock
std.time.now_micros now_micros() -> int micros since epoch
std.time.now_ms now_ms() -> int ms since epoch
std.time.now_nanos now_nanos() -> int nanos since epoch
std.time.pad2 pad2(n: int) -> str —
std.time.parse_rfc3339 parse_rfc3339(s: str) -> {str: int} Parse UTC RFC3339 (epoch on invalid)
std.time.sleep_micros sleep_micros(micros: int) -> unit Sleep micros
std.time.sleep_ms sleep_ms(ms: int) -> unit Cooperative sleep (yields green threads)
std.time.to_epoch_days to_epoch_days(d: {str: int}) -> int Days since 1970-01-01
time.micros micros(us: int) -> int Duration span (micros)
time.millis millis(ms: int) -> int Duration span (millis)
time.secs secs(s: int) -> int Duration span (secs)
time.sleep sleep(d: int) -> unit Sleep a duration span
time.to_micros to_micros(d: int) -> int Convert to micros
time.to_millis to_millis(d: int) -> int Convert to millis
time.to_nanos to_nanos(d: int) -> int Convert to nanos
time.to_secs to_secs(d: int) -> int Convert to secs
  • std.math — Rounding, roots, powers, trigonometry, predicates and numeric aggregates.

Full index: API reference · Standard library