scsys_core/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#[cfg(any(feature = "chrono", feature = "std"))]
17use crate::time::Timestamp;
18#[cfg(feature = "std")]
19use crate::time::utils::systime;
20#[cfg(all(feature = "alloc", feature = "chrono"))]
21use alloc::string::String;
22
23#[cfg(feature = "std")]
24impl Now for u64 {
25    type Output = Timestamp<Self>;
26
27    fn now() -> Self::Output {
28        Timestamp::new(systime().as_secs())
29    }
30}
31
32#[cfg(feature = "std")]
33impl Now for u128 {
34    type Output = Timestamp<Self>;
35
36    fn now() -> Self::Output {
37        Timestamp::new(systime().as_millis())
38    }
39}
40
41#[cfg(feature = "chrono")]
42impl Now for i64 {
43    type Output = Timestamp<Self>;
44
45    fn now() -> Self::Output {
46        Timestamp::new(chrono::Local::now().timestamp())
47    }
48}
49
50#[cfg(all(feature = "alloc", feature = "chrono"))]
51impl Now for String {
52    type Output = Timestamp<Self>;
53
54    fn now() -> Self::Output {
55        Timestamp::new(chrono::Local::now().to_rfc3339())
56    }
57}