Skip to main content

es_entity/clock/
global.rs

1use chrono::{DateTime, Utc};
2
3use std::sync::OnceLock;
4use std::time::Duration;
5
6use super::{ClockController, ClockHandle, ClockSleep, ClockTimeout};
7
8struct GlobalState {
9    handle: ClockHandle,
10    controller: Option<ClockController>,
11}
12
13static GLOBAL: OnceLock<GlobalState> = OnceLock::new();
14
15/// Global clock access - like `Utc::now()` but testable.
16pub struct Clock;
17
18impl Clock {
19    /// Get current time from the global clock.
20    ///
21    /// Lazily initializes to realtime if not already set.
22    pub fn now() -> DateTime<Utc> {
23        Self::handle().now()
24    }
25
26    /// Get the current date (without time component).
27    ///
28    /// Lazily initializes to realtime if not already set.
29    pub fn today() -> chrono::NaiveDate {
30        Self::handle().today()
31    }
32
33    /// Sleep using the global clock.
34    pub fn sleep(duration: Duration) -> ClockSleep {
35        Self::handle().sleep(duration)
36    }
37
38    /// Timeout using the global clock.
39    pub fn timeout<F: std::future::Future>(duration: Duration, future: F) -> ClockTimeout<F> {
40        Self::handle().timeout(duration, future)
41    }
42
43    /// Get a reference to the global clock handle.
44    pub fn handle() -> &'static ClockHandle {
45        &GLOBAL
46            .get_or_init(|| GlobalState {
47                handle: ClockHandle::realtime(),
48                controller: None,
49            })
50            .handle
51    }
52
53    /// Install a manual clock globally.
54    ///
55    /// - If not initialized: installs manual clock, returns controller
56    /// - If already manual: returns existing controller (idempotent)
57    /// - If already realtime: panics
58    ///
59    /// Must be called before any `Clock::now()` calls if you want manual time.
60    pub fn install_manual() -> ClockController {
61        // Check if already initialized
62        if let Some(state) = GLOBAL.get() {
63            return state
64                .controller
65                .clone()
66                .expect("Cannot install manual clock: realtime clock already initialized");
67        }
68
69        // Try to initialize
70        let (handle, ctrl) = ClockHandle::manual();
71
72        match GLOBAL.set(GlobalState {
73            handle,
74            controller: Some(ctrl.clone()),
75        }) {
76            Ok(()) => ctrl,
77            Err(_) => {
78                // Race: someone else initialized between our check and set
79                GLOBAL
80                    .get()
81                    .unwrap()
82                    .controller
83                    .clone()
84                    .expect("Cannot install manual clock: realtime clock already initialized")
85            }
86        }
87    }
88
89    /// Install a manual clock globally starting at a specific time.
90    ///
91    /// See [`install_manual`](Self::install_manual) for details.
92    pub fn install_manual_at(start_at: DateTime<Utc>) -> ClockController {
93        // Check if already initialized
94        if let Some(state) = GLOBAL.get() {
95            return state
96                .controller
97                .clone()
98                .expect("Cannot install manual clock: realtime clock already initialized");
99        }
100
101        // Try to initialize
102        let (handle, ctrl) = ClockHandle::manual_at(start_at);
103
104        match GLOBAL.set(GlobalState {
105            handle,
106            controller: Some(ctrl.clone()),
107        }) {
108            Ok(()) => ctrl,
109            Err(_) => {
110                // Race: someone else initialized between our check and set
111                GLOBAL
112                    .get()
113                    .unwrap()
114                    .controller
115                    .clone()
116                    .expect("Cannot install manual clock: realtime clock already initialized")
117            }
118        }
119    }
120
121    /// Check if a manual clock is installed.
122    pub fn is_manual() -> bool {
123        GLOBAL
124            .get()
125            .map(|s| s.controller.is_some())
126            .unwrap_or(false)
127    }
128
129    /// Get the current manual time, if a manual clock is installed.
130    ///
131    /// Returns:
132    /// - `None` if no clock is initialized (doesn't initialize one)
133    /// - `None` for realtime clocks
134    /// - `Some(time)` for manual clocks
135    pub fn manual_now() -> Option<DateTime<Utc>> {
136        GLOBAL.get().and_then(|s| s.handle.manual_now())
137    }
138}