NanoClock

Trait NanoClock 

Source
pub trait NanoClock: Clock {
    // Required method
    fn nanos(&self) -> i128;

    // Provided method
    fn time_precise(&self) -> DateTime<Utc> { ... }
}
Expand description

A trait representing a clock with nanosecond precision.

This trait extends Clock to provide high-precision time measurements at the nanosecond level. It’s useful for performance testing, microbenchmarking, and scenarios requiring very precise time measurements.

§Note

The nanosecond timestamp is stored as an i128 to avoid overflow issues.

§Examples

use prism3_clock::{NanoClock, NanoMonotonicClock};

let clock = NanoMonotonicClock::new();
let start = clock.nanos();

// Perform some operation
for _ in 0..1000 {
    // Some work
}

let elapsed = clock.nanos() - start;
println!("Elapsed: {} ns", elapsed);

§Author

Haixing Hu

Required Methods§

Source

fn nanos(&self) -> i128

Returns the current time as a Unix timestamp in nanoseconds (UTC).

The timestamp represents the number of nanoseconds since the Unix epoch (1970-01-01 00:00:00 UTC).

§Returns

The current time as nanoseconds since the Unix epoch.

§Examples
use prism3_clock::{NanoClock, NanoMonotonicClock};

let clock = NanoMonotonicClock::new();
let nanos = clock.nanos();
assert!(nanos > 0);

Provided Methods§

Source

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

Returns the current time as a DateTime<Utc> with nanosecond precision.

This method has a default implementation that constructs a DateTime<Utc> from the result of nanos().

§Returns

The current time as a DateTime<Utc> object with nanosecond precision.

§Examples
use prism3_clock::{NanoClock, NanoMonotonicClock};

let clock = NanoMonotonicClock::new();
let time = clock.time_precise();
println!("Current time (precise): {}", time);

Implementors§