qubit-clock 0.1.3

Thread-safe clock abstractions for Rust: monotonic clocks, mock testing, high-precision time meters, and timezone support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/
//! Nanosecond-precision time meter implementation.
//!
//! This module provides [`NanoTimeMeter`], a high-precision time
//! measurement tool with nanosecond precision. For millisecond precision,
//! use [`TimeMeter`](super::TimeMeter).
//!
//! # Author
//!
//! Haixing Hu

use crate::meter::format::{format_duration_nanos, format_speed};
use crate::{NanoClock, NanoMonotonicClock};
use chrono::Duration;

/// A time meter for measuring elapsed time with nanosecond precision.
///
/// This is the high-precision version of [`TimeMeter`](super::TimeMeter),
/// specifically designed for scenarios requiring nanosecond-level
/// precision, such as microbenchmarking and high-frequency operation
/// performance analysis.
///
/// # Differences from TimeMeter
///
/// - **Precision**: NanoTimeMeter provides nanosecond precision,
///   TimeMeter provides millisecond precision
/// - **Performance**: NanoTimeMeter has slightly higher computational
///   overhead, but offers higher precision
/// - **Use Cases**: NanoTimeMeter is suitable for microbenchmarking,
///   TimeMeter is suitable for general business monitoring
///
/// # Thread Safety
///
/// This type is not thread-safe. If you need to use it in a
/// multi-threaded environment, you should create separate instances for
/// each thread or use external synchronization mechanisms.
///
/// # Examples
///
/// ```
/// use qubit_clock::meter::NanoTimeMeter;
///
/// // Basic usage
/// let mut meter = NanoTimeMeter::new();
/// meter.start();
/// // Perform high-frequency operations
/// meter.stop();
/// println!("Elapsed: {} nanos", meter.nanos());
/// println!("Elapsed: {}", meter.readable_duration());
/// ```
///
/// # Author
///
/// Haixing Hu
pub struct NanoTimeMeter<C: NanoClock> {
    /// The clock used by this meter.
    clock: C,
    /// Start timestamp in nanoseconds. `None` means not started.
    /// Uses i128 to avoid overflow.
    start_time: Option<i128>,
    /// End timestamp in nanoseconds. `None` means not stopped.
    /// Uses i128 to avoid overflow.
    end_time: Option<i128>,
}

impl<C: NanoClock> NanoTimeMeter<C> {
    /// Creates a new nano time meter with the specified clock.
    ///
    /// # Arguments
    ///
    /// * `clock` - The clock to use for time measurement
    ///
    /// # Returns
    ///
    /// A new `NanoTimeMeter` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::{NanoMonotonicClock, meter::NanoTimeMeter};
    ///
    /// let clock = NanoMonotonicClock::new();
    /// let meter = NanoTimeMeter::with_clock(clock);
    /// ```
    #[inline]
    pub fn with_clock(clock: C) -> Self {
        NanoTimeMeter {
            clock,
            start_time: None,
            end_time: None,
        }
    }

    /// Creates a new nano time meter with the specified clock and starts
    /// it immediately.
    ///
    /// # Arguments
    ///
    /// * `clock` - The clock to use for time measurement
    ///
    /// # Returns
    ///
    /// A new `NanoTimeMeter` instance that has already been started
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::{NanoMonotonicClock, meter::NanoTimeMeter};
    ///
    /// let clock = NanoMonotonicClock::new();
    /// let meter = NanoTimeMeter::with_clock_started(clock);
    /// ```
    #[inline]
    pub fn with_clock_started(clock: C) -> Self {
        let mut meter = Self::with_clock(clock);
        meter.start();
        meter
    }

    /// Starts this meter.
    ///
    /// Records the current time as the start timestamp. If the meter has
    /// already been started, this operation will restart timing.
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::new();
    /// meter.start();
    /// ```
    #[inline]
    pub fn start(&mut self) {
        self.start_time = Some(self.clock.nanos());
        self.end_time = None;
    }

    /// Stops this meter.
    ///
    /// Records the current time as the end timestamp. After calling this
    /// method, `duration()` will return a fixed time interval until
    /// `start()` or `reset()` is called again.
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.stop();
    /// ```
    #[inline]
    pub fn stop(&mut self) {
        self.end_time = Some(self.clock.nanos());
    }

    /// Resets this meter.
    ///
    /// Clears the start and end timestamps, restoring the meter to its
    /// initial state. After reset, you need to call `start()` again to
    /// begin a new time measurement.
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.stop();
    /// meter.reset();
    /// ```
    #[inline]
    pub fn reset(&mut self) {
        self.start_time = None;
        self.end_time = None;
    }

    /// Resets and immediately starts this meter.
    ///
    /// This is equivalent to calling `reset()` followed by `start()`.
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.restart();
    /// // Do more work
    /// ```
    #[inline]
    pub fn restart(&mut self) {
        self.reset();
        self.start();
    }

