Expand description
Cross-platform time primitives.
Provides Instant and SystemTime plus traits for injecting custom
platform clocks through a global time context.
§universal-time
Cross-platform time primitives for Rust that can run in any envuironment.
§Why
universal-time gives you a single API for:
Instantfor monotonic elapsed-time measurementsSystemTimefor wall-clock timestamps- Trait-based clock injection for platforms without built-in time access
§Quick Start
use universal_time::{Instant, SystemTime, UNIX_EPOCH};
fn main() {
let start = Instant::now();
let now = SystemTime::now();
let elapsed = start.elapsed();
let since_epoch = now.duration_since(UNIX_EPOCH).unwrap_or_default();
println!("elapsed = {:?}", elapsed);
println!("since epoch = {:?}", since_epoch);
}For more examples, check out the examples directory.
§Panic Behavior
In no_std mode, and in std mode on wasm32-unknown-unknown, both
Instant::now() and SystemTime::now() panic when:
- no global context has been installed, or
- installed context returns
Nonefor that clock type
This is intentional so missing time sources fail fast instead of silently returning fake timestamps.
§Concurrency Notes
std: global context usesOnceLockno_stdwith atomics: global context uses lock-free once initializationno_stdwithout atomics: fallback expects single-threaded startup initialization
Structs§
- Duration
- A
Durationtype to represent a span of time, typically used for system timeouts. - Global
Time Context Already Set - Error returned when attempting to set the global time context more than once.
- Instant
- Monotonic clock reading.
- System
Time - Wall clock time represented as a duration since the Unix epoch.
Constants§
- UNIX_
EPOCH - Unix epoch (January 1, 1970).
Traits§
- Monotonic
Clock - Source of monotonic instants.
- Time
Context - A full time context that can provide wall-clock and monotonic time.
- Wall
Clock - Source of wall-clock timestamps.
Functions§
- global_
time_ context - Returns the globally configured time context if one was installed.
- set_
global_ time_ context - Installs the global time context.