scsys_core/time/
mod.rs

1/*
2    Appellation: time <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5//! # Time
6//!
7//! The `time` module provides a set of utilities for working with time and timestamps.
8#[doc(inline)]
9#[cfg(feature = "std")]
10pub use self::utils::{std_time, systime};
11#[doc(inline)]
12pub use self::{timestamp::Timestamp, types::prelude::*};
13/// this module implements the [`Timestamp`] type
14pub mod timestamp;
15/// this module contains various implementations used to support `time` related features
16pub mod types {
17    #[doc(inline)]
18    pub use self::prelude::*;
19
20    pub mod datetime;
21    pub mod epoch;
22
23    pub(crate) mod prelude {
24        #[doc(inline)]
25        pub use super::datetime::*;
26        #[doc(inline)]
27        pub use super::epoch::*;
28    }
29}
30
31pub(crate) mod prelude {
32    #[doc(inline)]
33    pub use super::timestamp::*;
34    #[doc(inline)]
35    pub use super::types::prelude::*;
36    #[cfg(feature = "std")]
37    pub use super::utils::{std_time, systime};
38    #[doc(inline)]
39    pub use super::{Now, RawTimestamp};
40}
41
42pub(crate) mod utils {
43    /// [systime] is a utilitarian function that returns the current system time in milliseconds.
44    #[cfg(feature = "std")]
45    #[inline]
46    pub fn systime() -> core::time::Duration {
47        std::time::SystemTime::now()
48            .duration_since(std::time::UNIX_EPOCH)
49            .unwrap()
50    }
51    /// [systime] is a utilitarian function that returns the current system time in milliseconds.
52    #[cfg(feature = "std")]
53    #[inline]
54    pub fn std_time() -> u128 {
55        systime().as_millis()
56    }
57}
58
59/// The [`Now`] trait provides a common creation routines for all datetime implementations.
60pub trait Now {
61    type Output;
62
63    fn now() -> Self::Output;
64}
65
66/// a private trait used to mark types capable of being uses as a basetype for a [`Timestamp`].
67pub trait RawTimestamp {
68    private!();
69}
70
71/*
72 ************* Implementations *************
73*/
74macro_rules! impl_raw_timestamp {
75    ($($t:ty),* $(,)?) => {
76        $(
77            impl_raw_timestamp!(@impl $t);
78        )*
79    };
80    (@impl $T:ty) => {
81        impl RawTimestamp for $T {
82            seal!();
83        }
84    };
85}
86
87impl_raw_timestamp! {
88    u64,
89    u128,
90    i64,
91}
92
93impl RawTimestamp for str {
94    seal!();
95}
96
97#[cfg(feature = "alloc")]
98impl RawTimestamp for alloc::string::String {
99    seal!();
100}
101
102#[cfg(feature = "std")]
103impl Now for u64 {
104    type Output = Timestamp<Self>;
105
106    fn now() -> Self::Output {
107        Timestamp::new(utils::systime().as_secs())
108    }
109}
110
111#[cfg(feature = "std")]
112impl Now for u128 {
113    type Output = Timestamp<Self>;
114
115    fn now() -> Self::Output {
116        Timestamp::new(utils::systime().as_millis())
117    }
118}
119
120#[cfg(feature = "chrono")]
121impl Now for i64 {
122    type Output = Timestamp<Self>;
123
124    fn now() -> Self::Output {
125        Timestamp::new(chrono::Local::now().timestamp())
126    }
127}
128
129#[cfg(all(feature = "alloc", feature = "chrono"))]
130impl Now for alloc::string::String {
131    type Output = Timestamp<Self>;
132
133    fn now() -> Self::Output {
134        Timestamp::new(chrono::Local::now().to_rfc3339())
135    }
136}