qubit_clock/nano_monotonic.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit 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 qubit_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 qubit_clock::NanoMonotonicClock;
85 ///
86 /// let clock = NanoMonotonicClock::new();
87 /// ```
88 ///
89 #[inline]
90 pub fn new() -> Self {
91 let now = Utc::now();
92 NanoMonotonicClock {
93 instant_base: Instant::now(),
94 system_time_base_seconds: now.timestamp(),
95 system_time_base_nanos: now.timestamp_subsec_nanos(),
96 }
97 }
98}
99
100impl Default for NanoMonotonicClock {
101 #[inline]
102 fn default() -> Self {
103 Self::new()
104 }
105}
106
107impl Clock for NanoMonotonicClock {
108 #[inline]
109 fn millis(&self) -> i64 {
110 let elapsed = self.instant_base.elapsed();
111 let elapsed_millis = elapsed.as_millis() as i64;
112 let base_millis =
113 self.system_time_base_seconds * 1000 + (self.system_time_base_nanos / 1_000_000) as i64;
114 base_millis + elapsed_millis
115 }
116}
117
118impl NanoClock for NanoMonotonicClock {
119 #[inline]
120 fn nanos(&self) -> i128 {
121 let elapsed = self.instant_base.elapsed();
122 let elapsed_nanos = elapsed.as_nanos() as i128;
123 let base_nanos = (self.system_time_base_seconds as i128) * 1_000_000_000
124 + (self.system_time_base_nanos as i128);
125 base_nanos + elapsed_nanos
126 }
127}