scsys_core/time/
timestamp.rs1use crate::time::Now;
6use core::time::Duration;
7
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
13pub struct Timestamp<T = u64>(pub T);
14
15impl<T> Timestamp<T> {
16 pub fn new(ts: T) -> Self {
17 Self(ts)
18 }
19 pub fn now() -> Self
21 where
22 Self: Now<Output = Self>,
23 {
24 <Self as Now>::now()
25 }
26 pub fn as_ref(&self) -> &T {
28 &self.0
29 }
30 pub fn as_mut(&mut self) -> &mut T {
32 &mut self.0
33 }
34 pub fn get(self) -> T {
36 self.0
37 }
38 pub fn replace(&mut self, ts: T) -> T {
40 core::mem::replace(&mut self.0, ts)
41 }
42 pub fn set(&mut self, ts: T) {
44 self.0 = ts;
45 }
46 pub fn take(&mut self) -> T
48 where
49 T: Default,
50 {
51 core::mem::take(&mut self.0)
52 }
53
54 pub fn update(&mut self, ts: T) {
55 self.0 = ts;
56 }
57}
58
59impl<T> Default for Timestamp<T>
60where
61 Self: Now<Output = Self>,
62{
63 fn default() -> Self {
64 Self::now()
65 }
66}
67
68#[cfg(feature = "std")]
69impl Now for Timestamp<u64> {
70 type Output = Self;
71
72 fn now() -> Self::Output {
73 Self::new(super::systime().as_secs())
74 }
75}
76
77#[cfg(feature = "std")]
78impl Now for Timestamp<u128> {
79 type Output = Self;
80
81 fn now() -> Self::Output {
82 Self::new(super::systime().as_millis())
83 }
84}
85
86#[cfg(feature = "chrono")]
87impl Now for Timestamp<i64> {
88 type Output = Self;
89
90 fn now() -> Self::Output {
91 Self::new(chrono::Local::now().timestamp())
92 }
93}
94
95impl<T> AsRef<T> for Timestamp<T> {
96 fn as_ref(&self) -> &T {
97 &self.0
98 }
99}
100
101impl<T> AsMut<T> for Timestamp<T> {
102 fn as_mut(&mut self) -> &mut T {
103 &mut self.0
104 }
105}
106
107impl<T> core::borrow::Borrow<T> for Timestamp<T> {
108 fn borrow(&self) -> &T {
109 &self.0
110 }
111}
112
113impl<T> core::borrow::BorrowMut<T> for Timestamp<T> {
114 fn borrow_mut(&mut self) -> &mut T {
115 &mut self.0
116 }
117}
118
119impl<T> core::ops::Deref for Timestamp<T> {
120 type Target = T;
121
122 fn deref(&self) -> &Self::Target {
123 &self.0
124 }
125}
126
127impl<T> core::ops::DerefMut for Timestamp<T> {
128 fn deref_mut(&mut self) -> &mut Self::Target {
129 &mut self.0
130 }
131}
132
133impl<T> core::fmt::Display for Timestamp<T>
134where
135 T: core::fmt::Display,
136{
137 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
138 write!(f, "{}", self.0)
139 }
140}
141
142impl From<Duration> for Timestamp<u64> {
143 fn from(dur: Duration) -> Self {
144 Self(dur.as_secs())
145 }
146}
147
148impl From<Duration> for Timestamp<u128> {
149 fn from(dur: Duration) -> Self {
150 Self(dur.as_millis())
151 }
152}
153
154#[cfg(feature = "time")]
155impl<Tz> From<chrono::DateTime<Tz>> for Timestamp<i64>
156where
157 Tz: chrono::TimeZone,
158{
159 fn from(ts: chrono::DateTime<Tz>) -> Self {
160 Self(ts.timestamp())
161 }
162}
163
164impl From<Timestamp<u64>> for Duration {
165 fn from(ts: Timestamp<u64>) -> Self {
166 Self::from_secs(*ts)
167 }
168}
169
170impl From<Timestamp<u128>> for Duration {
171 fn from(ts: Timestamp<u128>) -> Self {
172 Self::from_millis(*ts as u64)
173 }
174}