scsys_core/time/impls/
impl_timestamp.rs

1/*
2    appellation: impl_timestamp <module>
3    authors: @FL03
4*/
5use crate::time::{RawTimestamp, Timestamp};
6
7impl<T: RawTimestamp> AsRef<T> for Timestamp<T> {
8    fn as_ref(&self) -> &T {
9        self.get()
10    }
11}
12
13impl<T: RawTimestamp> AsMut<T> for Timestamp<T> {
14    fn as_mut(&mut self) -> &mut T {
15        self.get_mut()
16    }
17}
18
19impl<T: RawTimestamp> core::borrow::Borrow<T> for Timestamp<T> {
20    fn borrow(&self) -> &T {
21        self.get()
22    }
23}
24
25impl<T: RawTimestamp> core::borrow::BorrowMut<T> for Timestamp<T> {
26    fn borrow_mut(&mut self) -> &mut T {
27        self.get_mut()
28    }
29}
30
31impl<T: RawTimestamp> core::ops::Deref for Timestamp<T> {
32    type Target = T;
33
34    fn deref(&self) -> &Self::Target {
35        self.get()
36    }
37}
38
39impl<T: RawTimestamp> core::ops::DerefMut for Timestamp<T> {
40    fn deref_mut(&mut self) -> &mut Self::Target {
41        self.get_mut()
42    }
43}
44
45crate::fmt_wrapper! {
46    Timestamp<T>(
47        Binary,
48        Octal,
49        LowerHex,
50        UpperHex,
51        Display,
52        Debug,
53        LowerExp,
54        UpperExp,
55        Pointer,
56    )
57}
58
59impl From<core::time::Duration> for Timestamp<u64> {
60    fn from(dur: core::time::Duration) -> Self {
61        Self(dur.as_secs())
62    }
63}
64
65impl From<core::time::Duration> for Timestamp<u128> {
66    fn from(dur: core::time::Duration) -> Self {
67        Self(dur.as_millis())
68    }
69}
70
71impl From<Timestamp<u64>> for core::time::Duration {
72    fn from(ts: Timestamp<u64>) -> Self {
73        Self::from_secs(*ts)
74    }
75}
76
77impl From<Timestamp<u128>> for core::time::Duration {
78    fn from(ts: Timestamp<u128>) -> Self {
79        Self::from_millis(*ts as u64)
80    }
81}
82
83#[cfg(feature = "chrono")]
84impl<Tz> From<chrono::DateTime<Tz>> for Timestamp<i64>
85where
86    Tz: chrono::TimeZone,
87{
88    fn from(ts: chrono::DateTime<Tz>) -> Self {
89        Self(ts.timestamp())
90    }
91}