prism3_clock/
system.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! System clock implementation.
10//!
11//! This module provides [`SystemClock`], a clock implementation that uses the
12//! system's wall clock time. The time is subject to system time adjustments
13//! (e.g., NTP synchronization, manual changes).
14//!
15//! # Author
16//!
17//! Haixing Hu
18
19use crate::Clock;
20use chrono::{DateTime, Utc};
21
22/// A clock implementation that uses the system's wall clock time.
23///
24/// This is a zero-sized type (ZST) with no runtime overhead. It directly
25/// queries the system for the current time whenever [`millis()`](Clock::millis)
26/// or [`time()`](Clock::time) is called.
27///
28/// # Note
29///
30/// The time returned by this clock is subject to system time adjustments,
31/// such as NTP synchronization or manual changes. For monotonic time
32/// measurements, use [`MonotonicClock`](crate::MonotonicClock) instead.
33///
34/// # Thread Safety
35///
36/// This type is completely thread-safe as it has no mutable state.
37///
38/// # Examples
39///
40/// ```
41/// use prism3_clock::{Clock, SystemClock};
42///
43/// let clock = SystemClock::new();
44/// let timestamp = clock.millis();
45/// let time = clock.time();
46/// println!("Current system time: {}", time);
47/// ```
48///
49/// # Author
50///
51/// Haixing Hu
52#[derive(Debug, Clone, Copy, Default)]
53pub struct SystemClock;
54
55impl SystemClock {
56    /// Creates a new `SystemClock`.
57    ///
58    /// # Returns
59    ///
60    /// A new `SystemClock` instance.
61    ///
62    /// # Examples
63    ///
64    /// ```
65    /// use prism3_clock::SystemClock;
66    ///
67    /// let clock = SystemClock::new();
68    /// ```
69    ///
70    pub fn new() -> Self {
71        SystemClock
72    }
73}
74
75impl Clock for SystemClock {
76    fn millis(&self) -> i64 {
77        Utc::now().timestamp_millis()
78    }
79
80    fn time(&self) -> DateTime<Utc> {
81        Utc::now()
82    }
83}