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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::datetime::constant::{
self, DAYS_PER_YEAR, EPOCH_UNIX, MICROSECONDS_PER_SECOND, MINUTES_PER_HOUR, SECONDS_PER_HOUR,
YEARS_PER_LEAP, YEAR_MAX,
};
use super::{
constant::{Month, SECONDS_PER_DAY},
Timestamp,
};
use serde::{Serialize, Serializer};
use std::fmt::{Display, Error as FmtError, Formatter, Result as FmtResult};
#[derive(Debug)]
pub struct TimestampIso8601Display {
timestamp: Timestamp,
with_microseconds: bool,
}
impl TimestampIso8601Display {
pub(super) const fn new(timestamp: Timestamp) -> Self {
Self {
timestamp,
with_microseconds: true,
}
}
pub const fn get(self) -> Timestamp {
self.timestamp
}
pub const fn with_microseconds(mut self, with_microseconds: bool) -> Self {
self.with_microseconds = with_microseconds;
self
}
}
impl Display for TimestampIso8601Display {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
pub const APPROXIMATE_MAX_SECONDS: u64 =
(YEAR_MAX as u64 - EPOCH_UNIX as u64) * DAYS_PER_YEAR as u64 * SECONDS_PER_DAY;
let total_seconds = self.timestamp.as_secs();
let microseconds = self.timestamp.as_micros() % MICROSECONDS_PER_SECOND;
if total_seconds >= APPROXIMATE_MAX_SECONDS {
return Err(FmtError);
}
let seconds = total_seconds % SECONDS_PER_DAY;
let Date { day, month, year } = date(total_seconds);
Display::fmt(&(year / 1000), f)?;
Display::fmt(&(year / 100 % 10), f)?;
Display::fmt(&(year / 10 % 10), f)?;
Display::fmt(&(year % 10), f)?;
f.write_str("-")?;
Display::fmt(&(month / 10), f)?;
Display::fmt(&(month % 10), f)?;
f.write_str("-")?;
Display::fmt(&(day / 10), f)?;
Display::fmt(&(day % 10), f)?;
f.write_str("T")?;
Display::fmt(&(seconds / u64::from(SECONDS_PER_HOUR) / 10), f)?;
Display::fmt(&(seconds / u64::from(SECONDS_PER_HOUR) % 10), f)?;
f.write_str(":")?;
Display::fmt(&(seconds / u64::from(MINUTES_PER_HOUR) / 10 % 6), f)?;
Display::fmt(&(seconds / u64::from(MINUTES_PER_HOUR) % 10), f)?;
f.write_str(":")?;
Display::fmt(&(seconds / 10 % 6), f)?;
Display::fmt(&(seconds % 10), f)?;
if self.with_microseconds {
f.write_str(".")?;
Display::fmt(&(microseconds / 100_000), f)?;
Display::fmt(&(microseconds / 10_000 % 10), f)?;
Display::fmt(&(microseconds / 1_000 % 10), f)?;
Display::fmt(&(microseconds / 100 % 10), f)?;
Display::fmt(&(microseconds / 10 % 10), f)?;
Display::fmt(&(microseconds % 10), f)?;
}
f.write_str("+00:00")
}
}
impl Serialize for TimestampIso8601Display {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
struct Date {
day: u64,
month: u8,
year: u64,
}
const fn date(total_seconds: u64) -> Date {
pub const DAYS_PER_LEAP: u16 = (DAYS_PER_YEAR * YEARS_PER_LEAP as u16) + 1;
pub const LEAP_EPOCH_YEAR: u64 = 2008;
pub const LEAP_EPOCH: u64 = 13880;
let days_per_leap = DAYS_PER_LEAP as u64;
let days_per_year = DAYS_PER_YEAR as u64;
let mut day = (total_seconds / SECONDS_PER_DAY) - LEAP_EPOCH;
let leaps = day / days_per_leap;
day -= leaps * days_per_leap;
let years_after_leap = {
let value = day / days_per_year;
if value >= YEARS_PER_LEAP as u64 {
YEARS_PER_LEAP as u64 - 1
} else {
value
}
};
day -= years_after_leap * days_per_year;
let mut year = LEAP_EPOCH_YEAR + years_after_leap + (YEARS_PER_LEAP as u64 * leaps);
let is_leap_year = constant::is_leap_year(year);
let mut month = 0;
loop {
#[allow(clippy::option_if_let_else)]
let named_month = if let Some(named_month) = Month::new(month) {
named_month
} else {
month = Month::January as u8;
year += 1;
Month::January
};
let month_days = named_month.days(is_leap_year) as u64;
if day < month_days {
break;
}
day -= month_days;
month += 1;
}
if is_leap_year && month < Month::March as u8 {
day += 1;
}
day += 1;
if let Some(named_month) = Month::new(month) {
let month_days = named_month.days(is_leap_year) as u64;
if day > month_days {
month += 1;
day -= month_days;
}
if month as u8 > Month::December as u8 {
month = Month::January as u8;
year += 1;
}
}
Date {
day,
month: month + 1,
year,
}
}
#[cfg(test)]
mod tests {
use crate::datetime::constant::{YEAR_MAX, YEAR_MIN};
use super::{
super::{Timestamp, TimestampParseError},
TimestampIso8601Display,
};
use serde::Serialize;
use static_assertions::assert_impl_all;
use std::{fmt::Debug, str::FromStr};
assert_impl_all!(TimestampIso8601Display: Debug, Send, Serialize, Sync);
#[test]
fn test_display() {
const LONG: &str = "2020-02-02T02:02:02.020000+00:00";
const SHORT: &str = "2020-02-02T02:02:02+00:00";
const TIME: u64 = 1_580_608_922_020_000;
let mut formatter = Timestamp::from_micros(TIME).expect("non zero").iso_8601();
assert_eq!(LONG, formatter.to_string());
formatter = formatter.with_microseconds(true);
assert_eq!(LONG, formatter.to_string());
formatter = formatter.with_microseconds(false);
assert_eq!(SHORT, formatter.to_string());
}
#[test]
fn test_century() -> Result<(), TimestampParseError> {
for year in YEAR_MIN..YEAR_MAX {
let input = format!("{}-02-01T02:02:02.000000+00:00", year);
let timestamp = Timestamp::from_str(&input)?;
assert_eq!(input, timestamp.iso_8601().to_string());
}
Ok(())
}
}