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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    Appellation: timestamp <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::Temporal;
use crate::{BsonDateTime, ChronoDateTime, DefaultTimezone};
use serde::{Deserialize, Serialize};
use strum::{Display, VariantNames};

/// Timestamp implements a host of useful utilities for stamping data
#[derive(
    Clone,
    Debug,
    Deserialize,
    Display,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    Serialize,
    VariantNames,
)]
#[strum(serialize_all = "snake_case")]
pub enum Timestamp {
    Datetime(bson::DateTime),
    Rfc3339(String),
    Ts(i64),
}

impl Timestamp {
    pub fn new() -> Self {
        Self::Ts(DefaultTimezone::now().timestamp())
    }
    pub fn now() -> ChronoDateTime {
        chrono::Utc::now()
    }
    pub fn chrono_to_bson(&self, data: ChronoDateTime) -> bson::DateTime {
        bson::DateTime::from_chrono(data)
    }
    pub fn bson_datetime() -> bson::DateTime {
        bson::DateTime::from_chrono(Self::now())
    }
    pub fn datetime() -> ChronoDateTime {
        chrono::Utc::now()
    }
    pub fn gen_rfc3339() -> String {
        chrono::Utc::now().to_rfc3339()
    }
    pub fn rfc3339() -> Self {
        Self::Rfc3339(chrono::Utc::now().to_rfc3339())
    }
}

impl Temporal for Timestamp {
    type Timestamp = i64;

    fn timestamp(&self) -> Self::Timestamp {
        match self.clone() {
            Self::Rfc3339(ts) => chrono::DateTime::parse_from_rfc3339(ts.as_str())
                .unwrap()
                .timestamp(),
            Self::Ts(ts) => ts,
            Timestamp::Datetime(ts) => ts.to_chrono().timestamp(),
        }
    }
}

impl Default for Timestamp {
    fn default() -> Self {
        Self::Rfc3339(chrono::Utc::now().to_rfc3339())
    }
}

impl From<i64> for Timestamp {
    fn from(data: i64) -> Self {
        Self::Ts(data)
    }
}

impl From<BsonDateTime> for Timestamp {
    fn from(ts: BsonDateTime) -> Self {
        Self::Datetime(ts)
    }
}

impl From<chrono::DateTime<chrono::Utc>> for Timestamp {
    fn from(ts: chrono::DateTime<chrono::Utc>) -> Self {
        Self::Rfc3339(ts.to_rfc3339())
    }
}

impl TryFrom<String> for Timestamp {
    type Error = crate::BoxError;

    fn try_from(data: String) -> Result<Self, Self::Error> {
        Self::try_from(data.as_str())
    }
}

impl TryFrom<&str> for Timestamp {
    type Error = crate::BoxError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let dt = chrono::DateTime::parse_from_rfc3339(value)?;
        Ok(Self::Ts(dt.timestamp()))
    }
}

impl From<Timestamp> for i64 {
    fn from(data: Timestamp) -> i64 {
        data.timestamp()
    }
}

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

    #[test]
    fn test_timestamp() {
        let boundary = Timestamp::now();
        let a = Timestamp::from(boundary.clone());
        let b = Timestamp::from(boundary.timestamp());
        assert_ne!(a, b);
        assert_eq!(a.timestamp(), b.timestamp());
    }

    #[test]
    fn test_timestamp_default() {
        let m = Timestamp::default();
        let a = m.clone();
        let b: i64 = m.clone().into();
        assert_eq!(&m, &a);
        assert_eq!(a.timestamp(), b);
    }
}