livesplit_core/timing/atomic_date_time.rs
1use crate::{
2 platform::{utc_now, DateTime},
3 TimeSpan,
4};
5use core::ops::Sub;
6
7/// An Atomic Date Time represents a UTC [`DateTime`] that tries to be as close to
8/// an atomic clock as possible.
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10pub struct AtomicDateTime {
11 /// The UTC Date Time represented by this Atomic Date Time.
12 pub time: DateTime,
13 /// Represents whether the date time is actually properly derived from an
14 /// atomic clock. If the synchronization with the atomic clock didn't happen
15 /// yet or failed, this is set to `false`.
16 pub synced_with_atomic_clock: bool,
17}
18
19impl AtomicDateTime {
20 /// Creates a new Atomic Date Time from the UTC Date Time and the
21 /// information of whether this Date Time is derived from an atomic clock or
22 /// the local system that may be out of sync with the atomic clock.
23 pub const fn new(time: DateTime, synced_with_atomic_clock: bool) -> Self {
24 Self {
25 time,
26 synced_with_atomic_clock,
27 }
28 }
29
30 /// Creates a new Atomic Date Time that describes the current moment in
31 /// time. If a successful synchronization with an atomic clock occurred,
32 /// this value is marked as synchronized. Otherwise the local system's timer
33 /// is used.
34 ///
35 /// # Warning
36 ///
37 /// livesplit-core doesn't synchronize with any atomic clock yet.
38 #[inline]
39 pub fn now() -> Self {
40 AtomicDateTime {
41 time: utc_now(),
42 synced_with_atomic_clock: false,
43 }
44 }
45}
46
47impl Sub for AtomicDateTime {
48 type Output = TimeSpan;
49
50 fn sub(self, rhs: AtomicDateTime) -> TimeSpan {
51 (self.time - rhs.time).into()
52 }
53}
54
55impl Sub<DateTime> for AtomicDateTime {
56 type Output = TimeSpan;
57
58 fn sub(self, rhs: DateTime) -> TimeSpan {
59 (self.time - rhs).into()
60 }
61}