eryon_core/time/
timestamp.rs1pub type Ts = u128;
7
8#[derive(Clone, Copy, Default, Eq, Hash, PartialEq, Ord, PartialOrd)]
9#[cfg_attr(
10 feature = "serde",
11 derive(serde_derive::Deserialize, serde_derive::Serialize)
12)]
13pub struct Timestamp<T = Ts>(pub T);
14
15impl<T> Timestamp<T> {
16 pub fn from_value(index: T) -> Self {
17 Timestamp(index)
18 }
19 pub const fn as_ptr(&self) -> *const T {
21 core::ptr::from_ref(&self.0)
22 }
23 pub fn as_mut_ptr(&mut self) -> *mut T {
25 core::ptr::from_mut(&mut self.0)
26 }
27 #[inline]
29 pub fn into_inner(self) -> T {
30 self.0
31 }
32 pub const fn get(&self) -> &T {
34 &self.0
35 }
36 pub fn get_mut(&mut self) -> &mut T {
38 &mut self.0
39 }
40 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Timestamp<U> {
42 Timestamp(f(self.0))
43 }
44 pub const fn replace(&mut self, index: T) -> T {
46 core::mem::replace(&mut self.0, index)
47 }
48 pub fn set(&mut self, index: T) {
50 self.0 = index;
51 }
52 pub const fn swap(&mut self, other: &mut Self) {
54 core::mem::swap(&mut self.0, &mut other.0)
55 }
56}
57
58impl<T> From<T> for Timestamp<T> {
59 fn from(index: T) -> Self {
60 Timestamp(index)
61 }
62}
63
64impl<T> PartialEq<T> for Timestamp<T>
65where
66 T: PartialEq,
67{
68 fn eq(&self, other: &T) -> bool {
69 &self.0 == other
70 }
71}
72
73impl Timestamp<u64> {
74 #[cfg(feature = "std")]
75 pub fn now_in_secs() -> Self {
76 Self(
77 std::time::SystemTime::now()
78 .duration_since(std::time::UNIX_EPOCH)
79 .unwrap_or_default()
80 .as_secs(),
81 )
82 }
83 pub fn as_millis(&self) -> u128 {
85 self.0 as u128 * 1000
86 }
87}
88
89impl Timestamp<u128> {
90 #[cfg(feature = "std")]
91 pub fn now() -> Self {
92 Self(
93 std::time::SystemTime::now()
94 .duration_since(std::time::UNIX_EPOCH)
95 .unwrap_or_default()
96 .as_millis(),
97 )
98 }
99 #[cfg(feature = "std")]
100 pub fn now_in_millis() -> Self {
101 Self(super::utils::std_time())
102 }
103}
104
105impl<T> core::convert::AsRef<T> for Timestamp<T> {
106 fn as_ref(&self) -> &T {
107 &self.0
108 }
109}
110
111impl<T> core::convert::AsMut<T> for Timestamp<T> {
112 fn as_mut(&mut self) -> &mut T {
113 &mut self.0
114 }
115}
116
117impl<T> core::borrow::Borrow<T> for Timestamp<T> {
118 fn borrow(&self) -> &T {
119 &self.0
120 }
121}
122
123impl<T> core::borrow::BorrowMut<T> for Timestamp<T> {
124 fn borrow_mut(&mut self) -> &mut T {
125 &mut self.0
126 }
127}
128
129impl<T> core::ops::Deref for Timestamp<T> {
130 type Target = T;
131
132 fn deref(&self) -> &Self::Target {
133 &self.0
134 }
135}
136
137impl<T> core::ops::DerefMut for Timestamp<T> {
138 fn deref_mut(&mut self) -> &mut Self::Target {
139 &mut self.0
140 }
141}
142
143impl<T> core::ops::Neg for Timestamp<T>
144where
145 T: core::ops::Neg,
146{
147 type Output = Timestamp<<T as core::ops::Neg>::Output>;
148
149 fn neg(self) -> Self::Output {
150 Timestamp(-self.0)
151 }
152}
153
154impl<T> core::ops::Not for Timestamp<T>
155where
156 T: core::ops::Not,
157{
158 type Output = Timestamp<<T as core::ops::Not>::Output>;
159
160 fn not(self) -> Self::Output {
161 Timestamp(!self.0)
162 }
163}
164
165impl<T> num::One for Timestamp<T>
166where
167 T: num::One,
168{
169 fn one() -> Self {
170 Timestamp(T::one())
171 }
172}
173
174impl<T> num::Zero for Timestamp<T>
175where
176 T: num::Zero,
177{
178 fn zero() -> Self {
179 Timestamp(T::zero())
180 }
181
182 fn is_zero(&self) -> bool {
183 self.0.is_zero()
184 }
185}
186
187impl<T> num::Num for Timestamp<T>
188where
189 T: num::Num,
190{
191 type FromStrRadixErr = T::FromStrRadixErr;
192
193 fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
194 T::from_str_radix(str, radix).map(Timestamp)
195 }
196}
197
198macro_rules! impl_fmt {
199 ($($trait:ident),* $(,)?) => {
200 $(impl<T: core::fmt::$trait> core::fmt::$trait for Timestamp<T> {
201 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
202 core::fmt::$trait::fmt(&self.0, f)
203 }
204 })*
205 };
206}
207
208macro_rules! impl_bin_op {
209 (@impl $trait:ident::$method:ident) => {
210 impl<A, B, C> core::ops::$trait<Timestamp<B>> for Timestamp<A> where A: core::ops::$trait<B, Output = C>{
211 type Output = Timestamp<C>;
212
213 fn $method(self, rhs: Timestamp<B>) -> Self::Output {
214 Timestamp(core::ops::$trait::$method(self.0, rhs.0))
215 }
216 }
217 };
218
219 ($($trait:ident::$method:ident),* $(,)?) => {
220 $(impl_bin_op!(@impl $trait::$method);)*
221 };
222}
223
224macro_rules! impl_assign_op {
225 (@impl $trait:ident::$method:ident) => {
226 impl<A, B> core::ops::$trait<B> for Timestamp<A> where A: core::ops::$trait<B> {
227 fn $method(&mut self, rhs: B) {
228 core::ops::$trait::$method(&mut self.0, rhs)
229 }
230 }
231 };
232
233 ($($trait:ident::$method:ident),* $(,)?) => {
234 $(impl_assign_op!(@impl $trait::$method);)*
235 };
236}
237
238impl_assign_op! {
239 AddAssign::add_assign,
240 SubAssign::sub_assign,
241 MulAssign::mul_assign,
242 DivAssign::div_assign,
243 RemAssign::rem_assign,
244 BitAndAssign::bitand_assign,
245 BitOrAssign::bitor_assign,
246 BitXorAssign::bitxor_assign,
247 ShlAssign::shl_assign,
248 ShrAssign::shr_assign,
249}
250
251impl_bin_op! {
252 Add::add,
253 Sub::sub,
254 Mul::mul,
255 Div::div,
256 Rem::rem,
257 BitAnd::bitand,
258 BitOr::bitor,
259 BitXor::bitxor,
260 Shl::shl,
261 Shr::shr,
262}
263
264impl_fmt! {
265 Binary,
266 Debug,
267 Display,
268 LowerExp,
269 LowerHex,
270 Octal,
271 Pointer,
272 UpperExp,
273 UpperHex,
274}