Nox
Download

Standard library

nox.time

Wall-clock time, sleeping, calendar date-times and monotonic interval measurement.

Text
import nox.time

Capability: none for the module; the functions that actually read the clock — now_ms, sleep_ms, now, instant_now and the Instant methods — require the clock capability and are unavailable in freestanding profiles. The calendar arithmetic (DateTime, from_epoch_ms) is pure and available everywhere.

Functions#

Function Description
now_ms() milliseconds since the Unix epoch (wall clock; can jump if the system clock is changed)
boot_ms() a monotonic millisecond counter that includes time the device spent suspended; only differences are meaningful
now_ns() Unix time in nanoseconds (wall clock; resolution is platform dependent: about 1 ns on Linux/macOS, 100 ns on Windows, 1 ms in WebAssembly)
monotonic_ns() a nanosecond monotonic counter for measuring durations (only differences are meaningful; it never goes backwards)
sleep_ms(ms) waits ms milliseconds: inside a task it suspends only that task (other tasks keep running), elsewhere it blocks the thread. sleep_ms(0) inside a task is a short (about 1 ms) cooperative sleep so other tasks and timers can run
now() the current time as a DateTime (UTC)
from_epoch_ms(ms) converts epoch milliseconds to a DateTime (UTC)
instant_now() a monotonic Instant for measuring intervals (see "Clocks and device sleep")
pad2(n) formats n with at least two digits (7 → "07")

Clocks and device sleep#

Clock Source While the device is suspended
now_ms() wall clock (CLOCK_REALTIME) keeps the real time of day; jumps if the system clock is changed
instant_now() / Instant CLOCK_MONOTONIC Linux and Android: not counted. macOS and iOS: counted (mach_continuous_time)
boot_ms() CLOCK_BOOTTIME (Linux/Android), CLOCK_MONOTONIC (Darwin) counted on every platform

The monotonic Instant is therefore awake time on Android and elapsed time on iOS. When you need the same answer on both — for example "how long ago did the user last open the app", a cache lifetime or a timeout that must expire while the phone sleeps — use boot_ms() differences (or the wall clock for calendar time). Suspending only the process (the app moving to the background and being frozen) does not stop any of these clocks: a 4-second sleep that is frozen for 3 seconds still reports about 4 seconds on all three. Real device suspend cannot be reproduced in an emulator or simulator, so the table above is the documented platform behaviour, not a measurement.

DateTime#

A calendar date and time (UTC): fields year, month, day, hour, minute, second.

Member Description
DateTime(year, month, day, hour, minute, second) constructor
to_str() "YYYY-MM-DD HH:MM:SS"
to_epoch_ms() back to epoch milliseconds (a proleptic-Gregorian civil-date algorithm, exact for all dates)
Nox
import nox.time

dt: nox.time.DateTime = nox.time.from_epoch_ms(1700000000000)
print(dt.to_str(), dt.year, dt.month, dt.day, dt.to_epoch_ms())
made: nox.time.DateTime = nox.time.DateTime(2024, 2, 29, 12, 30, 5)
print(made.to_str(), made.to_epoch_ms() > 0, nox.time.pad2(7))
Output
2023-11-14 22:13:20 2023 11 14 1700000000000
2024-02-29 12:30:05 True 07

Instant and Duration#

Instant reads a monotonic clock — it never goes backwards — and is the right tool for measuring how long something took (prefer it to subtracting two now_ms() values).

Member Description
instant_now() captures the current monotonic time
Instant.elapsed_ms() milliseconds since the instant was captured
Instant.elapsed() the same as a Duration
Duration(ms), Duration.as_ms() a length of time in milliseconds
Nox
import nox.time

start: nox.time.Instant = nox.time.instant_now()
nox.time.sleep_ms(30)
took: nox.time.Duration = start.elapsed()
print(took.as_ms() >= 25, nox.time.now_ms() > 1700000000000)
Output
True True

Notes#

  • sleep_ms inside a task suspends only that task on both backends; outside a task (top-level code of a plain thread, Windows) it blocks the thread (Concurrency).
  • There is no time-zone support: DateTime is UTC. Format with nox.time.pad2 or f-string format specs.