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
138
139
140
141
142
143
144
145
146
147
148
use std::ops::{Add, AddAssign};
use crate::*;

// DateTime to chrono stuff

impl TryFrom<DateTime> for chrono::DateTime<chrono::Utc> {
    type Error = ();

    fn try_from(value: DateTime) -> Result<Self, Self::Error> {
        match Self::from_timestamp(value.timestamp().to_unix(), 0) {
            Some(dt) => Ok(dt),
            None => Err(()),
        }
    }
}

impl TryFrom<DateTime> for chrono::NaiveDateTime {
    type Error = ();

    fn try_from(value: DateTime) -> Result<Self, Self::Error> {
        let ts = value.timestamp().to_unix();
        match Self::from_timestamp_opt(ts, 0) {
            Some(dt) => Ok(dt),
            None => Err(()),
        }
    }
}

impl TryFrom<DateTime> for chrono::NaiveDate {
    type Error = ();

    fn try_from(value: DateTime) -> Result<Self, Self::Error> {
        let naive_dt: chrono::NaiveDateTime = value.try_into()?;
        Ok(naive_dt.date())
    }
}

// Chrono stuff to DateTime

impl<Tz: chrono::TimeZone> From<chrono::DateTime<Tz>> for DateTime {
    fn from(value: chrono::DateTime<Tz>) -> Self {
        let ts = value.timestamp();
        let ts = Timestamp::from_unix(ts);
        Self::from_timestamp(ts)
    }
}

impl TryFrom<chrono::NaiveDateTime> for DateTime {
    type Error = ();

    fn try_from(value: chrono::NaiveDateTime) -> Result<Self, Self::Error> {
        let ts = value.timestamp();
        let ts = Timestamp::from_unix(ts);
        Ok(Self::from_timestamp(ts))
    }
}

impl TryFrom<chrono::NaiveDate> for DateTime {
    type Error = ();

    fn try_from(value: chrono::NaiveDate) -> Result<Self, Self::Error> {
        let naive_dt = value.and_hms_opt(0, 0, 0).ok_or(())?;
        let ts = naive_dt.timestamp();
        let ts = Timestamp::from_unix(ts);
        Ok(Self::from_timestamp(ts))
    }
}

impl Add<chrono::Duration> for DateTime {
    type Output = Self;

    fn add(self, rhs: chrono::Duration) -> Self::Output {
        let seconds = self.timestamp().seconds + rhs.num_seconds() * REPUBLICAN_SECONDS_PER_DAY / GREGORIAN_SECONDS_PER_DAY;
        Self::from_timestamp(Timestamp { seconds: seconds })
    }
}

impl AddAssign<chrono::Duration> for DateTime {
    fn add_assign(&mut self, rhs: chrono::Duration) {
        let ts = self.timestamp().seconds + rhs.num_seconds() * REPUBLICAN_SECONDS_PER_DAY / GREGORIAN_SECONDS_PER_DAY;
        *self = Self::from_timestamp(Timestamp { seconds: ts });
    }
}

// Date to chrono stuff

impl TryFrom<Date> for chrono::DateTime<chrono::Utc> {
    type Error = ();

    fn try_from(value: Date) -> Result<Self, Self::Error> {
        match Self::from_timestamp(value.timestamp().to_unix(), 0) {
            Some(dt) => Ok(dt),
            None => Err(()),
        }
    }
}

impl TryFrom<Date> for chrono::NaiveDateTime {
    type Error = ();

    fn try_from(value: Date) -> Result<Self, Self::Error> {
        let ts = value.timestamp().to_unix();
        match Self::from_timestamp_opt(ts, 0) {
            Some(dt) => Ok(dt),
            None => Err(()),
        }
    }
}

impl TryFrom<Date> for chrono::NaiveDate {
    type Error = ();

    fn try_from(value: Date) -> Result<Self, Self::Error> {
        let naive_dt: chrono::NaiveDateTime = value.try_into()?;
        Ok(naive_dt.date())
    }
}

// Chrono stuff to Date

impl<Tz: chrono::TimeZone> From<chrono::DateTime<Tz>> for Date {
    fn from(value: chrono::DateTime<Tz>) -> Self {
        let ts = value.timestamp();
        let ts = Timestamp::from_unix(ts);
        Self::from_timestamp(ts)
    }
}

impl TryFrom<chrono::NaiveDateTime> for Date {
    type Error = ();

    fn try_from(value: chrono::NaiveDateTime) -> Result<Self, Self::Error> {
        let ts = value.timestamp();
        let ts = Timestamp::from_unix(ts);
        Ok(Self::from_timestamp(ts))
    }
}

impl TryFrom<chrono::NaiveDate> for Date {
    type Error = ();

    fn try_from(value: chrono::NaiveDate) -> Result<Self, Self::Error> {
        let naive_dt = value.and_hms_opt(0, 0, 0).ok_or(())?;
        let ts = naive_dt.timestamp();
        let ts = Timestamp::from_unix(ts);
        Ok(Self::from_timestamp(ts))
    }
}