calendrical_calculations/
iso.rs

1// This file is part of ICU4X.
2//
3// The contents of this file implement algorithms from Calendrical Calculations
4// by Reingold & Dershowitz, Cambridge University Press, 4th edition (2018),
5// which have been released as Lisp code at <https://github.com/EdReingold/calendar-code2/>
6// under the Apache-2.0 license. Accordingly, this file is released under
7// the Apache License, Version 2.0 which can be found at the calendrical_calculations
8// package root or at http://www.apache.org/licenses/LICENSE-2.0.
9
10use crate::helpers::{i64_to_i32, k_day_after, I32CastError};
11use crate::rata_die::RataDie;
12
13// The Gregorian epoch is equivalent to first day in fixed day measurement
14const EPOCH: RataDie = RataDie::new(1);
15
16/// Whether or not `year` is a leap year
17///
18/// Inspired by Neri-Schneider <https://www.youtube.com/watch?v=J9KijLyP-yg&t=1239s>
19pub const fn is_leap_year(year: i32) -> bool {
20    // This is branch-free, as it compiles to a conditional move
21    if year % 25 != 0 {
22        year % 4 == 0
23    } else {
24        year % 16 == 0
25    }
26}
27
28// Fixed is day count representation of calendars starting from Jan 1st of year 1.
29// The fixed calculations algorithms are from the Calendrical Calculations book.
30//
31/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1167-L1189>
32pub const fn const_fixed_from_iso(year: i32, month: u8, day: u8) -> RataDie {
33    day_before_year(year)
34        .add(days_before_month(year, month) as i64)
35        .add(day as i64)
36}
37
38/// The number of days in this year before this month starts
39///
40/// Inspired by Neri-Schneider <https://onlinelibrary.wiley.com/doi/10.1002/spe.3172>
41pub const fn days_before_month(year: i32, month: u8) -> u16 {
42    if month < 3 {
43        // This compiles to a conditional move, so there's only one branch in this function
44        if month == 1 {
45            0
46        } else {
47            31
48        }
49    } else {
50        31 + 28 + is_leap_year(year) as u16 + ((979 * (month as u32) - 2919) >> 5) as u16
51    }
52}
53
54/// Non-const version of [`const_fixed_from_iso`]
55pub fn fixed_from_iso(year: i32, month: u8, day: u8) -> RataDie {
56    const_fixed_from_iso(year, month, day)
57}
58
59/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1191-L1217>
60pub const fn iso_year_from_fixed(date: RataDie) -> Result<i32, I32CastError> {
61    // Shouldn't overflow because it's not possbile to construct extreme values of RataDie
62    let date = date.since(EPOCH);
63
64    // 400 year cycles have 146097 days
65    let (n_400, date) = (date.div_euclid(146097), date.rem_euclid(146097));
66
67    // 100 year cycles have 36524 days
68    let (n_100, date) = (date / 36524, date % 36524);
69
70    // 4 year cycles have 1461 days
71    let (n_4, date) = (date / 1461, date % 1461);
72
73    let n_1 = date / 365;
74
75    let year = 400 * n_400 + 100 * n_100 + 4 * n_4 + n_1;
76
77    if n_100 == 4 || n_1 == 4 {
78        i64_to_i32(year)
79    } else {
80        i64_to_i32(year + 1)
81    }
82}
83
84/// Calculates the day before Jan 1 of `year`.
85pub const fn day_before_year(year: i32) -> RataDie {
86    let prev_year = (year as i64) - 1;
87    // Calculate days per year
88    let mut fixed: i64 = 365 * prev_year;
89    // Adjust for leap year logic. We can avoid the branch of div_euclid by making prev_year positive:
90    // YEAR_SHIFT is larger (in magnitude) than any prev_year, and, being divisible by 400,
91    // distributes correctly over the calculation on the next line.
92    const YEAR_SHIFT: i64 = (-(i32::MIN as i64 - 1) / 400 + 1) * 400;
93    fixed += (prev_year + YEAR_SHIFT) / 4 - (prev_year + YEAR_SHIFT) / 100
94        + (prev_year + YEAR_SHIFT) / 400
95        - const { YEAR_SHIFT / 4 - YEAR_SHIFT / 100 + YEAR_SHIFT / 400 };
96    RataDie::new(fixed)
97}
98
99/// Calculates the month/day from the 1-based day of the year
100pub fn year_day(year: i32, day_of_year: u16) -> (u8, u8) {
101    // Calculates the prior days of the year, then applies a correction based on leap year conditions for the correct ISO date conversion.
102    let correction = if day_of_year < 31 + 28 + is_leap_year(year) as u16 {
103        -1
104    } else {
105        (!is_leap_year(year)) as i32
106    };
107    let month = ((12 * (day_of_year as i32 + correction) + 373) / 367) as u8; // in 1..12 < u8::MAX
108    let day = (day_of_year - days_before_month(year, month)) as u8; // <= days_in_month < u8::MAX
109    (month, day)
110}
111
112/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1525-L1540>
113pub fn iso_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> {
114    let year = iso_year_from_fixed(date)?;
115    let day_of_year = date - day_before_year(year);
116    let (month, day) = year_day(year, day_of_year as u16);
117    Ok((year, month, day))
118}
119
120/// Calculates the date of Easter in the given year
121pub fn easter(year: i32) -> RataDie {
122    let century = (year / 100) + 1;
123    let shifted_epact =
124        (14 + 11 * year.rem_euclid(19) - century * 3 / 4 + (5 + 8 * century) / 25).rem_euclid(30);
125    let adjusted_epact = shifted_epact
126        + (shifted_epact == 0 || (shifted_epact == 1 && 10 < year.rem_euclid(19))) as i32;
127    let paschal_moon = fixed_from_iso(year, 4, 19) - adjusted_epact as i64;
128
129    k_day_after(0, paschal_moon)
130}
131
132#[test]
133fn test_easter() {
134    // https://en.wikipedia.org/wiki/List_of_dates_for_Easter
135    for (y, m, d) in [
136        (2021, 4, 4),
137        (2022, 4, 17),
138        (2023, 4, 9),
139        (2024, 3, 31),
140        (2025, 4, 20),
141        (2026, 4, 5),
142        (2027, 3, 28),
143        (2028, 4, 16),
144        (2029, 4, 1),
145    ] {
146        assert_eq!(easter(y), fixed_from_iso(y, m, d));
147    }
148}