    /// Returns the elapsed duration in nanoseconds.
    ///
    /// If the meter has been stopped (by calling `stop()`), returns the
    /// time interval from start to stop. If the meter has not been
    /// stopped, returns the time interval from start to the current
    /// moment.
    ///
    /// If the meter has not been started (by calling `start()`), returns
    /// 0.
    ///
    /// To avoid overflow issues, this method uses safe arithmetic
    /// operations.
    ///
    /// # Returns
    ///
    /// The elapsed duration in nanoseconds
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// assert!(meter.nanos() > 0);
    /// ```
    #[inline]
    pub fn nanos(&self) -> i128 {
        let start = match self.start_time {
            Some(t) => t,
            None => return 0,
        };
        let end = self.end_time.unwrap_or_else(|| self.clock.nanos());
        end - start
    }

    /// Returns the elapsed duration in microseconds.
    ///
    /// This method is based on the result of `nanos()`, converting
    /// nanoseconds to microseconds.
    ///
    /// # Returns
    ///
    /// The elapsed duration in microseconds
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// assert!(meter.micros() >= 0);
    /// ```
    #[inline]
    pub fn micros(&self) -> i128 {
        self.nanos() / 1_000
    }

    /// Returns the elapsed duration in milliseconds.
    ///
    /// This method is based on the result of `nanos()`, converting
    /// nanoseconds to milliseconds.
    ///
    /// If the meter has not been started (by calling `start()`), returns
    /// 0.
    ///
    /// # Returns
    ///
    /// The elapsed duration in milliseconds
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    /// use std::thread;
    /// use std::time::Duration;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// thread::sleep(Duration::from_millis(100));
    /// assert!(meter.millis() >= 100);
    /// ```
    #[inline]
    pub fn millis(&self) -> i64 {
        (self.nanos() / 1_000_000) as i64
    }

    /// Returns the elapsed duration in seconds.
    ///
    /// This method is based on the result of `nanos()`, converting
    /// nanoseconds to seconds (rounded down).
    ///
    /// # Returns
    ///
    /// The elapsed duration in seconds
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    /// use std::thread;
    /// use std::time::Duration;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// thread::sleep(Duration::from_secs(1));
    /// assert!(meter.seconds() >= 1);
    /// ```
    #[inline]
    pub fn seconds(&self) -> i64 {
        (self.nanos() / 1_000_000_000) as i64
    }

    /// Returns the elapsed duration in minutes.
    ///
    /// This method is based on the result of `nanos()`, converting
    /// nanoseconds to minutes (rounded down).
    ///
    /// # Returns
    ///
    /// The elapsed duration in minutes
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::new();
    /// meter.start();
    /// // Simulate some time
    /// meter.stop();
    /// ```
    #[inline]
    pub fn minutes(&self) -> i64 {
        (self.nanos() / 60_000_000_000) as i64
    }

    /// Returns the elapsed duration as a `Duration` object.
    ///
    /// If the meter has been stopped (by calling `stop()`), returns the
    /// time interval from start to stop. If the meter has not been
    /// stopped, returns the time interval from start to the current
    /// moment.
    ///
    /// If the meter has not been started (by calling `start()`), returns
    /// a zero duration.
    ///
    /// The returned `Duration` object has nanosecond precision.
    ///
    /// # Returns
    ///
    /// The elapsed duration as a `Duration` object (nanosecond precision)
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// let duration = meter.duration();
    /// ```
    #[inline]
    pub fn duration(&self) -> Duration {
        Duration::nanoseconds(self.nanos() as i64)
    }

    /// Returns a human-readable string representation of the elapsed
    /// duration.
    ///
    /// Formats the duration into an easy-to-read string, such as
    /// "1h 23m 45s" or "2.5s".
    ///
    /// # Returns
    ///
    /// A human-readable string representation of the duration
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.stop();
    /// println!("Elapsed: {}", meter.readable_duration());
    /// ```
    #[inline]
    pub fn readable_duration(&self) -> String {
        format_duration_nanos(self.nanos())
    }

    /// Calculates the per-second speed for a given count.
    ///
    /// Computes the average count processed per second during the elapsed
    /// time. Useful for performance monitoring and speed analysis.
    ///
    /// # Arguments
    ///
    /// * `count` - The count value to calculate speed for
    ///
    /// # Returns
    ///
    /// The per-second speed, or `None` if the elapsed time is zero
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    /// use std::thread;
    /// use std::time::Duration;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// thread::sleep(Duration::from_secs(1));
    /// meter.stop();
    /// if let Some(speed) = meter.speed_per_second(1000) {
    ///     println!("Speed: {:.2} items/s", speed);
    /// }
    /// ```
    #[inline]
    pub fn speed_per_second(&self, count: usize) -> Option<f64> {
        let seconds = self.seconds();
        if seconds == 0 {
            None
        } else {
            Some(count as f64 / seconds as f64)
        }
    }

