1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    Appellation: times <module>
    Contrib: FL03 <jo3mccain@icloud.com>
    Description: ... Summary ...
*/
pub use self::{specs::*, timestamp::*, utils::*};

pub(crate) mod timestamp;

pub(crate) mod specs {
    use crate::{chrono_into_bson, ChronoDateTime};
    use chrono::Utc;

    pub trait TemporalExt {}

    /// Interface for time-related data-structures
    pub trait Temporal {
        fn chrono_to_bson(&self, data: ChronoDateTime) -> bson::DateTime {
            chrono_into_bson::<Utc>(data)
        }
        fn bson_datetime() -> bson::DateTime {
            chrono_into_bson::<chrono::Utc>(chrono::Utc::now())
        }
        fn datetime() -> ChronoDateTime {
            chrono::Utc::now()
        }
        fn gen_rfc3339() -> String {
            chrono::Utc::now().to_rfc3339()
        }
        fn ts() -> i64 {
            chrono::Utc::now().timestamp()
        }
        fn timestamp(&self) -> i64; // Recall an objects time of creation
    }
}

pub(crate) mod utils {
    use chrono::{DateTime, TimeZone, Utc};

    pub fn chrono_datetime_now() -> DateTime<Utc> {
        Utc::now()
    }

    pub fn ts_rfc3339() -> String {
        Utc::now().to_rfc3339()
    }

    pub fn chrono_into_bson<T: TimeZone>(data: DateTime<T>) -> bson::DateTime {
        bson::DateTime::from_chrono(data)
    }

    pub fn timestamp() -> i64 {
        Utc::now().timestamp()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_timestamp() {
        let boundary = Timestamp::now();
        let a = Timestamp::from(&boundary);
        let b = Timestamp::try_from(boundary.to_rfc3339()).ok().unwrap();
        assert_eq!(&a, &b);
    }
}