prism3_clock/
nano_monotonic.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! High-precision monotonic clock implementation.
10//!
11//! This module provides [`NanoMonotonicClock`], a clock implementation that
12//! provides nanosecond-precision monotonic time measurements.
13//!
14//! # Author
15//!
16//! Haixing Hu
17
18use crate::{Clock, NanoClock};
19use chrono::Utc;
20use std::time::Instant;
21
22/// A clock implementation that provides nanosecond-precision monotonic time.
23///
24/// This clock combines the monotonic guarantees of
25/// [`MonotonicClock`](crate::MonotonicClock) with the nanosecond precision
26/// of [`NanoClock`]. It uses `std::time::Instant` as its time source and
27/// stores the base time with nanosecond precision.
28///
29/// # Use Cases
30///
31/// - High-precision performance testing
32/// - Microbenchmarking
33/// - Scenarios requiring nanosecond-level time measurements
34///
35/// # Thread Safety
36///
37/// This type is completely thread-safe as all fields are immutable after
38/// creation.
39///
40/// # Examples
41///
42/// ```
43/// use prism3_clock::{NanoClock, NanoMonotonicClock};
44///
45/// let clock = NanoMonotonicClock::new();
46/// let start = clock.nanos();
47///
48/// // Perform some operation
49/// for _ in 0..1000 {
50///     // Some work
51/// }
52///
53/// let elapsed = clock.nanos() - start;
54/// println!("Elapsed: {} ns", elapsed);
55/// ```
56///
57/// # Author
58///
59/// Haixing Hu
60#[derive(Debug, Clone)]
61pub struct NanoMonotonicClock {
62    /// The base instant when this clock was created.
63    instant_base: Instant,
64    /// The system time (seconds part) when this clock was created.
65    system_time_base_seconds: i64,
66    /// The system time (nanoseconds part) when this clock was created.
67    system_time_base_nanos: u32,
68}
69
70impl NanoMonotonicClock {
71    /// Creates a new `NanoMonotonicClock`.
72    ///
73    /// The clock records the current instant and system time (with nanosecond
74    /// precision) as its base point. All subsequent time queries will be
75    /// calculated relative to this base point.
76    ///
77    /// # Returns
78    ///
79    /// A new `NanoMonotonicClock` instance.
80    ///
81    /// # Examples
82    ///
83    /// ```
84    /// use prism3_clock::NanoMonotonicClock;
85    ///
86    /// let clock = NanoMonotonicClock::new();
87    /// ```
88    ///
89    pub fn new() -> Self {
90        let now = Utc::now();
91        NanoMonotonicClock {
92            instant_base: Instant::now(),
93            system_time_base_seconds: now.timestamp(),
94            system_time_base_nanos: now.timestamp_subsec_nanos(),
95        }
96    }
97}
98
99impl Default for NanoMonotonicClock {
100    fn default() -> Self {
101        Self::new()
102    }
103}
104
105impl Clock for NanoMonotonicClock {
106    fn millis(&self) -> i64 {
107        let elapsed = self.instant_base.elapsed();
108        let elapsed_millis = elapsed.as_millis() as i64;
109        let base_millis =
110            self.system_time_base_seconds * 1000 + (self.system_time_base_nanos / 1_000_000) as i64;
111        base_millis + elapsed_millis
112    }
113}
114
115impl NanoClock for NanoMonotonicClock {
116    fn nanos(&self) -> i128 {
117        let elapsed = self.instant_base.elapsed();
118        let elapsed_nanos = elapsed.as_nanos() as i128;
119        let base_nanos = (self.system_time_base_seconds as i128) * 1_000_000_000
120            + (self.system_time_base_nanos as i128);
121        base_nanos + elapsed_nanos
122    }
123}