Files
sx/library/modules/std/time.sx
agra 59f90d2939 refactor(ffi-linkage): Phase 6.3 — migrate std/ #foreign→extern
Pure source rename across 11 std modules (~60 sites): cli/core/fmt/fs/log/
net/kqueue/process/socket/thread/time/trace. All fn-decl markers — bare
'#foreign;', '#foreign libc;'/'#foreign tlib;' (LIB ref), and
'#foreign libc "csym";' (LIB+rename) → the same 'extern …' tail (extern carries
the identical [LIB] ["csym"] axis). Plus 2 stale comment mentions (fmt/fs).
No class forms in std. These modules ARE host-corpus-exercised, so the empty
snapshot diff is direct validation. Suite green (647 corpus / 444 unit, 0
failed).
2026-06-15 04:35:52 +03:00

45 lines
1.4 KiB
Plaintext

// std.time — wall and monotonic clocks over clock_gettime(2)
// (PLAN-HTTPZ S1).
//
// now_secs() wall clock, unix epoch seconds (CLOCK_REALTIME).
// mono_ms() monotonic milliseconds for deadlines/timeouts —
// unaffected by wall-clock steps (NTP, manual set);
// meaningful only as a difference between two reads in
// one process, never as an absolute time.
//
// PER-OS: clock ids differ (CLOCK_MONOTONIC is 6 on darwin, 1 on
// linux); the constants below are darwin's. Per-OS selection is
// PLAN-HTTPZ C3 — until it lands this module is correct on the gate's
// host (darwin) only.
//
// No error channel: clock_gettime fails only on an unknown clock id
// (ours are module constants) or a bad pointer (ours is a stack slot),
// so the wrappers are total.
libc :: #library "c";
// darwin struct timespec: tv_sec and tv_nsec are both C long (i64).
Timespec :: struct {
sec: i64;
nsec: i64;
}
clock_gettime :: (clock_id: i32, ts: *Timespec) -> i32 extern libc;
CLOCK_REALTIME :i32: 0;
CLOCK_MONOTONIC :i32: 6; // darwin; linux is 1 (C3 selects per-OS)
// Wall clock as unix epoch seconds.
now_secs :: () -> i64 {
ts : Timespec = .{};
clock_gettime(CLOCK_REALTIME, @ts);
return ts.sec;
}
// Monotonic milliseconds since an arbitrary process-local origin.
mono_ms :: () -> i64 {
ts : Timespec = .{};
clock_gettime(CLOCK_MONOTONIC, @ts);
return ts.sec * 1000 + ts.nsec / 1000000;
}