Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

clock

Wall and monotonic clocks, pushed once a second until configure sets the interval.

mantle.clock:configure({ interval = 60 }) -- the text below shows no seconds

text {
    content = mantle.clock:map(function(clock)
        return os.date("%a %H:%M", clock.time)
    end, ""),
}

State

mantle.clock:get() returns ClockState, nil before the first push. A field marked ? may be absent.

mantle.clock’s payload, pushed on each tick of interval.

FieldTypeDescription
monotonicnumberSystem-wide CLOCK_MONOTONIC seconds since boot, as of the last push; excludes suspend. Take durations from it, since NTP moves time. mantle.clock:now() reads it live.
timeintegerUnix epoch seconds, as os.date takes them.

Actions

Call each as mantle.clock:<action>(arguments...); ? marks an argument you may omit.

ActionArgumentsDescription
configuresettings: ClockConfigureSets the push interval, 1 second until this. Each push lands on a wall-clock multiple of it, and one lands at once.

ClockConfigure

clock:configure’s table. An absent interval keeps the current one.

FieldTypeDescription
interval?integerSeconds between pushes, each on a multiple of it since the epoch, so 60 lands on every minute; 1 is the default and 0 stops them.

Reading monotonic time now

monotonic is CLOCK_MONOTONIC: seconds since boot as a number with a fraction, the same clock in every process, and it excludes suspend. The field holds the reading from the last push, up to interval stale. mantle.clock:now() reads the clock at that call, works before the first push and sends nothing:

local armed_at = mantle.clock:now()
local function elapsed() return mantle.clock:now() - armed_at end

Backend

The Supervisor’s own clocks; nothing external. Pushes land on wall-clock multiples of interval since the epoch (default 1): 60 lands on each minute’s :00; 3600 on UTC hours, not local ones in a half-hour timezone. A push due during suspend lands on resume, and a clock step (NTP, settimeofday) pushes at once and re-aligns. 0 stops pushes; the time and monotonic fields keep their last values, but now() stays live. The interval lives in the Supervisor, so it outlasts reloads until the next configure, and every reader shares it.

See also: Clock bar recipe.

Source: supervisor/src/capabilities/clock/