Trusted. Time
(Cure Standard Library v0.38.0)
View Source
Clock reading and sleeping, for the trusted (unproved) library.
Why this module exists
Std.Time models calendar time: Instant, Duration, ISO 8601 parsing
and formatting, and the wall clock. What it deliberately does not expose is
the raw clock surface — a monotonic reading for measuring elapsed time,
and sleep. Both are unprovable in the same way file I/O is: the value
depends on the machine, not on the program.
Monotonic vs wall clock
The distinction is load-bearing and easy to get wrong:
monotonic_us/0never goes backwards and is unaffected by NTP or a manual clock change. Use it to measure elapsed time.Std.Time.now/0is the wall clock. Use it to timestamp an event.
Measuring a duration with the wall clock produces a negative elapsed time the first time the system clock is corrected backwards.
The trust contract
This module satisfies the four rules every Trusted.* module must meet
(see docs/TRUSTED.md):
- Total surface. Every function returns a concrete type.
- Declared boundary. Every
@externcarries a## Trust:block. - No proof claims. Nothing here is a proof.
- Reviewable axiom set.
cure audit trust Trusted.Timesnapshots the reachable externs.
Examples
mod ElapsedExample
use Trusted.Time
fn measure() -> Int =
let start = monotonic_us()
let _ = sleep_ms(10)
elapsed_us(start)mod SleepExample
use Std.Io
use Trusted.Time
fn pause() -> Unit = sleep_ms(100)
Summary
Functions
Convert microseconds to whole milliseconds, rounding up.
Milliseconds elapsed since start, where start came from
monotonic_ms/0.
Microseconds elapsed since start, where start came from
monotonic_us/0.
The monotonic clock in milliseconds.
The monotonic clock in microseconds.
Clamp a duration at zero.
Trust: :erlang.monotonic_time/0 answers an integer in
implementation-defined native units. It is guaranteed monotonic (never
decreasing) but its absolute value is meaningless — only differences are.
Trust: :erlang.monotonic_time/1 answers the same reading converted to
the named unit. OTP documents :microsecond as a supported unit.
Trust: :timer.sleep/1 blocks the calling process for the given number of
milliseconds and answers ok. A negative duration is treated as zero.
Block the calling process for millis milliseconds.
Block the calling process for micros microseconds.
Functions
fn ceil_millis(micros: Int) -> Int
Convert microseconds to whole milliseconds, rounding up.
fn elapsed_ms(start: Int) -> Int
Milliseconds elapsed since start, where start came from
monotonic_ms/0.
Examples
mod ElapsedMsExample
use Trusted.Time
fn measure() -> Int =
let start = monotonic_ms()
let _ = sleep_ms(1)
elapsed_ms(start)
fn elapsed_us(start: Int) -> Int
Microseconds elapsed since start, where start came from
monotonic_us/0.
Non-negative by construction: the monotonic clock never decreases, so the
subtraction cannot underflow. This is the function that makes the
monotonic/wall-clock distinction pay off — the same measurement against
Std.Time.now/0 can produce a negative result.
Examples
mod ElapsedUsExample
use Trusted.Time
fn measure() -> Int =
let start = monotonic_us()
let _ = sleep_ms(1)
elapsed_us(start)
fn monotonic_ms() -> Int
The monotonic clock in milliseconds.
Examples
mod MonotonicMsExample
use Trusted.Time
fn start() -> Int = monotonic_ms()
fn monotonic_us() -> Int
The monotonic clock in microseconds.
Never goes backwards, and is unaffected by wall-clock corrections. Its absolute value is meaningless; subtract two readings to measure elapsed time.
Examples
mod MonotonicExample
use Trusted.Time
fn start() -> Int = monotonic_us()
fn non_negative(millis: Int) -> Int
Clamp a duration at zero.
fn raw_monotonic_time() -> Int
Trust: :erlang.monotonic_time/0 answers an integer in
implementation-defined native units. It is guaranteed monotonic (never
decreasing) but its absolute value is meaningless — only differences are.
fn raw_monotonic_time_in(unit: Atom) -> Int
Trust: :erlang.monotonic_time/1 answers the same reading converted to
the named unit. OTP documents :microsecond as a supported unit.
fn raw_sleep(millis: Int) -> Atom
Trust: :timer.sleep/1 blocks the calling process for the given number of
milliseconds and answers ok. A negative duration is treated as zero.
fn sleep_ms(millis: Int) -> Unit
Block the calling process for millis milliseconds.
A negative duration is treated as zero rather than raising: a caller that computed the delay from a deadline should not be able to abort the process by overshooting it.
This blocks the process, not the scheduler — other processes keep running.
Examples
mod SleepMsExample
use Trusted.Time
fn pause() -> Unit = sleep_ms(100)
fn sleep_us(micros: Int) -> Unit
Block the calling process for micros microseconds.
:timer.sleep/1 takes milliseconds, so a sub-millisecond request is
rounded UP to 1ms rather than truncated to 0: a caller asking for 500µs
wants a pause, and silently not pausing at all would be the more surprising
answer.
Examples
mod SleepUsExample
use Trusted.Time
fn pause() -> Unit = sleep_us(500)