scsys_time/traits/
now.rs

1/*
2    appellation: now <module>
3    authors: @FL03
4*/
5
6/// The [`Now`] trait provides a common creation routines for all datetime implementations.
7pub trait Now {
8    type Output;
9
10    fn now() -> Self::Output;
11}
12
13/*
14 ************* Implementations *************
15*/
16
17#[cfg(all(feature = "alloc", feature = "chrono"))]
18mod impl_alloc {
19    use super::Now;
20    use crate::Timestamp;
21    use alloc::string::String;
22
23    impl Now for String {
24        type Output = Timestamp<Self>;
25
26        fn now() -> Self::Output {
27            Timestamp::new(chrono::Local::now().to_rfc3339())
28        }
29    }
30}
31
32#[cfg(feature = "chrono")]
33mod impl_chrono {
34    use super::Now;
35    use crate::timestamp::Timestamp;
36
37    impl Now for i64 {
38        type Output = Timestamp<Self>;
39
40        fn now() -> Self::Output {
41            Timestamp::new(chrono::Local::now().timestamp())
42        }
43    }
44}
45
46#[cfg(feature = "std")]
47mod impl_std {
48    use super::Now;
49    use crate::timestamp::Timestamp;
50    use crate::utils::systime;
51
52    impl Now for u128 {
53        type Output = Timestamp<Self>;
54
55        fn now() -> Self::Output {
56            Timestamp::new(systime().as_millis())
57        }
58    }
59
60    impl Now for u64 {
61        type Output = Timestamp<Self>;
62
63        fn now() -> Self::Output {
64            Timestamp::new(systime().as_secs())
65        }
66    }
67}