1300 Clock
A Clock wraps the guest's wall clock. Operations dispatch to the agent, which
adjusts the kernel's CLOCK_REALTIME (and equivalents) under the hood.
The clock is signed, you can move the guest backwards in time. This matters for testing time-dependent code: certificate expiry, timeout handling, leap-second handling, retry backoff.
1300.1 Constructing #
| Source | Returns |
|---|---|
vm:clock() | The guest's clock. |
1300.2 Methods #
1300.2.1 clock:get() #
Returns the current time as seconds-since-epoch (float). For sub-microsecond
accuracy use :get_ns().
local t = vm::
-- t ≈ 1700000000.0
1300.2.2 clock:get_ns() #
Returns nanoseconds-since-epoch as a 64-bit signed integer. Lua 5.4 integers are 64-bit so no precision is lost.
1300.2.3 clock:set(seconds) #
Set the wall clock to seconds seconds since epoch. Accepts integers or
floats. Negative values are allowed, the guest goes pre-epoch, useful for
testing time_t boundary handling.
Errors on NaN or infinity
(clock:set: value must be a finite number (got NaN/inf)). Without this
guard, the integer cast would silently saturate or round to zero.
1300.2.4 clock:set_ns(ns) #
Set the wall clock to ns nanoseconds since epoch (i64). Same semantics as
:set but at full precision.
1300.2.5 clock:sleep(duration) #
Sleep for duration seconds. Accepts:
- Integer / float, seconds.
- String,
"100ms"/"5s"/"5m"/"2h".
Errors on NaN, infinity, or negative.
1300.2.6 clock:advance(duration) #
Advance the wall clock by duration seconds (signed). Unlike :sleep, this
does not block, the kernel's CLOCK_REALTIME jumps forward (or backward).
Negative values are explicitly allowed (clock:advance(-3600) rolls the guest
back an hour). Errors on NaN or infinity but not on negative.
1300.3 Example: certificate expiry test #
test
1300.4 Notes #
clock:get()(float) loses precision below ~250 ns at modern epoch values because of f64 mantissa width. Use:get_ns()if you need exact ns roundtrip.- The clock is not a guest namespace, every process inside the guest sees the
same time axis after
:set/:advance. - Boot-time initial clock is set through
boot_opts.initial_time(see VM boot opts). The Clock methods are for adjustment after boot.
1300.5 See also #
- VM:
vm:clock()and theboot_opts.initial_timeboot-time setter.