Skip to main content

Crate clock_lib

Crate clock_lib 

Source
Expand description

§clock-lib

TIME READINGS FOR RUST

Monotonic and wall-clock time readings with a mockable clock for deterministic testing. Simple Tier-1 API, zero dependencies, no unsafe.

§Two kinds of time

There are two fundamentally different kinds of time, and conflating them is a common source of bugs:

  • Monotonic time (now) never goes backwards. Use it to measure elapsed time: rate limiting, timeouts, benchmarks. Only meaningful as a delta.
  • Wall-clock time (wall, unix) is calendar time. It can jump (NTP, DST, manual changes). Use it for timestamps and logging, never for measuring elapsed time.

The two are returned as distinct types — Monotonic and Wall — so the compiler rejects any attempt to subtract one from the other. That separation is the central design choice of the crate.

§Tier-1 API (the lazy path)

use clock_lib as clock;

let start = clock::now();              // monotonic reading
// ... do work ...
let took = clock::elapsed(start);      // Duration since `start`

let secs = clock::unix();              // unix seconds (like PHP time())

§Tier-2 API (the mockable clock)

The real value is deterministic time in tests. A future release introduces a Clock trait with SystemClock (production) and ManualClock (test/sim) so that systems can advance time instantly without ever calling sleep.

§License

Dual-licensed under Apache-2.0 OR MIT.

Structs§

Monotonicstd
A captured monotonic instant.
Wallstd
A captured wall-clock instant.

Constants§

VERSION
Crate version string, populated by Cargo at build time.

Functions§

elapsedstd
Returns the Duration elapsed since earlier.
nowstd
Captures the current monotonic time.
unixstd
Returns the current Unix time in whole seconds.
unix_msstd
Returns the current Unix time in whole milliseconds.
unix_nsstd
Returns the current Unix time in whole nanoseconds.
wallstd
Captures the current wall-clock time.