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
use core::{
    cmp::Ordering,
    fmt::{self, Display, Formatter},
    str::FromStr,
};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

#[derive(Clone, Debug, PartialEq)]
pub struct TimeStamp(String, OffsetDateTime);

impl From<OffsetDateTime> for TimeStamp {
    fn from(t: OffsetDateTime) -> Self {
        Self(t.format(&Rfc3339).expect("Rfc3339 formatting works"), t)
    }
}

impl AsRef<OffsetDateTime> for TimeStamp {
    fn as_ref(&self) -> &OffsetDateTime {
        &self.1
    }
}

impl Display for TimeStamp {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "{}", &self.0)
    }
}

impl FromStr for TimeStamp {
    type Err = time::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(s.into(), OffsetDateTime::parse(s, &Rfc3339)?))
    }
}

impl PartialEq<OffsetDateTime> for TimeStamp {
    fn eq(&self, other: &OffsetDateTime) -> bool {
        &self.1 == other
    }
}

impl PartialOrd<OffsetDateTime> for TimeStamp {
    fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> {
        self.1.partial_cmp(other)
    }
}