calendrical_calculations/julian.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// 1st Jan of 1st year proleptic Julian is equivalent to December 30th of 0th year proleptic Gregorian
14const JULIAN_EPOCH: RataDie = crate::gregorian::fixed_from_gregorian(0, 12, 30);
15
16const DAYS_IN_YEAR: i64 = 365;
17
18// One leap year every 4 years
19const DAYS_IN_4_YEAR_CYCLE: i64 = DAYS_IN_YEAR * 4 + 1;
20
21/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1684-L1687>
22#[inline(always)]
23pub const fn is_leap_year(year: i32) -> bool {
24 year % 4 == 0
25}
26
27/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1689-L1709>
28pub const fn fixed_from_julian(year: i32, month: u8, day: u8) -> RataDie {
29 day_before_year(year)
30 .add(days_before_month(year, month) as i64)
31 .add(day as i64)
32}
33
34/// The number of days in this year before this month starts
35///
36/// Inspired by Neri-Schneider <https://onlinelibrary.wiley.com/doi/10.1002/spe.3172>
37pub const fn days_before_month(year: i32, month: u8) -> u16 {
38 if month < 3 {
39 // This compiles to a conditional move, so there's only one branch in this function
40 if month == 1 {
41 0
42 } else {
43 31
44 }
45 } else {
46 31 + 28 + is_leap_year(year) as u16 + ((979 * (month as u32) - 2919) >> 5) as u16
47 }
48}
49
50/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1191-L1217>
51const fn year_from_fixed(date: RataDie) -> Result<i32, I32CastError> {
52 // Shouldn't overflow because it's not possbile to construct extreme values of RataDie
53 let date = date.since(JULIAN_EPOCH);
54
55 let (n_4, date) = (
56 date.div_euclid(DAYS_IN_4_YEAR_CYCLE),
57 date.rem_euclid(DAYS_IN_4_YEAR_CYCLE),
58 );
59
60 let n_1 = date / DAYS_IN_YEAR;
61
62 let year = 4 * n_4 + n_1 + (n_1 != 4) as i64;
63
64 i64_to_i32(year)
65}
66
67/// Calculates the day before Jan 1 of `year`.
68pub const fn day_before_year(year: i32) -> RataDie {
69 let prev_year = (year as i64) - 1;
70 // Calculate days per year
71 let mut fixed: i64 = DAYS_IN_YEAR * prev_year;
72 // Adjust for leap year logic. We can avoid the branch of div_euclid by making prev_year positive:
73 // YEAR_SHIFT is larger (in magnitude) than any prev_year, and, being divisible by 4,
74 // distributes correctly over the calculation on the next line.
75 const YEAR_SHIFT: i64 = (-(i32::MIN as i64 - 1) / 4 + 1) * 4;
76 fixed += (prev_year + YEAR_SHIFT) / 4 - const { YEAR_SHIFT / 4 };
77 JULIAN_EPOCH.add(fixed - 1)
78}
79
80/// Calculates the month/day from the 1-based day of the year
81pub fn year_day(year: i32, day_of_year: u16) -> (u8, u8) {
82 // Calculates the prior days of the year, then applies a correction based on leap year conditions for the correct ISO date conversion.
83 let correction = if day_of_year < 31 + 28 + is_leap_year(year) as u16 {
84 -1
85 } else {
86 (!is_leap_year(year)) as i32
87 };
88 let month = ((12 * (day_of_year as i32 + correction) + 373) / 367) as u8; // in 1..12 < u8::MAX
89 let day = (day_of_year - days_before_month(year, month)) as u8; // <= days_in_month < u8::MAX
90 (month, day)
91}
92
93/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1711-L1738>
94pub fn julian_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> {
95 let year = year_from_fixed(date)?;
96 let day_of_year = date - day_before_year(year);
97 let (month, day) = year_day(year, day_of_year as u16);
98 Ok((year, month, day))
99}
100
101/// Get a fixed date from the ymd of a Julian date.
102///
103/// Years are counted as in _Calendrical Calculations_ by Reingold & Dershowitz,
104/// meaning there is no year 0. For instance, near the epoch date, years are counted: -3, -2, -1, 1, 2, 3 instead of -2, -1, 0, 1, 2, 3.
105///
106/// Primarily useful for use with code constructing epochs specified in the bookg
107pub const fn fixed_from_julian_book_version(book_year: i32, month: u8, day: u8) -> RataDie {
108 debug_assert!(book_year != 0);
109 // TODO: Should we check the bounds here?
110 fixed_from_julian(
111 if book_year < 0 {
112 book_year + 1
113 } else {
114 book_year
115 },
116 month,
117 day,
118 )
119}
120
121/// Calculates the date of Easter in the given year
122pub fn easter(year: i32) -> RataDie {
123 let shifted_epact = (14 + 11 * year.rem_euclid(19)) % 30;
124 let paschal_moon = fixed_from_julian(year, 4, 19) - shifted_epact as i64;
125 k_day_after(0, paschal_moon)
126}
127
128#[test]
129fn test_easter() {
130 // https://en.wikipedia.org/wiki/List_of_dates_for_Easter, dates in Gregorian
131 for (y, m, d) in [
132 (2021, 5, 2),
133 (2022, 4, 24),
134 (2023, 4, 16),
135 (2024, 5, 5),
136 (2025, 4, 20),
137 (2026, 4, 12),
138 (2027, 5, 2),
139 (2028, 4, 16),
140 (2029, 4, 8),
141 ] {
142 assert_eq!(easter(y), crate::gregorian::fixed_from_gregorian(y, m, d));
143 }
144}