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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::fmt;
use std::ops;
use chrono::TimeZone;
use super::Date;
pub type FixedOffset = chrono::FixedOffset;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[serde(transparent)]
#[repr(transparent)]
pub struct DateTime {
#[serde(with = "friendly_date_time")]
inner: DateTimeImpl,
}
type DateTimeImpl = chrono::DateTime<chrono::FixedOffset>;
impl DateTime {
pub fn now() -> Self {
let d = chrono::Utc::now().with_timezone(&chrono::FixedOffset::east(0));
Self::with_chrono(d)
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(other: &str) -> Option<Self> {
parse_date_time(other).map(|d| Self { inner: d })
}
pub fn with_date(self, other: Date) -> Self {
use chrono::{NaiveDateTime, NaiveTime};
let other = DateTimeImpl::from_utc(
NaiveDateTime::new(other.inner, NaiveTime::from_hms(0, 0, 0)),
*self.inner.offset(),
);
Self::with_chrono(other)
}
pub fn with_timezone(&self, tz: &chrono::FixedOffset) -> Self {
Self::with_chrono(self.inner.with_timezone(tz))
}
pub fn date(&self) -> Date {
Date::with_chrono(self.inner.naive_utc().date())
}
pub fn format<'a>(&self, fmt: &'a str) -> impl fmt::Display + 'a {
self.inner.format(fmt)
}
fn with_chrono(inner: DateTimeImpl) -> Self {
Self { inner }
}
}
impl Default for DateTime {
fn default() -> Self {
let d = chrono::Utc
.timestamp(0, 0)
.with_timezone(&chrono::FixedOffset::east(0));
DateTime::with_chrono(d)
}
}
impl fmt::Display for DateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.format(DATE_TIME_FORMAT).fmt(f)
}
}
impl ops::Deref for DateTime {
type Target = chrono::DateTime<chrono::FixedOffset>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl ops::DerefMut for DateTime {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl chrono::Datelike for DateTime {
#[inline]
fn year(&self) -> i32 {
self.inner.year()
}
#[inline]
fn month(&self) -> u32 {
self.inner.month()
}
#[inline]
fn month0(&self) -> u32 {
self.inner.month0()
}
#[inline]
fn day(&self) -> u32 {
self.inner.day()
}
#[inline]
fn day0(&self) -> u32 {
self.inner.day0()
}
#[inline]
fn ordinal(&self) -> u32 {
self.inner.ordinal()
}
#[inline]
fn ordinal0(&self) -> u32 {
self.inner.ordinal0()
}
#[inline]
fn weekday(&self) -> chrono::Weekday {
self.inner.weekday()
}
#[inline]
fn iso_week(&self) -> chrono::IsoWeek {
self.inner.iso_week()
}
#[inline]
fn with_year(&self, year: i32) -> Option<Self> {
self.inner.with_year(year).map(Self::with_chrono)
}
#[inline]
fn with_month(&self, month: u32) -> Option<Self> {
self.inner.with_month(month).map(Self::with_chrono)
}
#[inline]
fn with_month0(&self, month0: u32) -> Option<Self> {
self.inner.with_month0(month0).map(Self::with_chrono)
}
#[inline]
fn with_day(&self, day: u32) -> Option<Self> {
self.inner.with_day(day).map(Self::with_chrono)
}
#[inline]
fn with_day0(&self, day0: u32) -> Option<Self> {
self.inner.with_day(day0).map(Self::with_chrono)
}
#[inline]
fn with_ordinal(&self, ordinal: u32) -> Option<Self> {
self.inner.with_ordinal(ordinal).map(Self::with_chrono)
}
#[inline]
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self> {
self.inner.with_ordinal0(ordinal0).map(Self::with_chrono)
}
}
impl chrono::Timelike for DateTime {
#[inline]
fn hour(&self) -> u32 {
self.inner.hour()
}
#[inline]
fn minute(&self) -> u32 {
self.inner.minute()
}
#[inline]
fn second(&self) -> u32 {
self.inner.second()
}
#[inline]
fn nanosecond(&self) -> u32 {
self.inner.nanosecond()
}
#[inline]
fn with_hour(&self, hour: u32) -> Option<Self> {
self.inner.with_hour(hour).map(Self::with_chrono)
}
#[inline]
fn with_minute(&self, min: u32) -> Option<Self> {
self.inner.with_minute(min).map(Self::with_chrono)
}
#[inline]
fn with_second(&self, sec: u32) -> Option<Self> {
self.inner.with_second(sec).map(Self::with_chrono)
}
#[inline]
fn with_nanosecond(&self, nano: u32) -> Option<Self> {
self.inner.with_nanosecond(nano).map(Self::with_chrono)
}
}
const DATE_TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S %z";
mod friendly_date_time {
use super::*;
use serde::{self, Deserialize, Deserializer, Serializer};
pub(crate) fn serialize<S>(date: &DateTimeImpl, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = date.to_string();
serializer.serialize_str(&s)
}
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<DateTimeImpl, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
DateTimeImpl::parse_from_str(&s, DATE_TIME_FORMAT).map_err(serde::de::Error::custom)
}
}
fn parse_date_time(s: &str) -> Option<DateTimeImpl> {
match s {
"now" => {
use chrono::offset;
let now = offset::Utc::now();
let now = now.naive_utc();
let now = DateTimeImpl::from_utc(now, offset::FixedOffset::east(0));
Some(now)
}
_ => {
let formats = ["%d %B %Y %H:%M:%S %z", "%Y-%m-%d %H:%M:%S %z"];
formats
.iter()
.filter_map(|f| DateTimeImpl::parse_from_str(s, f).ok())
.next()
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_date_time_empty_is_bad() {
assert!(parse_date_time("").is_none());
}
#[test]
fn parse_date_time_bad() {
assert!(parse_date_time("aaaaa").is_none());
}
#[test]
fn parse_date_time_now() {
assert!(parse_date_time("now").is_some());
}
}