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
//! Display implementation for formatting a [`Timestamp`].

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};

/// Display implementation to format a [`Timestamp`] in an ISO 8601 format.
///
/// Timestamps up to and including the year 2038 are supported.
///
/// # Examples
///
/// Format a timestamp as an ISO 8601 datetime both with microseconds:
///
/// ```
/// # fn foo() -> Option<()> {
/// use twilight_model::datetime::Timestamp;
///
/// let timestamp = Timestamp::from_micros(1_628_594_197_020_000)?;
/// assert_eq!(
///     "2021-08-10T11:16:37.020000+00:00",
///     timestamp.iso_8601().to_string(),
/// );
/// # None }
/// ```
#[derive(Debug)]
pub struct TimestampIso8601Display {
    /// Timestamp with the time stored as a Unix timestamp.
    timestamp: Timestamp,
    /// Whether to format the timestamp with microseconds included.
    with_microseconds: bool,
}

impl TimestampIso8601Display {
    /// Create a new ISO 8601 display formatter for a timestamp.
    pub(super) const fn new(timestamp: Timestamp) -> Self {
        Self {
            timestamp,
            with_microseconds: true,
        }
    }

    /// Get the inner timestamp.
    pub const fn get(self) -> Timestamp {
        self.timestamp
    }

    /// Whether to format the timestamp with microseconds.
    ///
    /// The ISO 8601 display formatter formats with microseconds by default.
    ///
    /// # Examples
    ///
    /// Format a timestamp with microseconds:
    ///
    /// ```
    /// # fn foo() -> Option<()> {
    /// use twilight_model::datetime::Timestamp;
    ///
    /// let timestamp = Timestamp::from_micros(1_628_594_197_020_000)?;
    /// let formatter = timestamp.iso_8601().with_microseconds(true);
    ///
    /// assert_eq!("2021-08-10T11:16:37.020000+00:00", formatter.to_string());
    /// # None }
    /// ```
    ///
    /// Format a timestamp without microseconds:
    ///
    /// ```
    /// # fn foo() -> Option<()> {
    /// use twilight_model::datetime::Timestamp;
    ///
    /// let timestamp = Timestamp::from_micros(1_628_594_197_020_000)?;
    /// let formatter = timestamp.iso_8601().with_microseconds(false);
    ///
    /// assert_eq!("2021-08-10T11:16:37+00:00", formatter.to_string());
    /// # None }
    /// ```
    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 {
        /// Approximate number of seconds before we're too far in the future
        /// to care.
        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);

        // Years.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //       |----|
        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("-")?;

        // Months.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //            |--|
        Display::fmt(&(month / 10), f)?;
        Display::fmt(&(month % 10), f)?;
        f.write_str("-")?;

        // Days.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //               |--|
        Display::fmt(&(day / 10), f)?;
        Display::fmt(&(day % 10), f)?;

        // Time designator.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //                 |-|
        f.write_str("T")?;

        // Hours.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //                  |--|
        Display::fmt(&(seconds / u64::from(SECONDS_PER_HOUR) / 10), f)?;
        Display::fmt(&(seconds / u64::from(SECONDS_PER_HOUR) % 10), f)?;
        f.write_str(":")?;

        // Minutes.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //                     |--|
        Display::fmt(&(seconds / u64::from(MINUTES_PER_HOUR) / 10 % 6), f)?;
        Display::fmt(&(seconds / u64::from(MINUTES_PER_HOUR) % 10), f)?;
        f.write_str(":")?;

        // Seconds.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //                        |--|
        Display::fmt(&(seconds / 10 % 6), f)?;
        Display::fmt(&(seconds % 10), f)?;

        if self.with_microseconds {
            // Subsecond designator.
            //
            // Input: 2021-01-01T01:01:01.010000+00:00
            //                          |-|
            f.write_str(".")?;

            // Microseconds.
            //
            // Input: 2021-01-01T01:01:01.010000+00:00
            //                           |------|
            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)?;
        }

        // Finish it all off.
        //
        // Input: 2021-01-01T01:01:01.010000+00:00
        //                                 |-----|
        //
        // The API doesn't operate in offsets other than +00:00, so we can just
        // fill that in.
        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)
    }
}

/// Parsed date of the timestamp.
struct Date {
    /// Day in the month.
    day: u64,
    /// Month in the year.
    month: u8,
    /// Year.
    year: u64,
}

/// Determine the date of a timestamp based on the total number of seconds
/// within it.
const fn date(total_seconds: u64) -> Date {
    /// Number of days between each leap day, exclusively from one and
    /// inclusive to the other.
    pub const DAYS_PER_LEAP: u16 = (DAYS_PER_YEAR * YEARS_PER_LEAP as u16) + 1;

    /// Closest leap year before the Discord epoch.
    pub const LEAP_EPOCH_YEAR: u64 = 2008;

    /// Closest date before the Discord epoch that is just after a leap day.
    ///
    /// This is equivalent to March 1st, [`LEAP_BEFORE_EPOCH`].
    pub const LEAP_EPOCH: u64 = 13880;

    // Convert the constants to u64s for simple repeated use.
    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;

    // Number of years since the most recent 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 {
        // `const`: Clippy suggests using `Option::map_or` which isn't const.
        #[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();

        // Default formatter should be with microseconds.
        assert_eq!(LONG, formatter.to_string());

        // Now with explicitly setting it to format with microseconds.
        formatter = formatter.with_microseconds(true);
        assert_eq!(LONG, formatter.to_string());

        // And now without microseconds.
        formatter = formatter.with_microseconds(false);
        assert_eq!(SHORT, formatter.to_string());
    }

    /// Test that the years we care about properly parse.
    #[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(())
    }
}