Features
Two Kinds of Time, Clearly Separated
- Monotonic readings — for measuring elapsed time; never goes backwards
- Wall-clock readings — for timestamps and calendar time
- Type-level separation — the API makes it impossible to subtract a wall-clock time from a monotonic one
Mockable Clock (the killer feature)
SystemClock— reads the OS in productionManualClock— advance time instantly in tests, fully deterministicClocktrait — inject time into any system for testability
Simple Tier-1 API
now()— monotonic readingelapsed(earlier)— duration since a monotonic readingwall()— wall-clock readingunix()/unix_ms()/unix_ns()— unix time conveniences
Lean & Correct
- Zero runtime dependencies — wraps
std::time #![forbid(unsafe_code)]— enforced by the compilerno_std-compatible — the crate builds against bare-metal targets withdefault-features = false- Cross-platform — Linux, macOS, Windows
This crate deliberately does not do calendar math, date formatting, or timezones. For that, use
chronoortime. clock-lib is the lean primitive layer beneath them.
Installation
[]
= "1.0"
For no_std builds, opt out of the default std feature:
[]
= { = "1.0", = false }
In no_std mode, only the VERSION constant is exposed — the reading APIs all require an OS clock and are gated behind the std feature. This is the honest cost of a portable clock library: the readings themselves are platform-specific.
Quick Start
use clock_lib as clock;
// Measure elapsed time (monotonic — safe)
let start = now;
// ... do work ...
let took = elapsed;
// Get a timestamp (wall-clock)
let unix_seconds = unix;
Reach for the typed surface when you need it — Monotonic for elapsed-time math, Wall for timestamps. The compiler refuses to mix the two, so you cannot accidentally measure an interval with a clock that can step backwards.
Deterministic Time in Tests
use ;
use Duration;
let clock = new;
let stamp = clock.now;
assert!;
clock.advance; // instant, no sleep
assert!;
Inject Clock into anything time-driven (rate limiters, TTL caches, timeouts) and your test suite drops every thread::sleep it had.
Documentation
- API reference — every public type, trait, and function with examples.
- Performance — benchmark methodology and the zero-overhead claim, verified.
- Developer guidelines — the engineering bar this project is built to.
- Release notes — what shipped, when, and why.
Contributing
Contributions are welcome under the project's dual license. Before opening a pull request, please make sure:
cargo fmt --all -- --checkpasses.cargo clippy --all-targets --all-features -- -D warningsis clean.cargo test --all-featurespasses.- New public items include documentation and at least one example.
- The API reference and CHANGELOG are updated alongside code changes.