saito_core/core/process/
keep_time.rs

1use crate::core::defs::Timestamp;
2use std::sync::Arc;
3
4/// Provides the current time in a implementation agnostic way into the core logic library. Since the core logic lib can be used on rust
5/// application as well as WASM, it needs to get the time via this trait implementation.
6
7pub trait KeepTime {
8    fn get_timestamp_in_ms(&self) -> Timestamp;
9}
10
11#[derive(Clone)]
12pub struct Timer {
13    pub time_reader: Arc<dyn KeepTime + Sync + Send>,
14    pub hasten_multiplier: u64,
15    pub start_time: Timestamp,
16}
17
18impl Timer {
19    pub fn get_timestamp_in_ms(&self) -> Timestamp {
20        assert_ne!(
21            self.hasten_multiplier, 0,
22            "hasten multiplier should be positive"
23        );
24
25        let current_time = self.time_reader.get_timestamp_in_ms();
26
27        self.start_time + (current_time - self.start_time) * self.hasten_multiplier
28    }
29}