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
use chrono::{DateTime, Utc};
use std::time::SystemTime;
pub type ChronoTime = DateTime<Utc>;
#[derive(Clone, Copy, Serialize)]
#[serde(untagged)]
pub enum Date {
System(SystemTime),
Chrono(ChronoTime),
}
impl std::fmt::Debug for Date {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::System(value) => value.fmt(f),
Self::Chrono(value) => value.fmt(f),
}
}
}
impl From<SystemTime> for Date {
fn from(value: SystemTime) -> Self {
Self::System(value)
}
}
impl From<ChronoTime> for Date {
fn from(value: ChronoTime) -> Self {
Self::Chrono(value)
}
}
impl From<&SystemTime> for Date {
fn from(value: &SystemTime) -> Self {
Self::System(*value)
}
}
impl From<&ChronoTime> for Date {
fn from(value: &ChronoTime) -> Self {
Self::Chrono(*value)
}
}
impl PartialEq for Date {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Date::System(s), Date::System(o)) => s.eq(o),
(Date::System(s), Date::Chrono(o)) => ChronoTime::from(*s).eq(o),
(Date::Chrono(s), Date::System(o)) => s.eq(&ChronoTime::from(*o)),
(Date::Chrono(s), Date::Chrono(o)) => s.eq(o),
}
}
}
impl PartialEq<SystemTime> for Date {
fn eq(&self, other: &SystemTime) -> bool {
match self {
Self::System(s) => s.eq(other),
Self::Chrono(s) => s.eq(&ChronoTime::from(*other)),
}
}
}
impl PartialEq<ChronoTime> for Date {
fn eq(&self, other: &ChronoTime) -> bool {
match self {
Self::Chrono(s) => s.eq(other),
Self::System(s) => ChronoTime::from(*s).eq(other),
}
}
}
impl Eq for Date {}
impl PartialOrd for Date {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialOrd<SystemTime> for Date {
fn partial_cmp(&self, other: &SystemTime) -> Option<std::cmp::Ordering> {
match self {
Date::System(s) => s.partial_cmp(other),
Date::Chrono(s) => s.partial_cmp(&ChronoTime::from(*other)),
}
}
}
impl PartialOrd<ChronoTime> for Date {
fn partial_cmp(&self, other: &ChronoTime) -> Option<std::cmp::Ordering> {
match self {
Self::Chrono(s) => s.partial_cmp(other),
Self::System(s) => ChronoTime::from(*s).partial_cmp(other),
}
}
}
impl Ord for Date {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(Date::System(s), Date::System(o)) => s.cmp(o),
(Date::System(s), Date::Chrono(o)) => ChronoTime::from(*s).cmp(o),
(Date::Chrono(s), Date::System(o)) => s.cmp(&ChronoTime::from(*o)),
(Date::Chrono(s), Date::Chrono(o)) => s.cmp(o),
}
}
}