pzzld_server/types/
timestamp.rs

1/*
2    Appellation: timestamp <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use core::time::Duration;
6
7/// [Timestamp] is a generic type used to represent a timestamp.
8///
9/// The timestamp considers the standard timestamp to be the
10#[derive(
11    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
12)]
13pub struct Timestamp<T = u64>(pub T);
14
15impl<T> Timestamp<T> {
16    pub fn new(ts: T) -> Self {
17        Self(ts)
18    }
19    /// Get an immutable reference to the current timestamp.
20    pub fn as_ref(&self) -> &T {
21        &self.0
22    }
23    /// Get a mutable reference to the current timestamp.
24    pub fn as_mut(&mut self) -> &mut T {
25        &mut self.0
26    }
27    /// Get the current timestamp.
28    pub fn get(self) -> T {
29        self.0
30    }
31    /// Replace the current timestamp with a new one.
32    pub fn replace(&mut self, ts: T) -> T {
33        core::mem::replace(&mut self.0, ts)
34    }
35    /// Set the current timestamp to a new value.
36    pub fn set(&mut self, ts: T) {
37        self.0 = ts;
38    }
39    /// Take the current timestamp and replace it with the default value.
40    pub fn take(&mut self) -> T
41    where
42        T: Default,
43    {
44        core::mem::take(&mut self.0)
45    }
46}
47
48/// Standard timestamp in seconds.
49impl Timestamp<u64> {
50    pub fn now() -> Self {
51        Self(crate::std_time().as_secs())
52    }
53}
54
55/// Standard timestamp in milliseconds.
56impl Timestamp<u128> {
57    pub fn now() -> Self {
58        Self(crate::std_time().as_millis())
59    }
60}
61
62/// [`chrono`] timestamp
63impl Timestamp<i64> {
64    pub fn now() -> Self {
65        Self(chrono::Local::now().timestamp())
66    }
67
68    pub fn from_chrono<Tz: chrono::TimeZone>(ts: chrono::DateTime<Tz>) -> Self {
69        Self(ts.timestamp())
70    }
71}
72
73impl<T> AsRef<T> for Timestamp<T> {
74    fn as_ref(&self) -> &T {
75        &self.0
76    }
77}
78
79impl<T> AsMut<T> for Timestamp<T> {
80    fn as_mut(&mut self) -> &mut T {
81        &mut self.0
82    }
83}
84
85impl<T> core::borrow::Borrow<T> for Timestamp<T> {
86    fn borrow(&self) -> &T {
87        &self.0
88    }
89}
90
91impl<T> core::borrow::BorrowMut<T> for Timestamp<T> {
92    fn borrow_mut(&mut self) -> &mut T {
93        &mut self.0
94    }
95}
96
97impl<T> core::ops::Deref for Timestamp<T> {
98    type Target = T;
99
100    fn deref(&self) -> &Self::Target {
101        &self.0
102    }
103}
104
105impl<T> core::ops::DerefMut for Timestamp<T> {
106    fn deref_mut(&mut self) -> &mut Self::Target {
107        &mut self.0
108    }
109}
110
111impl<T> core::fmt::Display for Timestamp<T>
112where
113    T: core::fmt::Display,
114{
115    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
116        write!(f, "{}", self.0)
117    }
118}
119
120impl From<Duration> for Timestamp<u64> {
121    fn from(dur: Duration) -> Self {
122        Self(dur.as_secs())
123    }
124}
125
126impl From<Duration> for Timestamp<u128> {
127    fn from(dur: Duration) -> Self {
128        Self(dur.as_millis())
129    }
130}
131
132impl<Tz> From<chrono::DateTime<Tz>> for Timestamp<i64>
133where
134    Tz: chrono::TimeZone,
135{
136    fn from(ts: chrono::DateTime<Tz>) -> Self {
137        Self(ts.timestamp())
138    }
139}