calendrical_calculations/
persian.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, I32CastError, IntegerRoundings};
11use crate::rata_die::RataDie;
12
13/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L4720>
14// Book states that the Persian epoch is the date: 3/19/622 and since the Persian Calendar has no year 0, the best choice was to use the Julian function.
15const FIXED_PERSIAN_EPOCH: RataDie = crate::julian::fixed_from_julian(622, 3, 19);
16
17// All these years are not leap, while they are considered leap by the 33-year
18// rule. The year following each of them is leap, but it's considered non-leap
19// by the 33-year rule. This table has been tested to match the modified
20// astronomical algorithm based on the 52.5 degrees east meridian from 1178 AP
21// (an arbitrary date before the Persian calendar was adopted in 1304 AP) to
22// 3000 AP (an arbitrary date far into the future).
23const NON_LEAP_CORRECTION: [i32; 78] = [
24    1502, 1601, 1634, 1667, 1700, 1733, 1766, 1799, 1832, 1865, 1898, 1931, 1964, 1997, 2030, 2059,
25    2063, 2096, 2129, 2158, 2162, 2191, 2195, 2224, 2228, 2257, 2261, 2290, 2294, 2323, 2327, 2356,
26    2360, 2389, 2393, 2422, 2426, 2455, 2459, 2488, 2492, 2521, 2525, 2554, 2558, 2587, 2591, 2620,
27    2624, 2653, 2657, 2686, 2690, 2719, 2723, 2748, 2752, 2756, 2781, 2785, 2789, 2818, 2822, 2847,
28    2851, 2855, 2880, 2884, 2888, 2913, 2917, 2921, 2946, 2950, 2954, 2979, 2983, 2987,
29];
30
31const MIN_NON_LEAP_CORRECTION: i32 = NON_LEAP_CORRECTION[0];
32
33/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L4803>
34/// Not used, but kept for comparative purposes
35pub fn fixed_from_arithmetic_persian(year: i32, month: u8, day: u8) -> RataDie {
36    let p_year = i64::from(year);
37    let month = i64::from(month);
38    let day = i64::from(day);
39    let y = if p_year > 0 {
40        p_year - 474
41    } else {
42        p_year - 473
43    };
44    let year = y.rem_euclid(2820) + 474;
45
46    RataDie::new(
47        FIXED_PERSIAN_EPOCH.to_i64_date() - 1
48            + 1029983 * y.div_euclid(2820)
49            + 365 * (year - 1)
50            + (31 * year - 5).div_euclid(128)
51            + if month <= 7 {
52                31 * (month - 1)
53            } else {
54                30 * (month - 1) + 6
55            }
56            + day,
57    )
58}
59
60/// fixed_from_arithmetic_persian, modified to use the more correct 33-year rule
61pub fn fixed_from_fast_persian(year: i32, month: u8, day: u8) -> RataDie {
62    let p_year = i64::from(year);
63    let month = i64::from(month);
64    let day = i64::from(day);
65    let mut new_year = FIXED_PERSIAN_EPOCH.to_i64_date() - 1
66        + 365 * (p_year - 1)
67        + (8 * p_year + 21).div_euclid(33);
68    if year > MIN_NON_LEAP_CORRECTION && NON_LEAP_CORRECTION.binary_search(&(year - 1)).is_ok() {
69        new_year -= 1;
70    }
71    RataDie::new(
72        new_year - 1
73            + if month <= 7 {
74                31 * (month - 1)
75            } else {
76                30 * (month - 1) + 6
77            }
78            + day,
79    )
80}
81
82/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L4857>
83/// Not used, but kept for comparative purposes
84pub fn arithmetic_persian_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> {
85    let year = arithmetic_persian_year_from_fixed(date);
86    let year = i64_to_i32(year)?;
87    let day_of_year = 1_i64 + (date - fixed_from_arithmetic_persian(year, 1, 1));
88    #[allow(unstable_name_collisions)] // div_ceil is unstable and polyfilled
89    let month = if day_of_year <= 186 {
90        day_of_year.div_ceil(31) as u8
91    } else {
92        (day_of_year - 6).div_ceil(30) as u8
93    };
94    let day = (date - fixed_from_arithmetic_persian(year, month, 1) + 1) as u8;
95    Ok((year, month, day))
96}
97
98/// arithmetic_persian_from_fixed, modified to use the 33-year rule method
99pub fn fast_persian_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> {
100    let year = fast_persian_year_from_fixed(date);
101    let mut year = i64_to_i32(year)?;
102    let mut day_of_year = 1_i64 + (date - fixed_from_fast_persian(year, 1, 1));
103    if day_of_year == 366
104        && year >= MIN_NON_LEAP_CORRECTION
105        && NON_LEAP_CORRECTION.binary_search(&year).is_ok()
106    {
107        year += 1;
108        day_of_year = 1;
109    }
110    #[allow(unstable_name_collisions)] // div_ceil is unstable and polyfilled
111    let month = if day_of_year <= 186 {
112        day_of_year.div_ceil(31) as u8
113    } else {
114        (day_of_year - 6).div_ceil(30) as u8
115    };
116    let day = (date - fixed_from_fast_persian(year, month, 1) + 1) as u8;
117    Ok((year, month, day))
118}
119
120/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L4829>
121/// Not used, but kept for comparative purposes
122fn arithmetic_persian_year_from_fixed(date: RataDie) -> i64 {
123    let d0 = date - fixed_from_arithmetic_persian(475, 1, 1);
124    let n2820 = d0.div_euclid(1029983);
125    let d1 = d0.rem_euclid(1029983);
126    let y2820 = if d1 == 1029982 {
127        2820
128    } else {
129        (128 * d1 + 46878).div_euclid(46751)
130    };
131    let year = 474 + n2820 * 2820 + y2820;
132    if year > 0 {
133        year
134    } else {
135        year - 1
136    }
137}
138
139/// arithmetic_persian_year_from_fixed modified for the 33-year rule
140fn fast_persian_year_from_fixed(date: RataDie) -> i64 {
141    let days_since_epoch = date - FIXED_PERSIAN_EPOCH + 1;
142    1 + (33 * days_since_epoch + 3).div_euclid(12053)
143}
144
145/// Lisp code reference: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L4789
146/// Not used, but kept for comparative purposes
147#[allow(dead_code)]
148fn is_arithmetic_leap_year(p_year: i32) -> bool {
149    let mut p_year = p_year as i64;
150    if 0 < p_year {
151        p_year -= 474;
152    } else {
153        p_year -= 473;
154    };
155    let year = p_year.rem_euclid(2820) + 474;
156
157    ((year + 38) * 31).rem_euclid(128) < 31
158}
159
160/// Calculated using the 33-year rule
161pub fn is_leap_year(p_year: i32) -> bool {
162    if p_year >= MIN_NON_LEAP_CORRECTION && NON_LEAP_CORRECTION.binary_search(&p_year).is_ok() {
163        false
164    } else if p_year > MIN_NON_LEAP_CORRECTION
165        && NON_LEAP_CORRECTION.binary_search(&(p_year - 1)).is_ok()
166    {
167        true
168    } else {
169        let p_year = p_year as i64;
170        (25 * p_year + 11).rem_euclid(33) < 8
171    }
172}
173
174#[cfg(test)]
175mod tests {
176    use super::*;
177    #[test]
178    fn test_persian_epoch() {
179        let epoch = FIXED_PERSIAN_EPOCH.to_i64_date();
180        // Iso year of Persian Epoch
181        let epoch_year_from_fixed = crate::iso::iso_year_from_fixed(RataDie::new(epoch)).unwrap();
182        // 622 is the correct ISO year for the Persian Epoch
183        assert_eq!(epoch_year_from_fixed, 622);
184    }
185
186    // Persian New Year occurring in March of Gregorian year (g_year) to fixed date
187    fn nowruz(g_year: i32) -> RataDie {
188        let (y, _m, _d) = crate::iso::iso_from_fixed(FIXED_PERSIAN_EPOCH).unwrap();
189        let persian_year = g_year - y + 1;
190        let year = if persian_year <= 0 {
191            persian_year - 1
192        } else {
193            persian_year
194        };
195        fixed_from_fast_persian(year, 1, 1)
196    }
197
198    #[test]
199    fn test_nowruz() {
200        // These values are used as test data in appendix C of the "Calendrical Calculations" book
201        let nowruz_test_year_start = 2000;
202        let nowruz_test_year_end = 2103;
203
204        for year in nowruz_test_year_start..=nowruz_test_year_end {
205            let two_thousand_eight_to_fixed = nowruz(year).to_i64_date();
206            let iso_date = crate::iso::fixed_from_iso(year, 3, 21);
207            let (persian_year, _m, _d) = fast_persian_from_fixed(iso_date).unwrap();
208            assert_eq!(
209                fast_persian_from_fixed(RataDie::new(two_thousand_eight_to_fixed))
210                    .unwrap()
211                    .0,
212                persian_year
213            );
214        }
215    }
216}