    /// Calculates the per-minute speed for a given count.
    ///
    /// Computes the average count processed per minute during the elapsed
    /// time. Useful for performance monitoring and speed analysis.
    ///
    /// # Arguments
    ///
    /// * `count` - The count value to calculate speed for
    ///
    /// # Returns
    ///
    /// The per-minute speed, or `None` if the elapsed time is zero
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    /// use std::thread;
    /// use std::time::Duration;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// thread::sleep(Duration::from_secs(1));
    /// meter.stop();
    /// if let Some(speed) = meter.speed_per_minute(1000) {
    ///     println!("Speed: {:.2} items/m", speed);
    /// }
    /// ```
    #[inline]
    pub fn speed_per_minute(&self, count: usize) -> Option<f64> {
        let seconds = self.seconds();
        if seconds == 0 {
            None
        } else {
            Some((count as f64 / seconds as f64) * 60.0)
        }
    }

    /// Returns a formatted string of the per-second speed for a given
    /// count.
    ///
    /// # Arguments
    ///
    /// * `count` - The count value to calculate speed for
    ///
    /// # Returns
    ///
    /// A string in the format "{speed}/s", or "N/A" if the elapsed time
    /// is zero
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.stop();
    /// println!("Speed: {}", meter.formatted_speed_per_second(1000));
    /// ```
    #[inline]
    pub fn formatted_speed_per_second(&self, count: usize) -> String {
        match self.speed_per_second(count) {
            Some(speed) => format_speed(speed, "/s"),
            None => "N/A".to_string(),
        }
    }

    /// Returns a formatted string of the per-minute speed for a given
    /// count.
    ///
    /// # Arguments
    ///
    /// * `count` - The count value to calculate speed for
    ///
    /// # Returns
    ///
    /// A string in the format "{speed}/m", or "N/A" if the elapsed time
    /// is zero
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// // Do some work
    /// meter.stop();
    /// println!("Speed: {}", meter.formatted_speed_per_minute(1000));
    /// ```
    #[inline]
    pub fn formatted_speed_per_minute(&self, count: usize) -> String {
        match self.speed_per_minute(count) {
            Some(speed) => format_speed(speed, "/m"),
            None => "N/A".to_string(),
        }
    }

    /// Checks if the meter is currently running.
    ///
    /// # Returns
    ///
    /// `true` if the meter has been started but not stopped, `false`
    /// otherwise
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::new();
    /// assert!(!meter.is_running());
    /// meter.start();
    /// assert!(meter.is_running());
    /// meter.stop();
    /// assert!(!meter.is_running());
    /// ```
    #[inline]
    pub fn is_running(&self) -> bool {
        self.start_time.is_some() && self.end_time.is_none()
    }

    /// Checks if the meter has been stopped.
    ///
    /// # Returns
    ///
    /// `true` if the meter has been stopped, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::start_now();
    /// assert!(!meter.is_stopped());
    /// meter.stop();
    /// assert!(meter.is_stopped());
    /// ```
    #[inline]
    pub fn is_stopped(&self) -> bool {
        self.end_time.is_some()
    }

    /// Returns a reference to the clock used by this meter.
    ///
    /// # Returns
    ///
    /// A reference to the clock
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let meter = NanoTimeMeter::new();
    /// let clock = meter.clock();
    /// ```
    #[inline]
    pub fn clock(&self) -> &C {
        &self.clock
    }

    /// Returns a mutable reference to the clock used by this meter.
    ///
    /// # Returns
    ///
    /// A mutable reference to the clock
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let mut meter = NanoTimeMeter::new();
    /// let clock = meter.clock_mut();
    /// ```
    #[inline]
    pub fn clock_mut(&mut self) -> &mut C {
        &mut self.clock
    }
}

impl NanoTimeMeter<NanoMonotonicClock> {
    /// Creates a new nano time meter using the default
    /// `NanoMonotonicClock`.
    ///
    /// The default clock uses `NanoMonotonicClock`, which is based on
    /// `Instant` and is not affected by system time adjustments, making
    /// it more suitable for high-precision time measurement.
    ///
    /// # Returns
    ///
    /// A new `NanoTimeMeter` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let meter = NanoTimeMeter::new();
    /// ```
    #[inline]
    pub fn new() -> Self {
        Self::with_clock(NanoMonotonicClock::new())
    }

    /// Creates a new nano time meter using the default
    /// `NanoMonotonicClock` and starts it immediately.
    ///
    /// # Returns
    ///
    /// A new `NanoTimeMeter` instance that has already been started
    ///
    /// # Examples
    ///
    /// ```
    /// use qubit_clock::meter::NanoTimeMeter;
    ///
    /// let meter = NanoTimeMeter::start_now();
    /// ```
    #[inline]
    pub fn start_now() -> Self {
        Self::with_clock_started(NanoMonotonicClock::new())
    }
}

impl Default for NanoTimeMeter<NanoMonotonicClock> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}