quicklog_clock/
quanta.rs

1use chrono::{DateTime, Duration, OutOfRangeError, Utc};
2use quanta::Instant;
3
4use crate::Clock;
5
6pub struct QuantaClock {
7    clock: quanta::Clock,
8    start_time: DateTime<Utc>,
9    start_instant: quanta::Instant,
10}
11
12impl QuantaClock {
13    pub fn new() -> QuantaClock {
14        let clock = quanta::Clock::new();
15        // this also lazily initializes a global clock which
16        // can take up to 200ms if it is not initialized
17        let start_instant = clock.now();
18        QuantaClock {
19            clock,
20            start_time: Utc::now(),
21            start_instant,
22        }
23    }
24}
25
26impl Default for QuantaClock {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl Clock for QuantaClock {
33    fn get_instant(&self) -> Instant {
34        self.clock.now()
35    }
36
37    fn compute_system_time_from_instant(
38        &self,
39        instant: Instant,
40    ) -> Result<DateTime<Utc>, OutOfRangeError> {
41        let elapsed_time = instant.duration_since(self.start_instant);
42        let chrono_duration = Duration::from_std(elapsed_time);
43        chrono_duration.map(|duration| self.start_time + duration)
44    }
45}