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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Contains utility functions for shifting Date objects.
use chrono::Datelike;

/// Returns true if the year is a leap-year, as naively defined in the Gregorian calendar.
#[inline]
pub fn is_leap_year(year: i32) -> bool {
    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

// If the day lies within the month, this function has no effect. Otherwise, it shifts
// day backwards to the final day of the month.
// XXX: No attempt is made to handle days outside the 1-31 range.
#[inline]
fn normalise_day(year: i32, month: u32, day: u32) -> u32 {
    if day <= 28 {
        day
    } else if month == 2 {
        28 + is_leap_year(year) as u32
    } else if day == 31 && (month == 4 || month == 6 || month == 9 || month == 11) {
        30
    } else {
        day
    }
}

/// Shift a date by the given number of months.
/// Ambiguous month-ends are shifted backwards as necessary.
pub fn shift_months<D: Datelike>(date: D, months: i32) -> D {
    shift_months_opt(date, months).unwrap()
}

/// Same as [`shift_months`] except fallible on unresolvable dates/times.
///
/// Returns `None` rather than panicking when shift results in an ambiguous or non-existing
/// date/time (e.g. in a DST transition).
pub fn shift_months_opt<D: Datelike>(date: D, months: i32) -> Option<D> {
    let mut year = date.year() + (date.month() as i32 + months) / 12;
    let mut month = (date.month() as i32 + months) % 12;
    let mut day = date.day();

    if month < 1 {
        year -= 1;
        month += 12;
    }

    day = normalise_day(year, month as u32, day);

    // This is slow but guaranteed to succeed (short of interger overflow)
    if day <= 28 {
        date.with_day(day)?
            .with_month(month as u32)?
            .with_year(year)
    } else {
        date.with_day(1)?
            .with_month(month as u32)?
            .with_year(year)?
            .with_day(day)
    }
}

/// Shift a date by the given number of years.
/// Ambiguous month-ends are shifted backwards as necessary.
pub fn shift_years<D: Datelike>(date: D, years: i32) -> D {
    shift_years_opt(date, years).unwrap()
}

/// Same as [`shift_years`] except fallible on unresolvable dates/times.
///
/// Returns `None` rather than panicking when shift results in an ambiguous or non-existing
/// date/time (e.g. in a DST transition).
pub fn shift_years_opt<D: Datelike>(date: D, years: i32) -> Option<D> {
    shift_months_opt(date, years * 12)
}

/// Shift the date to have the given day.  Returns None if the day is not in the range 1-31.
///
/// Ambiguous month-ends are shifted backwards as necessary.
/// For example:
/// ```rust
/// # use chrono::NaiveDate;
/// # use chronoutil::with_day;
/// let start = NaiveDate::from_ymd_opt(2020, 2, 1).unwrap();
/// assert_eq!(with_day(start, 31), NaiveDate::from_ymd_opt(2020, 2, 29));
/// assert_eq!(with_day(start, 42), None);
/// ```
pub fn with_day<D: Datelike>(date: D, day: u32) -> Option<D> {
    if day == 0 || day > 31 {
        None
    } else {
        date.with_day(normalise_day(date.year(), date.month(), day))
    }
}

/// Shift the date to have the given month. Returns None if the month is out of range.
///
/// Ambiguous month-ends are shifted backwards as necessary.
/// For example:
/// ```rust
/// # use chrono::NaiveDate;
/// # use chronoutil::with_month;
/// let start = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
/// assert_eq!(with_month(start, 2), NaiveDate::from_ymd_opt(2020, 2, 29));
/// assert_eq!(with_month(start, 13), None);
/// ```
pub fn with_month<D: Datelike>(date: D, month: u32) -> Option<D> {
    if month == 0 || month > 12 {
        None
    } else {
        let delta = month as i32 - date.month() as i32;
        Some(shift_months(date, delta))
    }
}

/// Similar to [`with_month`] except _also_ fallible on unresolvable dates/times.
///
/// In addition to returning `None` when the month arg is out of range, also returns `None` rather
/// than panicking when shift results in an ambiguous or non-existing date/time (e.g. in a DST
/// transition).
pub fn with_month_opt<D: Datelike>(date: D, month: u32) -> Option<D> {
    if month == 0 || month > 12 {
        None
    } else {
        let delta = month as i32 - date.month() as i32;
        shift_months_opt(date, delta)
    }
}

/// Shift the date to have the given year.
///
/// Ambiguous month-ends are shifted backwards as necessary.
/// For example:
/// ```rust
/// # use chrono::NaiveDate;
/// # use chronoutil::with_year;
/// let start = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
/// assert_eq!(with_year(start, 2021), NaiveDate::from_ymd_opt(2021, 2, 28).unwrap());
/// ```
pub fn with_year<D: Datelike>(date: D, year: i32) -> D {
    with_year_opt(date, year).unwrap()
}

/// Same as [`with_year`] except fallible on unresolvable dates/times.
///
/// Returns `None` rather than panicking when shift results in an ambiguous or non-existing
/// date/time (e.g. in a DST transition).
pub fn with_year_opt<D: Datelike>(date: D, year: i32) -> Option<D> {
    let delta = year - date.year();
    shift_years_opt(date, delta)
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use chrono::{
        naive::{NaiveDate, NaiveDateTime, NaiveTime},
        LocalResult, TimeZone,
    };

    use super::*;

    #[test]
    fn test_leap_year_cases() {
        let _leap_years: Vec<i32> = vec![
            1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956,
            1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012,
            2016, 2020,
        ];
        let leap_years_1900_to_2020: HashSet<i32> = _leap_years.into_iter().collect();

        for year in 1900..2021 {
            assert_eq!(is_leap_year(year), leap_years_1900_to_2020.contains(&year))
        }
    }

    #[test]
    fn test_shift_months() {
        let base = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();

        assert_eq!(
            shift_months(base, 0),
            NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 1),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            shift_months(base, 2),
            NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 3),
            NaiveDate::from_ymd_opt(2020, 4, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, 4),
            NaiveDate::from_ymd_opt(2020, 5, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 5),
            NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, 6),
            NaiveDate::from_ymd_opt(2020, 7, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 7),
            NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 8),
            NaiveDate::from_ymd_opt(2020, 9, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, 9),
            NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 10),
            NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, 11),
            NaiveDate::from_ymd_opt(2020, 12, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 12),
            NaiveDate::from_ymd_opt(2021, 1, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 13),
            NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
        );

        assert_eq!(
            shift_months(base, -1),
            NaiveDate::from_ymd_opt(2019, 12, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -2),
            NaiveDate::from_ymd_opt(2019, 11, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, -3),
            NaiveDate::from_ymd_opt(2019, 10, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -4),
            NaiveDate::from_ymd_opt(2019, 9, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, -5),
            NaiveDate::from_ymd_opt(2019, 8, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -6),
            NaiveDate::from_ymd_opt(2019, 7, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -7),
            NaiveDate::from_ymd_opt(2019, 6, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, -8),
            NaiveDate::from_ymd_opt(2019, 5, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -9),
            NaiveDate::from_ymd_opt(2019, 4, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, -10),
            NaiveDate::from_ymd_opt(2019, 3, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -11),
            NaiveDate::from_ymd_opt(2019, 2, 28).unwrap()
        );
        assert_eq!(
            shift_months(base, -12),
            NaiveDate::from_ymd_opt(2019, 1, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -13),
            NaiveDate::from_ymd_opt(2018, 12, 31).unwrap()
        );

        assert_eq!(
            shift_months(base, 1265),
            NaiveDate::from_ymd_opt(2125, 6, 30).unwrap()
        );
    }

    #[test]
    fn test_shift_months_with_overflow() {
        let base = NaiveDate::from_ymd_opt(2020, 12, 31).unwrap();

        assert_eq!(shift_months(base, 0), base);
        assert_eq!(
            shift_months(base, 1),
            NaiveDate::from_ymd_opt(2021, 1, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 2),
            NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
        );
        assert_eq!(
            shift_months(base, 12),
            NaiveDate::from_ymd_opt(2021, 12, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 18),
            NaiveDate::from_ymd_opt(2022, 6, 30).unwrap()
        );

        assert_eq!(
            shift_months(base, -1),
            NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
        );
        assert_eq!(
            shift_months(base, -2),
            NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -10),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            shift_months(base, -12),
            NaiveDate::from_ymd_opt(2019, 12, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, -18),
            NaiveDate::from_ymd_opt(2019, 6, 30).unwrap()
        );
    }

    #[test]
    fn test_shift_months_datetime() {
        let date = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
        let o_clock = NaiveTime::from_hms_opt(1, 2, 3).unwrap();

        let base = NaiveDateTime::new(date, o_clock);

        assert_eq!(
            shift_months(base, 0).date(),
            NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
        );
        assert_eq!(
            shift_months(base, 1).date(),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            shift_months(base, 2).date(),
            NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
        );
        assert_eq!(shift_months(base, 0).time(), o_clock);
        assert_eq!(shift_months(base, 1).time(), o_clock);
        assert_eq!(shift_months(base, 2).time(), o_clock);
    }

    #[test]
    fn test_shift_months_datetime_tz() {
        let tz = &chrono_tz::Australia::Melbourne;

        let base = tz.with_ymd_and_hms(2020, 1, 31, 1, 2, 3).single().unwrap();

        assert_eq!(
            shift_months(base, 0),
            tz.with_ymd_and_hms(2020, 1, 31, 1, 2, 3).single().unwrap()
        );
        assert_eq!(
            shift_months(base, 1),
            tz.with_ymd_and_hms(2020, 2, 29, 1, 2, 3).single().unwrap()
        );
        assert_eq!(
            shift_months(base, 2),
            tz.with_ymd_and_hms(2020, 3, 31, 1, 2, 3).single().unwrap()
        );

        assert_eq!(
            shift_months_opt(base, 0).unwrap(),
            tz.with_ymd_and_hms(2020, 1, 31, 1, 2, 3).single().unwrap()
        );
        assert_eq!(
            shift_months_opt(base, 1).unwrap(),
            tz.with_ymd_and_hms(2020, 2, 29, 1, 2, 3).single().unwrap()
        );
        assert_eq!(
            shift_months_opt(base, 2).unwrap(),
            tz.with_ymd_and_hms(2020, 3, 31, 1, 2, 3).single().unwrap()
        );
    }

    #[test]
    #[should_panic]
    fn test_shift_months_datetime_to_dst_backward_transition() {
        let dst_tz = &chrono_tz::Australia::Melbourne;

        // On Apr 5th 2020 after 02:59:59, clocks were wound back to 02:00:00 making 02:00::00 to
        // 02:59:59 ambiguous.
        // <https://www.timeanddate.com/time/change/australia/melbourne?year=2020>
        if let LocalResult::Single(base) = dst_tz.with_ymd_and_hms(2020, 3, 5, 2, 00, 0) {
            shift_months(base, 1); // panics
        }
    }

    #[test]
    fn test_shift_months_opt_datetime_to_dst_backward_transition() {
        let dst_tz = &chrono_tz::Australia::Melbourne;

        let base = dst_tz
            .with_ymd_and_hms(2020, 3, 5, 2, 00, 0)
            .single()
            .unwrap();
        assert_eq!(None, shift_months_opt(base, 1))
    }

    #[test]
    #[should_panic]
    fn test_shift_months_datetime_to_dst_forward_transition() {
        let dst_tz = &chrono_tz::Australia::Melbourne;

        // On Oct 4th 2020 after 01:59:59, clocks were advanced to 03:00:00 making 02:00:00 to
        // 02:59:59 non-existent.
        // <https://www.timeanddate.com/time/change/australia/melbourne?year=2020>
        if let LocalResult::Single(base) = dst_tz.with_ymd_and_hms(2020, 9, 4, 2, 00, 0) {
            shift_months(base, 1); // panics
        }
    }

    #[test]
    fn test_shift_months_opt_datetime_to_dst_forward_transition() {
        let dst_tz = &chrono_tz::Australia::Melbourne;

        let base = dst_tz
            .with_ymd_and_hms(2020, 9, 4, 2, 00, 0)
            .single()
            .unwrap();
        assert_eq!(None, shift_months_opt(base, 1))
    }

    #[test]
    fn test_shift_years() {
        let base = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();

        assert_eq!(
            shift_years(base, 0),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            shift_years(base, 1),
            NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
        );
        assert_eq!(
            shift_years(base, 4),
            NaiveDate::from_ymd_opt(2024, 2, 29).unwrap()
        );
        assert_eq!(
            shift_years(base, 80),
            NaiveDate::from_ymd_opt(2100, 2, 28).unwrap()
        );
        assert_eq!(
            shift_years(base, -1),
            NaiveDate::from_ymd_opt(2019, 2, 28).unwrap()
        );
        assert_eq!(
            shift_years(base, -4),
            NaiveDate::from_ymd_opt(2016, 2, 29).unwrap()
        );
        assert_eq!(
            shift_years(base, -20),
            NaiveDate::from_ymd_opt(2000, 2, 29).unwrap()
        );
        assert_eq!(
            shift_years(base, -120),
            NaiveDate::from_ymd_opt(1900, 2, 28).unwrap()
        );
    }

    #[test]
    fn test_with_month() {
        let base = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();

        assert_eq!(with_month(base, 0), None);
        assert_eq!(with_month(base, 1), Some(base));
        assert_eq!(
            with_month(base, 2).unwrap(),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            with_month(base, 3).unwrap(),
            NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
        );
        assert_eq!(
            with_month(base, 4).unwrap(),
            NaiveDate::from_ymd_opt(2020, 4, 30).unwrap()
        );
        assert_eq!(
            with_month(base, 5).unwrap(),
            NaiveDate::from_ymd_opt(2020, 5, 31).unwrap()
        );
        assert_eq!(
            with_month(base, 6).unwrap(),
            NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()
        );
        assert_eq!(
            with_month(base, 7).unwrap(),
            NaiveDate::from_ymd_opt(2020, 7, 31).unwrap()
        );
        assert_eq!(
            with_month(base, 8).unwrap(),
            NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
        );
        assert_eq!(
            with_month(base, 9).unwrap(),
            NaiveDate::from_ymd_opt(2020, 9, 30).unwrap()
        );
        assert_eq!(
            with_month(base, 10).unwrap(),
            NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
        );
        assert_eq!(
            with_month(base, 11).unwrap(),
            NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
        );
        assert_eq!(
            with_month(base, 12).unwrap(),
            NaiveDate::from_ymd_opt(2020, 12, 31).unwrap()
        );
        assert_eq!(with_month(base, 13), None);

        assert_eq!(
            with_month(NaiveDate::from_ymd_opt(2021, 1, 31).unwrap(), 2),
            Some(NaiveDate::from_ymd_opt(2021, 2, 28).unwrap())
        );

        // Backwards shifts work too
        assert_eq!(
            with_month(NaiveDate::from_ymd_opt(2021, 2, 15).unwrap(), 1),
            Some(NaiveDate::from_ymd_opt(2021, 1, 15).unwrap())
        );
    }

    #[test]
    fn test_with_month_opt() {
        let tz = &chrono_tz::Australia::Melbourne;

        let base = tz.with_ymd_and_hms(2020, 1, 31, 0, 0, 0).single().unwrap();

        assert_eq!(with_month(base, 0), None);
        assert_eq!(with_month(base, 1), Some(base));
        assert_eq!(
            with_month(base, 2).unwrap(),
            tz.with_ymd_and_hms(2020, 2, 29, 0, 0, 0).single().unwrap()
        );
        assert_eq!(
            with_month(base, 3).unwrap(),
            tz.with_ymd_and_hms(2020, 3, 31, 0, 0, 0).single().unwrap()
        );
        assert_eq!(
            with_month(base, 12).unwrap(),
            tz.with_ymd_and_hms(2020, 12, 31, 0, 0, 0).single().unwrap()
        );
        assert_eq!(with_month(base, 13), None);

        // Backwards shifts work too
        assert_eq!(
            with_month(
                tz.with_ymd_and_hms(2021, 2, 15, 0, 0, 0).single().unwrap(),
                1
            ),
            Some(tz.with_ymd_and_hms(2021, 1, 15, 0, 0, 0).single().unwrap())
        );
    }

    #[test]
    fn test_with_year() {
        let base = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();

        assert_eq!(
            with_year(base, 2024),
            NaiveDate::from_ymd_opt(2024, 2, 29).unwrap()
        );
        assert_eq!(
            with_year(base, 2021),
            NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
        );
        assert_eq!(
            with_year(base, 2020),
            NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
        );
        assert_eq!(
            with_year(base, 2019),
            NaiveDate::from_ymd_opt(2019, 2, 28).unwrap()
        );
        assert_eq!(
            with_year(base, 2016),
            NaiveDate::from_ymd_opt(2016, 2, 29).unwrap()
        );
    }
}