Skip to main content

Crate universal_time

Crate universal_time 

Source
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:

  • Instant for monotonic elapsed-time measurements
  • SystemTime for 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 None for that clock type

This is intentional so missing time sources fail fast instead of silently returning fake timestamps.

§Concurrency Notes

  • std: global context uses OnceLock
  • no_std with atomics: global context uses lock-free once initialization
  • no_std without atomics: fallback expects single-threaded startup initialization

Structs§

Duration
A Duration type to represent a span of time, typically used for system timeouts.
GlobalTimeContextAlreadySet
Error returned when attempting to set the global time context more than once.
Instant
Monotonic clock reading.
SystemTime
Wall clock time represented as a duration since the Unix epoch.

Constants§

UNIX_EPOCH
Unix epoch (January 1, 1970).

Traits§

MonotonicClock
Source of monotonic instants.
TimeContext
A full time context that can provide wall-clock and monotonic time.
WallClock
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.