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 - No
unsafecode no_stdsupport — via the default-off path- 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
[]
= "0.2"
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.
Documentation
- API reference — every public type, trait, and function with examples.
- 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.