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