Skip to main content

MonotonicClock

Struct MonotonicClock 

Source
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

Source

pub fn new() -> Self

Creates a new MonotonicClock.

The clock records the current instant and system time as its base point. All subsequent time queries will be calculated relative to this base point.

§Returns

A new MonotonicClock instance.

§Examples
use qubit_clock::MonotonicClock;

let clock = MonotonicClock::new();
Source

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));
Source

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

Source§

fn millis(&self) -> i64

Returns the current time as a Unix timestamp in milliseconds (UTC). Read more
Source§

fn time(&self) -> DateTime<Utc>

Returns the current time as a DateTime<Utc>. Read more
Source§

impl Clone for MonotonicClock

Source§

fn clone(&self) -> MonotonicClock

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MonotonicClock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MonotonicClock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.