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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::MILLIS_PER_DAY;

/// Conversions from/to number of days since the unix epoch.
/// Ported from <https://github.com/ThreeTen/threetenbp/blob/master/src/main/java/org/threeten/bp/LocalDate.java>
/// Original code has the following license:
/*
 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  * Neither the name of JSR-310 nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

const SECONDS_PER_DAY: i32 = 86400;
const DAYS_PER_CYCLE: i32 = 146097;
const DAYS_0000_TO_1970: i32 = (DAYS_PER_CYCLE * 5) - (30 * 365 + 7);

const SUPPORT_NEGATIVE_YEAR: bool = false;

// stored as i32 instead of smaller types in order to access via vectorized gather instructions
static DAYS_PER_MONTH: [[i32; 12]; 2] = [
    [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
    [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
];

/// branchless calculation of whether the given year is a leap year
#[inline]
fn is_leap_year(year: i32) -> bool {
    ((year % 4) == 0) & ((year % 100) != 0) | ((year % 400) == 0)
}

#[inline]
fn days_per_month(year: i32, zero_based_month: i32) -> i32 {
    let is_leap = is_leap_year(year);
    let is_feb = zero_based_month == 1;
    let mut days = 30 + ((zero_based_month % 2) != (zero_based_month <= 6) as i32) as i32;
    days -= (2 - is_leap as i32  ) * (is_feb as i32);
    days
}

/// A date represented as the number of days since the unix epoch 1970-01-01.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct EpochDays(i32);

impl EpochDays {
    #[inline]
    pub fn new(epoch_days: i32) -> Self {
        Self(epoch_days)
    }

    #[inline]
    pub fn days(&self) -> i32 {
        self.0
    }

    /// Convert a date to the number of days since the unix epoch 1970-01-01.
    /// See https://github.com/ThreeTen/threetenbp/blob/master/src/main/java/org/threeten/bp/LocalDate.java#L1634
    #[inline]
    pub fn from_ymd(year: i32, month: i32, day: i32) -> Self {
        let y = year;
        let m = month;
        let mut total = 365 * y;

        total += if y < 0 && SUPPORT_NEGATIVE_YEAR {
            -(y / -4 - y / -100 + y / -400)
        } else {
            (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400
        };

        total += ((367 * m - 362) / 12);
        total += day - 1;

        total -= 0_i32.wrapping_sub((m > 2) as i32) & (1 + (!is_leap_year(year) as i32));

        Self(total - DAYS_0000_TO_1970)
    }

    /// Convert the number of days since the unix epoch into a (year, month, day) tuple.
    /// See https://github.com/ThreeTen/threetenbp/blob/master/src/main/java/org/threeten/bp/LocalDate.java#L281
    /// The resulting month and day values are 1-based.
    #[inline]
    pub fn to_ymd(&self) -> (i32, i32, i32) {
        let epoch_days = self.0;
        let mut zero_day = epoch_days + DAYS_0000_TO_1970;
        // find the march-based year
        zero_day -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle
        let mut adjust = 0;
        if zero_day < 0 && SUPPORT_NEGATIVE_YEAR {
            // adjust negative years to positive for calculation
            let adjust_cycles = (zero_day + 1) / DAYS_PER_CYCLE - 1;
            adjust = adjust_cycles * 400;
            zero_day += -adjust_cycles * DAYS_PER_CYCLE;
        }
        let mut year_est = (400 * zero_day + 591) / DAYS_PER_CYCLE;

        if !SUPPORT_NEGATIVE_YEAR {
            year_est &= i32::MAX;
        }

        let mut doy_est =
            zero_day - (365 * year_est + year_est / 4 - year_est / 100 + year_est / 400);

        // fix estimate
        year_est -= (doy_est < 0) as i32;
        if !SUPPORT_NEGATIVE_YEAR {
            year_est &= i32::MAX;
        }

        doy_est = zero_day - (365 * year_est + year_est / 4 - year_est / 100 + year_est / 400);

        year_est += adjust; // reset any negative year
        let march_doy0 = doy_est;

        // convert march-based values back to january-based
        let march_month0 = (march_doy0 * 5 + 2) / 153;
        let month = (march_month0 + 2) % 12 + 1;
        let dom = march_doy0 - (march_month0 * 306 + 5) / 10 + 1;
        year_est += march_month0 / 10;

        (year_est, month, dom)
    }

    #[inline]
    pub fn from_timestamp_millis(ts: i64) -> Self {
        // Converting to f64 is not necessarily faster but allows autovectorization if used in a loop
        Self::from_timestamp_millis_float(ts as f64)
    }

    #[inline]
    pub fn from_timestamp_millis_float(ts: f64) -> Self {
        let epoch_days = (ts * (1.0 / MILLIS_PER_DAY as f64)).floor();
        Self(unsafe { epoch_days.to_int_unchecked() })
    }

    #[inline]
    pub fn to_timestamp_millis(&self) -> i64 {
        (self.0 as i64) * MILLIS_PER_DAY
    }

    #[inline]
    pub fn to_timestamp_millis_float(&self) -> f64 {
        (self.0 as f64) * (MILLIS_PER_DAY as f64)
    }

    /// Adds the given number of `months` to `epoch_days`.
    /// If the day would be out of range for the resulting month
    /// then the date will be clamped to the end of the month.
    ///
    /// For example: 2022-01-31 + 1month => 2022-02-28
    #[inline]
    pub fn add_months(&self, months: i32) -> Self {
        let (mut y, mut m, mut d) = self.to_ymd();
        let mut m0 = m - 1;
        m0 += months;
        y += m0.div_euclid(12);
        m0 = m0.rem_euclid(12);
        d = d.min(days_per_month(y, m0));
        m = m0 + 1;
        Self::from_ymd(y, m, d)
    }

    /// Adds the given number of `years` to `epoch_days`.
    /// If the day would be out of range for the resulting month
    /// then the date will be clamped to the end of the month.
    ///
    /// For example: 2020-02-29 + 1year => 2021-02-28
    #[inline]
    pub fn add_years(&self, years: i32) -> Self {
        let (mut y, m, mut d) = self.to_ymd();
        y += years;
        d = d.min(days_per_month(y, m-1));
        Self::from_ymd(y, m, d)
    }

    #[inline]
    pub fn diff_months(&self, other: EpochDays) -> i32 {
        let (y0, m0, d0) = self.to_ymd();
        let (y1, m1, d1) = other.to_ymd();

        // TODO: Special-case the end of month? IOW, should diff(2023-10-31, 2023-11-30) return 1?
        (y1 * 12 + m1) - (y0 * 12 + m0) - (d1 < d0) as i32
    }

    #[inline]
    pub fn diff_years(&self, other: EpochDays) -> i32 {
        let (y0, m0, d0) = self.to_ymd();
        let (y1, m1, d1) = other.to_ymd();

        // y1 - y0 - ((m1 < m0) | ((m1 == m0) & (d1 < d0))) as i32
        y1 - y0 - ((m1, d1) < (m0, d0)) as i32
    }

    #[inline]
    pub fn date_trunc_month(&self) -> Self {
        let (y, m, d) = self.to_ymd();
        Self::from_ymd(y, m, 1)
    }

    #[inline]
    pub fn date_trunc_year(&self) -> Self {
        let (y, m, d) = self.to_ymd();
        Self::from_ymd(y, 1, 1)
    }

    #[inline]
    pub fn date_trunc_quarter(&self) -> Self {
        let (y, m, d) = self.to_ymd();
        Self::from_ymd(y, (m - 1) / 3 * 3 + 1, 1)
    }

    #[inline]
    pub fn extract_year(&self) -> i32 {
        self.to_ymd().0
    }

    #[inline]
    pub fn extract_month(&self) -> i32 {
        self.to_ymd().1
    }

    #[inline]
    pub fn extract_quarter(&self) -> i32 {
        (self.to_ymd().1 - 1) / 3 + 1
    }

    #[inline]
    pub fn extract_day_of_month(&self) -> i32 {
        self.to_ymd().2
    }
}

#[cfg(test)]
mod tests {
    use crate::epoch_days::{days_per_month, DAYS_PER_MONTH, is_leap_year};
    use crate::EpochDays;

    #[test]
    fn test_is_leap_year() {
        assert!(!is_leap_year(1900));
        assert!(!is_leap_year(1999));

        assert!(is_leap_year(2000));

        assert!(!is_leap_year(2001));
        assert!(!is_leap_year(2002));
        assert!(!is_leap_year(2003));

        assert!(is_leap_year(2004));
        assert!(is_leap_year(2020));
    }

    #[test]
    fn test_days_per_month() {
        for i in 0..12 {
            assert_eq!(days_per_month(2023, i as i32), DAYS_PER_MONTH[0][i], "non-leap: {i}");
        }
        for i in 0..12 {
            assert_eq!(days_per_month(2020, i as i32), DAYS_PER_MONTH[1][i], "leap: {i}");
        }
    }

    #[test]
    fn test_to_epoch_day() {
        assert_eq!(0, EpochDays::from_ymd(1970, 1, 1).0);
        assert_eq!(1, EpochDays::from_ymd(1970, 1, 2).0);
        assert_eq!(365, EpochDays::from_ymd(1971, 1, 1).0);
        assert_eq!(365 * 2, EpochDays::from_ymd(1972, 1, 1).0);
        assert_eq!(365 * 2 + 366, EpochDays::from_ymd(1973, 1, 1).0);

        assert_eq!(18998, EpochDays::from_ymd(2022, 1, 6).0);
        assert_eq!(19198, EpochDays::from_ymd(2022, 7, 25).0);
    }

    #[test]
    fn test_date_trunc_year_epoch_days() {
        assert_eq!(18993, EpochDays::new(19198).date_trunc_year().days());
    }

    #[test]
    fn test_date_trunc_month_epoch_days() {
        assert_eq!(19174, EpochDays::new(19198).date_trunc_month().days());
    }

    #[test]
    fn test_date_diff_month_epoch_days() {
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 1).diff_months(EpochDays::from_ymd(2023, 11, 1)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 1).diff_months(EpochDays::from_ymd(2023, 12, 1)),
            2
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 1).diff_months(EpochDays::from_ymd(2023, 12, 31)),
            2
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 22).diff_months(EpochDays::from_ymd(2023, 11, 22)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 22).diff_months(EpochDays::from_ymd(2023, 11, 21)),
            0
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 31).diff_months(EpochDays::from_ymd(2023, 11, 30)),
            0
        );
    }

    #[test]
    fn test_date_diff_month_epoch_days_negative() {
        assert_eq!(
            EpochDays::from_ymd(2023, 11, 1).diff_months(EpochDays::from_ymd(2023, 10, 1)),
            -1
        );
    }

    #[test]
    fn test_date_diff_year_epoch_days() {
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 1).diff_years(EpochDays::from_ymd(2023, 10, 1)),
            0
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 10, 1).diff_years(EpochDays::from_ymd(2023, 11, 1)),
            0
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 1, 1).diff_years(EpochDays::from_ymd(2024, 1, 1)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 2, 28).diff_years(EpochDays::from_ymd(2024, 2, 28)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 2, 28).diff_years(EpochDays::from_ymd(2024, 2, 29)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 6, 15).diff_years(EpochDays::from_ymd(2024, 6, 14)),
            0
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 6, 15).diff_years(EpochDays::from_ymd(2025, 6, 14)),
            1
        );
        assert_eq!(
            EpochDays::from_ymd(2023, 6, 15).diff_years(EpochDays::from_ymd(2025, 6, 16)),
            2
        );
    }

    #[test]
    fn test_extract_year() {
        assert_eq!(2022, EpochDays::from_ymd(2022, 1, 1).extract_year());
        assert_eq!(2022, EpochDays::from_ymd(2022, 8, 24).extract_year());
        assert_eq!(2022, EpochDays::from_ymd(2022, 12, 31).extract_year());
    }

    #[test]
    fn test_extract_month() {
        assert_eq!(1, EpochDays::from_ymd(2000, 1, 1).extract_month());
        assert_eq!(2, EpochDays::from_ymd(2000, 2, 1).extract_month());
        assert_eq!(2, EpochDays::from_ymd(2000, 2, 29).extract_month());
        assert_eq!(1, EpochDays::from_ymd(2022, 1, 1).extract_month());
        assert_eq!(8, EpochDays::from_ymd(2022, 8, 24).extract_month());
        assert_eq!(12, EpochDays::from_ymd(2022, 12, 31).extract_month());
    }

    #[test]
    fn test_extract_day() {
        assert_eq!(1, EpochDays::from_ymd(2000, 1, 1).extract_day_of_month());
        assert_eq!(1, EpochDays::from_ymd(2000, 2, 1).extract_day_of_month());
        assert_eq!(29, EpochDays::from_ymd(2000, 2, 29).extract_day_of_month());
        assert_eq!(1, EpochDays::from_ymd(2000, 3, 1).extract_day_of_month());
    }

    #[test]
    fn test_extract_quarter() {
        assert_eq!(1, EpochDays::from_ymd(2000, 1, 1).extract_quarter());
        assert_eq!(1, EpochDays::from_ymd(2000, 2, 1).extract_quarter());
        assert_eq!(1, EpochDays::from_ymd(2000, 3, 31).extract_quarter());
        assert_eq!(2, EpochDays::from_ymd(2000, 4, 1).extract_quarter());
        assert_eq!(3, EpochDays::from_ymd(2000, 7, 1).extract_quarter());
        assert_eq!(4, EpochDays::from_ymd(2000, 10, 1).extract_quarter());
        assert_eq!(4, EpochDays::from_ymd(2000, 12, 31).extract_quarter());
    }
}