es_entity/clock/
config.rs

1use chrono::{DateTime, Utc};
2
3/// Configuration for artificial time.
4#[derive(Debug, Clone)]
5pub struct ArtificialClockConfig {
6    /// What time should the clock start at (truncated to millisecond precision).
7    pub start_at: DateTime<Utc>,
8    /// How should time advance.
9    pub mode: ArtificialMode,
10}
11
12/// Truncate a DateTime to millisecond precision.
13/// This ensures consistency since we store time as epoch milliseconds.
14fn truncate_to_millis(time: DateTime<Utc>) -> DateTime<Utc> {
15    DateTime::from_timestamp_millis(time.timestamp_millis()).expect("valid timestamp")
16}
17
18impl ArtificialClockConfig {
19    /// Create a manual config starting at the current time.
20    pub fn manual() -> Self {
21        Self {
22            start_at: truncate_to_millis(Utc::now()),
23            mode: ArtificialMode::Manual,
24        }
25    }
26
27    /// Create a manual config starting at a specific time.
28    pub fn manual_at(start_at: DateTime<Utc>) -> Self {
29        Self {
30            start_at: truncate_to_millis(start_at),
31            mode: ArtificialMode::Manual,
32        }
33    }
34
35    /// Create an auto-advancing config.
36    pub fn auto(time_scale: f64) -> Self {
37        Self {
38            start_at: truncate_to_millis(Utc::now()),
39            mode: ArtificialMode::AutoAdvance { time_scale },
40        }
41    }
42
43    /// Create an auto-advancing config starting at a specific time.
44    pub fn auto_at(start_at: DateTime<Utc>, time_scale: f64) -> Self {
45        Self {
46            start_at: truncate_to_millis(start_at),
47            mode: ArtificialMode::AutoAdvance { time_scale },
48        }
49    }
50}
51
52impl Default for ArtificialClockConfig {
53    fn default() -> Self {
54        Self::manual()
55    }
56}
57
58/// How artificial time advances.
59#[derive(Debug, Clone, Copy)]
60pub enum ArtificialMode {
61    /// Time advances automatically at the given scale.
62    /// A time_scale of 1000.0 means 1 real second = 1000 artificial seconds.
63    AutoAdvance {
64        /// Multiplier for time passage (e.g., 1000.0 = 1000x faster)
65        time_scale: f64,
66    },
67    /// Time only advances via explicit `advance()` or `set_time()` calls.
68    /// This is ideal for deterministic testing.
69    Manual,
70}