Module
std.time — monotonic instants, signed durations, and one wall-clock rendering, local civil time and calendar formatting. Stability tier: core. local.lu documents the Fs/Env zone reader and pure Local arithmetic.
**Capability: Clock (I13).** Every function that reads a clock or sleeps is tagged Clock in the toolchain's sandbox table and is categorically refused at comptime (D33), for the reason X12 gives: *determinism — two identical builds must not observe different times*. The refusal is E0701 and tests/time/comptime_refuses.lu holds it, one level down at the builtin, because a comptime fn cannot call across a module boundary at this pin (F-0051).
Seven of this module's functions reach the clock (now, monotonic_ns, unix_ms, sleep, elapsed, now_iso8601, unix_iso8601); the other eighteen are PURE and comptime-safe — arithmetic over two one-field structs and one calendar rendering. §14 says to state the exception explicitly, because in a capability module the pure function is the surprising one, so every doc below says which it is.
The two clocks are different clocks and the types say so. now() reads a MONOTONIC counter from an arbitrary process-local anchor: its values compare and subtract, and they are never dates. unix_ms() reads the WALL clock — milliseconds since 1970-01-01T00:00:00Z — which can jump backwards when the host adjusts it, and is the only reading with a calendar meaning. So to_iso8601 takes an int of wall milliseconds and there is deliberately no way to render an Instant: rendering a monotonic counter as a date is the mistake this split exists to make unspellable.
**Instant and Duration are the facade over an int ABI.** The builtin tier is three calls that speak in bare milliseconds (time_now_ms, time_unix_ms, time_sleep_ms); wrapping them in two one-field structs is the whole point of the module, because it is what turns unit confusion into a compile error — sleep(millis(2)) and sleep(seconds(2)) cannot be mistyped for each other, and elapsed(t) cannot be added to a count of bytes.
A Duration is a SIGNED millisecond count. between(a, b) where b precedes a is negative rather than clamped or trapped: the reversal is the caller's information, not this module's to discard.
Neither struct has a private field, because this pin has none (sc07's fs.File lesson): a caller can write time.Instant { ms: 0 } and read t.ms directly. Nothing here traps on a forged value — a nonsense Instant produces a nonsense Duration, which is arithmetic and not a fault. Read the millisecond count through as_millis anyway; the field is the representation and the function is the API.
ISO formatting is render-only and UTC-only — the ISO renderer is independent of the local-time reader. to_iso8601 writes exactly YYYY-MM-DDTHH:MM:SS.sssZ: the RFC 3339 profile of ISO 8601, always three fractional digits, always the Z zone. What this module does NOT have, deliberately:
- no parsing. Text to time is the inverse direction and it needs an error taxonomy (which field was wrong, at which offset) that the payload conventions of §12 can express and no caller can yet branch on. parse_iso8601 is a reviewed contract below. - local time is separate. local, local_in, offset_at, Local, to_unix and format are in this module's local.lu; they read the host's TZif database, not compiler-carried tables. The ISO renderer remains pure UTC arithmetic and never reads that database. - no calendar arithmetic. "One month later" is a policy, not a function; days, hours, minutes and seconds are exact and are all this module adds. - no leap seconds. Unix milliseconds do not count them and neither does this rendering; a UTC minute here is always 60 seconds long.
Years outside 0..9999 are a documented deviation rather than a refusal: they render with their own digit count and a leading - when negative, which is not ISO 8601's expanded-year form (that form needs an agreed digit width between writer and reader, and there is nobody to agree with here). Inside 0..9999 — every timestamp a program is likely to hold — the output is exactly RFC 3339.
The lanes are unequal and the reason is the builtin tier. The checked tier and the native rung both execute time_*; lupin 0.1.6 has no time builtins at all, so it refuses them at resolve. It resolves module bodies LAZILY, so a program that touches only the pure members runs there too — which is why tests/time/duration_algebra.lu and tests/time/iso8601_render.lu have three lanes and tests/time/monotonic.lu has two.
Reviewed contracts, blocked or unscheduled, landing verbatim when they are not:
parse_iso8601(s: str) -> int ! {parse}
The inverse of `to_iso8601`: wall milliseconds from an RFC 3339
text with a `Z` zone. Writable at these pins over `str.get` and
`fmt.parse_int` — unwritten rather than unwritable, and waiting
on the payload question: a caller wants to know WHICH field was
malformed and at which offset, which is a `Parse(ParseErr)`
payload (§12) that no `else` handler can destructure while
`E0806` stands (F-0043's unmoved half).
deadline(d: Duration) -> Deadline / with_timeout(...)
A bound on how long an operation may take. Blocked at the tier
below: no builtin arms a deadline anywhere in the toolchain
(F-0049 for sockets; the same hole for everything else), and
there is no `select` to race a timer against work. std ships no
deadline type rather than one that cannot expire.
unix_ns()
The WALL clock at nanoseconds. The builtin ABI is milliseconds
for that clock and a library cannot invent precision it was not
given; a `_ns` face over a `_ms` source would report a
thousand-fold lie. (`monotonic_ns()`, reviewed here beside it,
landed verbatim at sc69 over wolf 0.2.34's `os_clock_ns`.)
std.time — local civil time and C-locale calendar formatting. Stability tier: core.
local, local_in and offset_at read the host zone database: capabilities Fs and Env, refused at comptime (D33) for determinism and confinement. A file read is the capability, not calendar arithmetic. to_unix and format are pure, comptime-safe (cross-module CTFE is still F-0051); they observe neither a clock, a file nor the environment.
RFC 8536 TZif v1/v2/v3 (64-bit table when present) and explicit POSIX TZ strings are read in wolf. No database is bundled in the library. $TZDIR or /usr/share/zoneinfo supplies named files; $TZ, a colon path or an absolute path selects one. Unset $TZ reads /etc/localtime; empty $TZ is UTC. Unreadable or malformed selections report zone or io, never a silent change of zone. With leap records (the right/ tree), the integer input is UNIX leap time (RFC 8536 section 2), as with the host date command; otherwise it is POSIX UNIX time. No implicit conversion between those two timestamp scales is performed.
Bare database names contain only ASCII letters, digits, _ . + - and /; a complete POSIX rule is parsed before this restriction. Malformed bare names answer zone before any file I/O. Colon and absolute paths (including drive-absolute paths) use the host's path syntax and retain the io row for file failures.
The Gregorian arithmetic and POSIX/TZif design are ported from the maintainer's aspen src/clock/clock.lu, commit 476edee3bb0ddbf2afef0681ec74fdd211d4d3da, lines 19–405, 407–666; validation and rows are std's. RFC 8536 §§3.1–3.3 and POSIX TZ/strftime specify the input. No foreign implementation's source was read. Lord Howe's DST is 1800 seconds: no one-hour correction is applied to a transition. DST strings require explicit change rules; no host's undocumented default US rule is guessed here.
Local.offset_s is seconds EAST of UTC. to_unix trusts it: the two occurrences of an overlap retain their distinct offsets and round-trip. A caller-created nonexistent gap time is interpreted at the supplied offset, not normalized or assigned an arbitrary earlier/later rule. nanos is 0 for a seconds input and is ignored by to_unix/format. local/local_in apply leap corrections; offset_at returns only the UT offset. Local.leap_correction_s retains the net correction so to_unix and format %s invert even a leap second. Literal-created ordinary locals must set it to zero. No locale state or timezone cache; each lookup reads anew.
Functions and methods
fn · Source line 148
pub fn now() -> Instant
The current reading of the monotonic clock.
Total: no rows, no traps. Successive calls are non-decreasing within one process; the value has no meaning outside comparison and subtraction (see the module header's two-clocks note).
Capability Clock; refused at comptime (D33). Delegates to time_now_ms.
let a = time.now()
let b = time.now()
time.as_millis(time.between(a, b)) >= 0
fn · Source line 173
pub fn monotonic_ns() -> int
**s248 ([os.clock.ns]): the monotonic clock in NANOSECONDS**, from the anchor now reads (time_now_ms's): monotonic_ns() / 1000000 read after a now() is never smaller than its ms. A value is a duration to compare and subtract, never a timestamp; it never goes backwards and saturates at the largest int, 292 years from the anchor. What a latency gate needs (a keypress to its paint, at most 5 ms): an interval of 100 us that now reads as 0 reads here as about 100,000. The host's finest monotonic clock (CLOCK_MONOTONIC, the mach clock, QueryPerformanceCounter).
Total: no rows, no traps. Capability Clock; refused at comptime (D33), charged nothing in a manifest. lupin serves it from 0.1.56 (0.1.55 did not resolve the call, wolf-interp#268). Delegates to os_clock_ns.
let t = time.now()
let a = time.monotonic_ns()
let b = time.monotonic_ns()
b >= a && a / 1000000 >= t.ms
fn · Source line 190
pub fn unix_ms() -> int
The current wall clock, in milliseconds since 1970-01-01T00:00:00Z.
Total: no rows, no traps. Unlike now, this reading has a calendar meaning and can move BACKWARDS when the host adjusts its clock, so it is the wrong thing to measure an elapsed interval with. Use now and elapsed for durations and this for timestamps.
Capability Clock; refused at comptime (D33). Delegates to time_unix_ms.
time.unix_ms() > 1577836800000
fn · Source line 218
pub fn sleep(d: Duration)
Block the calling thread for d.
A non-positive d returns immediately. The sleep is a FLOOR, not a promise: the host may wake the thread later than asked and never earlier, so a program that must not overshoot measures with elapsed rather than trusting the argument.
Total: no rows, no traps. This blocks the whole thread, and as of the sc11 pin that sentence has teeth on every lane: s73 lowers tasks, channels and procs natively, so a wolf program can now have several tasks on all three rungs — and time_sleep_ms still parks the OS thread rather than the task, so sleeping inside one task stops the others that share its thread. There is no task-aware sleep at this tier. The s35 reactor has the parking story and reaches std when its builtin tier does; until then, a program that must not stall its peers waits on a channel instead of sleeping.
Capability Clock; refused at comptime (D33). Delegates to time_sleep_ms.
let before = time.now()
time.sleep(time.millis(2))
time.as_millis(time.elapsed(before)) >= 2
fn · Source line 241
pub fn elapsed(since: Instant) -> Duration
How long ago since was read, on the monotonic clock.
elapsed(t) is between(t, now()). Never negative in practice, because the clock it reads is monotonic — but it is not clamped, so a forged Instant from the future answers a negative Duration rather than a lie.
Total: no rows, no traps.
Capability Clock; refused at comptime (D33). Delegates to time_now_ms.
let start = time.now()
time.as_millis(time.elapsed(start)) >= 0
fn · Source line 260
pub fn now_iso8601() -> str
The wall clock, rendered — to_iso8601(unix_ms()).
Total: no rows, no traps. The one function here that turns a clock reading straight into text, and the only correct way to timestamp a log line: to_iso8601(as_millis(elapsed(t))) would render a duration as a date, which the types cannot stop because both are int at the boundary and this doc is where that is said.
Capability Clock; refused at comptime (D33). Delegates to time_unix_ms.
time.now_iso8601().len == 24
time.now_iso8601().ends_with("Z") == true
fn · Source line 270
pub fn millis(n: int) -> Duration
A Duration of n milliseconds. Pure and comptime-safe.
time.as_millis(time.millis(250)) == 250
time.as_millis(time.millis(-1)) == -1
fn · Source line 284
pub fn seconds(n: int) -> Duration
A Duration of n seconds. Pure and comptime-safe.
Traps overflow (X3, checked arithmetic in every profile) when n * 1000 leaves int — a span that long is a bug in the caller's data model, not an outcome to hand back as a row (§10's trap-versus- raise rule).
time.as_millis(time.seconds(3)) == 3000
fn · Source line 294
pub fn minutes(n: int) -> Duration
A Duration of n minutes. Pure and comptime-safe; the overflow posture is seconds'.
time.as_millis(time.minutes(2)) == 120000
fn · Source line 304
pub fn hours(n: int) -> Duration
A Duration of n hours. Pure and comptime-safe; the overflow posture is seconds'.
time.as_millis(time.hours(1)) == 3600000
fn · Source line 316
pub fn as_millis(d: Duration) -> int
The millisecond count d names. Pure and comptime-safe.
The exact representation, signed, with nothing rounded — the as_ form because it borrows and allocates nothing (§1).
time.as_millis(time.seconds(2)) == 2000
fn · Source line 331
pub fn as_seconds(d: Duration) -> int
d in whole seconds, truncated TOWARD ZERO. Pure and comptime-safe.
Truncation rather than rounding, and toward zero rather than down, so that as_seconds(d) and as_seconds(negate(d)) are mirror images: 1999ms is 1 second and -1999ms is -1.
time.as_seconds(time.millis(1999)) == 1
time.as_seconds(time.millis(-1999)) == -1
time.as_seconds(time.hours(1)) == 3600
fn · Source line 340
pub fn as_minutes(d: Duration) -> int
d in whole minutes, truncated toward zero. Pure and comptime-safe.
time.as_minutes(time.seconds(119)) == 1
fn · Source line 350
pub fn as_hours(d: Duration) -> int
d in whole hours, truncated toward zero. Pure and comptime-safe.
time.as_hours(time.minutes(59)) == 0
time.as_hours(time.minutes(60)) == 1
fn · Source line 367
pub fn between(earlier: Instant, later: Instant) -> Duration
The span from earlier to later, signed. Pure and comptime-safe.
Negative when the arguments are the other way round, per the module header. Traps overflow (X3) when the two instants are further apart than int can express, which cannot happen to two readings of one process's clock.
let a = time.Instant { ms: 10 }
let b = time.Instant { ms: 25 }
time.as_millis(time.between(a, b)) == 15
time.as_millis(time.between(b, a)) == -15
fn · Source line 380
pub fn add(t: Instant, d: Duration) -> Instant
t advanced by d (which may be negative). Pure and comptime-safe.
Traps overflow (X3) when the result leaves int.
let t = time.Instant { ms: 100 }
let u = time.add(t, time.millis(50))
time.as_millis(time.between(t, u)) == 50
fn · Source line 394
pub fn is_before(a: Instant, b: Instant) -> bool
Whether a is strictly before b on the monotonic clock. Pure and comptime-safe.
let a = time.Instant { ms: 1 }
let b = time.Instant { ms: 2 }
time.is_before(a, b) == true
time.is_before(b, a) == false
time.is_before(a, a) == false
fn · Source line 406
pub fn is_after(a: Instant, b: Instant) -> bool
Whether a is strictly after b. Pure and comptime-safe.
let a = time.Instant { ms: 3 }
let b = time.Instant { ms: 2 }
time.is_after(a, b) == true
time.is_after(b, b) == false
fn · Source line 416
pub fn plus(a: Duration, b: Duration) -> Duration
a + b. Pure and comptime-safe; traps overflow (X3) on a sum outside int.
time.as_millis(time.plus(time.seconds(1), time.millis(500))) == 1500
fn · Source line 426
pub fn minus(a: Duration, b: Duration) -> Duration
a - b. Pure and comptime-safe; traps overflow (X3) on a difference outside int.
time.as_millis(time.minus(time.seconds(1), time.millis(250))) == 750
fn · Source line 439
pub fn negate(d: Duration) -> Duration
d with its sign flipped. Pure and comptime-safe.
Traps overflow (X3) on the one input that has no negation: the most negative int.
time.as_millis(time.negate(time.millis(5))) == -5
time.as_millis(time.negate(time.millis(-5))) == 5
fn · Source line 449
pub fn is_zero(d: Duration) -> bool
Whether d is the zero span. Pure and comptime-safe.
time.is_zero(time.millis(0)) == true
time.is_zero(time.millis(1)) == false
fn · Source line 459
pub fn is_negative(d: Duration) -> bool
Whether d runs backwards. Pure and comptime-safe.
time.is_negative(time.millis(-1)) == true
time.is_negative(time.millis(0)) == false
fn · Source line 492
pub fn to_iso8601(unix_millis: int) -> str
unix_millis rendered as YYYY-MM-DDTHH:MM:SS.sssZ — the RFC 3339 profile of ISO 8601, in UTC, with exactly three fractional digits.
Pure and comptime-safe: it reads no clock, so the same argument always gives the same text on every host, and this is the function a test pins. The argument is WALL milliseconds — unix_ms(), or a timestamp a program received as data — never an Instant (the module header explains why there is no way to render one).
Total: no rows, no traps, defined for every int. Negative arguments are pre-1970 dates and render correctly (-1 is 1969-12-31T23:59:59.999Z); the proleptic Gregorian calendar runs backwards without a switchover, which is what every other implementation of this format does. Years outside 0..9999 render with their own digit count and a leading - when negative — a documented deviation from ISO 8601's expanded-year form, per the module header.
No leap seconds: a minute here is always 60 seconds, as Unix time itself defines it.
time.to_iso8601(0) == "1970-01-01T00:00:00.000Z"
time.to_iso8601(1000) == "1970-01-01T00:00:01.000Z"
time.to_iso8601(86399999) == "1970-01-01T23:59:59.999Z"
time.to_iso8601(-1) == "1969-12-31T23:59:59.999Z"
time.to_iso8601(1577836800000) == "2020-01-01T00:00:00.000Z"
time.to_iso8601(951782400000) == "2000-02-29T00:00:00.000Z"
fn · Source line 521
pub fn unix_iso8601() -> str
unix_ms() rendered — see to_iso8601, which does the work.
Capability Clock; refused at comptime (D33). Delegates to time_unix_ms. Kept beside now_iso8601 because the two read different clocks in every other language and the same one here: this is the honest name, and now_iso8601 is the one a caller reaches for.
time.unix_iso8601().starts_with("2") == true
fn · Source line 764
pub fn local(unix_s: int) -> Local ! {zone, io}
Local time at timestamp unix_s, from $TZ or /etc/localtime. The scale is POSIX UNIX time without leap records, UNIX leap time with them; see the module contract. An empty setting is UTC. Rows: zone for missing or invalid zone data; io for a refused/failed host access or unreadable environment value. Capability Fs/Env, refused at comptime (D33, determinism and confinement). Arithmetic overflow traps overflow (X3) for an extreme second plus offset outside int.
env_set("TZ", "UTC0")?
time.local(0)?.nanos == 0
fn · Source line 794
pub fn local_in(zone: str, unix_s: int) -> Local ! {zone, io}
Local time at unix_s: POSIX UNIX time without leap records, UNIX leap time with them (the same integer interpretation as host date). A named zone, an absolute path, :path (relative paths use forward slashes), or an explicit POSIX TZ string. Named files use $TZDIR or /usr/share/zoneinfo; an empty zone is UTC. Rows: zone for missing/malformed data, a malformed TZ rule, or a bare database name outside ASCII letters, digits, _ . + - and /. Complete bare POSIX rules are parsed first. Colon/absolute paths are explicit host file operations; drive-absolute paths are recognized before name validation. io reports host access failure. Capability Fs/Env, refused at comptime (D33, determinism and confinement). Extreme arithmetic traps overflow (X3).
let l = time.local_in("UTC0", 0)?
l.year == 1970
l.offset_s == 0
fn · Source line 809
pub fn offset_at(zone: str, unix_s: int) -> int ! {zone, io}
Seconds east of UT in force in zone at timestamp unix_s. Does not include the leap correction; see local_in for civil fields. Leap-aware files interpret the input as UNIX leap time. Rows: zone for missing/malformed zone data; io for host access failure. Same zone spellings as local_in. Capability Fs/Env, refused at comptime (D33, determinism and confinement); extreme POSIX-rule arithmetic traps overflow (X3).
time.offset_at("<+0530>-5:30", 0)? == 19800
fn · Source line 843
pub fn to_unix(l: Local) -> int
Timestamp of calendar fields at their EXPLICIT offset_s and leap_correction_s: UNIX leap time for leap-aware locals, POSIX UNIX time otherwise. Like the explicit offset, a caller editing civil fields supplies the correction appropriate to the intended instant; use zero for literal-created locals from ordinary zones. Pure, comptime-safe. Nanos are ignored, not rounded. Both occurrences in a DST fold invert exactly. A caller-created gap is interpreted at its supplied offset, not normalized. is_dst/abbrev are descriptive and do not override the offset. Traps assert for invalid calendar fields and overflow when checked arithmetic leaves int (X3).
let l = time.Local { year: 1970, month: 1, day: 1, hour: 1, minute: 0, second: 0, nanos: 0, offset_s: 3600, leap_correction_s: 0, is_dst: false, abbrev: "X" }
time.to_unix(l) == 0
fn · Source line 966
pub fn format(l: Local, fmt: str) -> str ! {format}
C-locale POSIX calendar formatting, pure and comptime-safe. Conversions: %Y %m %d %H %M %S %e %b %B %a %A %j %Z %z %p %I %y %C %u %w %F %T %D %R %s %n %t %% and C-locale %c %x %X. %- removes padding, %_ selects spaces, %0 selects zeros; the last flag wins. Decimal widths count bytes (§11) and are bounded at 4096 to refuse an unbounded allocation. Numeric padding preserves a sign. %z truncates offset seconds to minutes, as POSIX specifies; %s uses to_unix's explicit offset. Nanos are not rendered. Rows: format for unknown/incomplete conversions, widths above 4096, or invalid calendar fields. Checked arithmetic can trap overflow (X3) for extreme forged fields/offsets. No locale or host access.
let l = time.Local { year: 2024, month: 2, day: 3, hour: 13, minute: 4, second: 5, nanos: 0, offset_s: 19800, leap_correction_s: 0, is_dst: false, abbrev: "IST" }
time.format(l, "%F %T %z")? == "2024-02-03 13:04:05 +0530"
time.format(l, "%-d|%_d|%0e")? == "3| 3|03"