pub struct MonotonicClock { /* private fields */ }Expand description
A clock implementation that provides monotonically increasing time.
This clock uses std::time::Instant as its time source, which guarantees
that time always moves forward and is not affected by system time
adjustments (e.g., NTP synchronization, manual changes).
The clock records a base point when created, and all subsequent time queries are calculated relative to this base point.
§Use Cases
- Performance monitoring
- Timeout control
- Measuring time intervals
- Any scenario requiring stable, monotonic time
§Note
This clock is designed for measuring time intervals, not for getting the
“current time” for display purposes. For timezone support, you can wrap it
with Zoned, but this is generally not recommended as
timezone information is not meaningful for interval measurements.
§Thread Safety
This type is completely thread-safe as all fields are immutable after creation.
§Examples
use qubit_clock::{Clock, MonotonicClock};
use std::thread;
use std::time::Duration;
let clock = MonotonicClock::new();
let start = clock.millis();
thread::sleep(Duration::from_millis(100));
let elapsed = clock.millis() - start;
assert!(elapsed >= 100);§Author
Haixing Hu
Implementations§
Source§impl MonotonicClock
impl MonotonicClock
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the elapsed monotonic duration since this clock was created.
This value is based purely on Instant and is not affected by system
time adjustments.
§Returns
The elapsed monotonic duration.
§Examples
use qubit_clock::MonotonicClock;
use std::thread;
use std::time::Duration;
let clock = MonotonicClock::new();
thread::sleep(Duration::from_millis(10));
assert!(clock.elapsed() >= Duration::from_millis(10));Sourcepub fn monotonic_millis(&self) -> i64
pub fn monotonic_millis(&self) -> i64
Returns the elapsed monotonic time in milliseconds since creation.
Unlike Clock::millis, this value does not
include a wall-clock epoch anchor and is intended for interval
measurement.
§Returns
The elapsed monotonic milliseconds, saturated at i64::MAX.
§Examples
use qubit_clock::MonotonicClock;
use std::thread;
use std::time::Duration;
let clock = MonotonicClock::new();
thread::sleep(Duration::from_millis(10));
assert!(clock.monotonic_millis() >= 10);Trait Implementations§
Source§impl Clock for MonotonicClock
impl Clock for MonotonicClock
Source§impl Clone for MonotonicClock
impl Clone for MonotonicClock
Source§fn clone(&self) -> MonotonicClock
fn clone(&self) -> MonotonicClock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more