es_entity/clock/
config.rs1use serde::{Deserialize, Serialize};
2
3use chrono::{DateTime, Utc};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ArtificialClockConfig {
8 pub start_at: DateTime<Utc>,
10 pub mode: ArtificialMode,
12}
13
14fn truncate_to_millis(time: DateTime<Utc>) -> DateTime<Utc> {
17 DateTime::from_timestamp_millis(time.timestamp_millis()).expect("valid timestamp")
18}
19
20impl ArtificialClockConfig {
21 pub fn manual() -> Self {
23 Self {
24 start_at: truncate_to_millis(Utc::now()),
25 mode: ArtificialMode::Manual,
26 }
27 }
28
29 pub fn manual_at(start_at: DateTime<Utc>) -> Self {
31 Self {
32 start_at: truncate_to_millis(start_at),
33 mode: ArtificialMode::Manual,
34 }
35 }
36
37 pub fn auto(time_scale: f64) -> Self {
39 Self {
40 start_at: truncate_to_millis(Utc::now()),
41 mode: ArtificialMode::AutoAdvance { time_scale },
42 }
43 }
44
45 pub fn auto_at(start_at: DateTime<Utc>, time_scale: f64) -> Self {
47 Self {
48 start_at: truncate_to_millis(start_at),
49 mode: ArtificialMode::AutoAdvance { time_scale },
50 }
51 }
52}
53
54impl Default for ArtificialClockConfig {
55 fn default() -> Self {
56 Self::manual()
57 }
58}
59
60#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
62#[serde(tag = "type", rename_all = "snake_case")]
63pub enum ArtificialMode {
64 AutoAdvance {
67 time_scale: f64,
69 },
70 Manual,
73}