Debian Codegraph @

third_party/rust/calendrical_calculations/src/chinese_based.rs

raw
1 use crate::astronomy::{self, Astronomical, Location, MEAN_SYNODIC_MONTH, MEAN_TROPICAL_YEAR};
2 use crate::helpers::i64_to_i32;
3 use crate::iso::{fixed_from_iso, iso_from_fixed};
4 use crate::rata_die::{Moment, RataDie};
5 use core::num::NonZeroU8;
6 #[allow(unused_imports)]
7 use core_maths::*;
8
9 // Don't iterate more than 14 times (which accounts for checking for 13 months)
10 const MAX_ITERS_FOR_MONTHS_OF_YEAR: u8 = 14;
11
12 /// The trait ChineseBased is used by Chinese-based calendars to perform computations shared by such calendar.
13 /// To do so, calendars should:
14 ///
15 /// - Implement `fn location` by providing a location at which observations of the moon are recorded, which
16 /// may change over time (the zone is important, long, lat, and elevation are not relevant for these calculations)
17 /// - Define `const EPOCH` as a `RataDie` marking the start date of the era of the Calendar for internal use,
18 /// which may not accurately reflect how years or eras are marked traditionally or seen by end-users
19 pub trait ChineseBased {
20 /// Given a fixed date, return the location used for observations of the new moon in order to
21 /// calculate the beginning of months. For multiple Chinese-based lunar calendars, this has
22 /// changed over the years, and can cause differences in calendar date.
23 fn location(fixed: RataDie) -> Location;
24
25 /// The RataDie of the beginning of the epoch used for internal computation; this may not
26 /// reflect traditional methods of year-tracking or eras, since Chinese-based calendars
27 /// may not track years ordinally in the same way many western calendars do.
28 const EPOCH: RataDie;
29
30 /// The ISO year that corresponds to year 1
31 const EPOCH_ISO: i32;
32
33 /// The name of the calendar for debugging.
34 const DEBUG_NAME: &'static str;
35
36 /// Given an ISO year, return the extended year
37 fn extended_from_iso(iso_year: i32) -> i32 {
38 iso_year - Self::EPOCH_ISO + 1
39 }
40 /// Given an extended year, return the ISO year
41 fn iso_from_extended(extended_year: i32) -> i32 {
42 extended_year - 1 + Self::EPOCH_ISO
43 }
44 }
45
46 // The equivalent first day in the Chinese calendar (based on inception of the calendar)
47 const CHINESE_EPOCH: RataDie = RataDie::new(-963099); // Feb. 15, 2637 BCE (-2636)
48 const CHINESE_EPOCH_ISO: i32 = -2636;
49
50 /// The Chinese calendar relies on knowing the current day at the moment of a new moon;
51 /// however, this can vary depending on location. As such, new moon calculations are based
52 /// on the time in Beijing. Before 1929, local time was used, represented as UTC+(1397/180 h).
53 /// In 1929, China adopted a standard time zone based on 120 degrees of longitude, meaning
54 /// from 1929 onward, all new moon calculations are based on UTC+8h.
55 ///
56 /// Offsets are not given in hours, but in partial days (1 hour = 1 / 24 day)
57 const UTC_OFFSET_PRE_1929: f64 = (1397.0 / 180.0) / 24.0;
58 const UTC_OFFSET_POST_1929: f64 = 8.0 / 24.0;
59
60 const CHINESE_LOCATION_PRE_1929: Location =
61 Location::new_unchecked(39.0, 116.0, 43.5, UTC_OFFSET_PRE_1929);
62 const CHINESE_LOCATION_POST_1929: Location =
63 Location::new_unchecked(39.0, 116.0, 43.5, UTC_OFFSET_POST_1929);
64
65 // The first day in the Korean Dangi calendar (based on the founding of Gojoseon)
66 const KOREAN_EPOCH: RataDie = RataDie::new(-852065); // Lunar new year 2333 BCE (-2332 ISO)
67 const KOREAN_EPOCH_ISO: i32 = -2332; // Lunar new year 2333 BCE (-2332 ISO)
68
69 /// The Korean Dangi calendar relies on knowing the current day at the moment of a new moon;
70 /// however, this can vary depending on location. As such, new moon calculations are based on
71 /// the time in Seoul. Before 1908, local time was used, represented as UTC+(3809/450 h).
72 /// This changed multiple times as different standard timezones were adopted in Korea.
73 /// Currently, UTC+9h is used.
74 ///
75 /// Offsets are not given in hours, but in partial days (1 hour = 1 / 24 day).
76 const UTC_OFFSET_ORIGINAL: f64 = (3809.0 / 450.0) / 24.0;
77 const UTC_OFFSET_1908: f64 = 8.5 / 24.0;
78 const UTC_OFFSET_1912: f64 = 9.0 / 24.0;
79 const UTC_OFFSET_1954: f64 = 8.5 / 24.0;
80 const UTC_OFFSET_1961: f64 = 9.0 / 24.0;
81
82 const FIXED_1908: RataDie = RataDie::new(696608); // Apr 1, 1908
83 const FIXED_1912: RataDie = RataDie::new(697978); // Jan 1, 1912
84 const FIXED_1954: RataDie = RataDie::new(713398); // Mar 21, 1954
85 const FIXED_1961: RataDie = RataDie::new(716097); // Aug 10, 1961
86
87 const KOREAN_LATITUDE: f64 = 37.0 + (34.0 / 60.0);
88 const KOREAN_LONGITUDE: f64 = 126.0 + (58.0 / 60.0);
89 const KOREAN_ELEVATION: f64 = 0.0;
90
91 const KOREAN_LOCATION_ORIGINAL: Location = Location::new_unchecked(
92 KOREAN_LATITUDE,
93 KOREAN_LONGITUDE,
94 KOREAN_ELEVATION,
95 UTC_OFFSET_ORIGINAL,
96 );
97 const KOREAN_LOCATION_1908: Location = Location::new_unchecked(
98 KOREAN_LATITUDE,
99 KOREAN_LONGITUDE,
100 KOREAN_ELEVATION,
101 UTC_OFFSET_1908,
102 );
103 const KOREAN_LOCATION_1912: Location = Location::new_unchecked(
104 KOREAN_LATITUDE,
105 KOREAN_LONGITUDE,
106 KOREAN_ELEVATION,
107 UTC_OFFSET_1912,
108 );
109 const KOREAN_LOCATION_1954: Location = Location::new_unchecked(
110 KOREAN_LATITUDE,
111 KOREAN_LONGITUDE,
112 KOREAN_ELEVATION,
113 UTC_OFFSET_1954,
114 );
115 const KOREAN_LOCATION_1961: Location = Location::new_unchecked(
116 KOREAN_LATITUDE,
117 KOREAN_LONGITUDE,
118 KOREAN_ELEVATION,
119 UTC_OFFSET_1961,
120 );
121
122 /// A type implementing [`ChineseBased`] for the Chinese calendar
123 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
124 #[allow(clippy::exhaustive_structs)] // newtype
125 pub struct Chinese;
126
127 /// A type implementing [`ChineseBased`] for the Dangi (Korean) calendar
128 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
129 #[allow(clippy::exhaustive_structs)] // newtype
130 pub struct Dangi;
131
132 impl ChineseBased for Chinese {
133 fn location(fixed: RataDie) -> Location {
134 let year = crate::iso::iso_year_from_fixed(fixed);
135 if year < 1929 {
136 CHINESE_LOCATION_PRE_1929
137 } else {
138 CHINESE_LOCATION_POST_1929
139 }
140 }
141
142 const EPOCH: RataDie = CHINESE_EPOCH;
143 const EPOCH_ISO: i32 = CHINESE_EPOCH_ISO;
144 const DEBUG_NAME: &'static str = "chinese";
145 }
146
147 impl ChineseBased for Dangi {
148 fn location(fixed: RataDie) -> Location {
149 if fixed < FIXED_1908 {
150 KOREAN_LOCATION_ORIGINAL
151 } else if fixed < FIXED_1912 {
152 KOREAN_LOCATION_1908
153 } else if fixed < FIXED_1954 {
154 KOREAN_LOCATION_1912
155 } else if fixed < FIXED_1961 {
156 KOREAN_LOCATION_1954
157 } else {
158 KOREAN_LOCATION_1961
159 }
160 }
161
162 const EPOCH: RataDie = KOREAN_EPOCH;
163 const EPOCH_ISO: i32 = KOREAN_EPOCH_ISO;
164 const DEBUG_NAME: &'static str = "dangi";
165 }
166
167 /// Marks the bounds of a lunar year
168 #[derive(Debug, Copy, Clone)]
169 #[allow(clippy::exhaustive_structs)] // we're comfortable making frequent breaking changes to this crate
170 pub struct YearBounds {
171 /// The date marking the start of the current lunar year
172 pub new_year: RataDie,
173 /// The date marking the start of the next lunar year
174 pub next_new_year: RataDie,
175 }
176
177 impl YearBounds {
178 /// Compute the YearBounds for the lunar year (年) containing `date`,
179 /// as well as the corresponding solar year (歲). Note that since the two
180 /// years overlap significantly but not entirely, the solstice bounds for the solar
181 /// year *may* not include `date`.
182 #[inline]
183 pub fn compute<C: ChineseBased>(date: RataDie) -> Self {
184 let prev_solstice = winter_solstice_on_or_before::<C>(date);
185 let (new_year, next_solstice) = new_year_on_or_before_fixed_date::<C>(date, prev_solstice);
186 // Using 400 here since new years can be up to 390 days apart, and we add some padding
187 let next_new_year = new_year_on_or_before_fixed_date::<C>(new_year + 400, next_solstice).0;
188
189 Self {
190 new_year,
191 next_new_year,
192 }
193 }
194
195 /// The number of days in this year
196 pub fn count_days(self) -> u16 {
197 let result = self.next_new_year - self.new_year;
198 debug_assert!(
199 ((u16::MIN as i64)..=(u16::MAX as i64)).contains(&result),
200 "Days in year should be in range of u16."
201 );
202 result as u16
203 }
204
205 /// Whether or not this is a leap year
206 pub fn is_leap(self) -> bool {
207 let difference = self.next_new_year - self.new_year;
208 difference > 365
209 }
210 }
211
212 /// Get the current major solar term of a fixed date, output as an integer from 1..=12.
213 ///
214 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
215 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5273-L5281
216 pub(crate) fn major_solar_term_from_fixed<C: ChineseBased>(date: RataDie) -> u32 {
217 let moment: Moment = date.as_moment();
218 let location = C::location(date);
219 let universal: Moment = Location::universal_from_standard(moment, location);
220 let solar_longitude =
221 i64_to_i32(Astronomical::solar_longitude(Astronomical::julian_centuries(universal)) as i64);
222 debug_assert!(
223 solar_longitude.is_ok(),
224 "Solar longitude should be in range of i32"
225 );
226 let s = solar_longitude.unwrap_or_else(|e| e.saturate());
227 let result_signed = (2 + s.div_euclid(30) - 1).rem_euclid(12) + 1;
228 debug_assert!(result_signed >= 0);
229 result_signed as u32
230 }
231
232 /// The fixed date in standard time at the observation location of the next new moon on or after a given Moment.
233 ///
234 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
235 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5329-L5338
236 pub(crate) fn new_moon_on_or_after<C: ChineseBased>(moment: Moment) -> RataDie {
237 let new_moon_moment = Astronomical::new_moon_at_or_after(midnight::<C>(moment));
238 let location = C::location(new_moon_moment.as_rata_die());
239 Location::standard_from_universal(new_moon_moment, location).as_rata_die()
240 }
241
242 /// The fixed date in standard time at the observation location of the previous new moon before a given Moment.
243 ///
244 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
245 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5318-L5327
246 pub(crate) fn new_moon_before<C: ChineseBased>(moment: Moment) -> RataDie {
247 let new_moon_moment = Astronomical::new_moon_before(midnight::<C>(moment));
248 let location = C::location(new_moon_moment.as_rata_die());
249 Location::standard_from_universal(new_moon_moment, location).as_rata_die()
250 }
251
252 /// Universal time of midnight at start of a Moment's day at the observation location
253 ///
254 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
255 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5353-L5357
256 pub(crate) fn midnight<C: ChineseBased>(moment: Moment) -> Moment {
257 Location::universal_from_standard(moment, C::location(moment.as_rata_die()))
258 }
259
260 /// Determines the fixed date of the lunar new year given the start of its corresponding solar year (歲), which is
261 /// also the winter solstice
262 ///
263 /// Calls to `no_major_solar_term` have been inlined for increased efficiency.
264 ///
265 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
266 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5370-L5394
267 pub(crate) fn new_year_in_sui<C: ChineseBased>(prior_solstice: RataDie) -> (RataDie, RataDie) {
268 // s1 is prior_solstice
269 // Using 370 here since solstices are ~365 days apart
270 // Both solstices should fall on December 20, 21, 22, or 23. The calendrical calculations
271 // drift away from this for large positive and negative years, so we artifically bind them
272 // to this range in order for other code invariants to be upheld.
273 let prior_solstice = bind_winter_solstice::<C>(prior_solstice);
274 let following_solstice =
275 bind_winter_solstice::<C>(winter_solstice_on_or_before::<C>(prior_solstice + 370)); // s2
276 let month_after_eleventh = new_moon_on_or_after::<C>((prior_solstice + 1).as_moment()); // m12
277 debug_assert!(month_after_eleventh - prior_solstice >= 0);
278 let month_after_twelfth = new_moon_on_or_after::<C>((month_after_eleventh + 1).as_moment()); // m13
279 let month_after_thirteenth = new_moon_on_or_after::<C>((month_after_twelfth + 1).as_moment());
280 debug_assert!(month_after_twelfth - month_after_eleventh >= 29);
281 let next_eleventh_month = new_moon_before::<C>((following_solstice + 1).as_moment()); // next-m11
282 let lhs_argument =
283 ((next_eleventh_month - month_after_eleventh) as f64 / MEAN_SYNODIC_MONTH).round() as i64;
284 let solar_term_a = major_solar_term_from_fixed::<C>(month_after_eleventh);
285 let solar_term_b = major_solar_term_from_fixed::<C>(month_after_twelfth);
286 let solar_term_c = major_solar_term_from_fixed::<C>(month_after_thirteenth);
287 if lhs_argument == 12 && (solar_term_a == solar_term_b || solar_term_b == solar_term_c) {
288 (month_after_thirteenth, following_solstice)
289 } else {
290 (month_after_twelfth, following_solstice)
291 }
292 }
293
294 /// This function forces the RataDie to be on December 20, 21, 22, or 23. It was
295 /// created for practical considerations and is not in the text.
296 ///
297 /// See: <https://github.com/unicode-org/icu4x/pull/4904>
298 fn bind_winter_solstice<C: ChineseBased>(solstice: RataDie) -> RataDie {
299 let (iso_year, iso_month, iso_day) = match iso_from_fixed(solstice) {
300 Ok(ymd) => ymd,
301 Err(_) => {
302 debug_assert!(false, "Solstice REALLY out of bounds: {solstice:?}");
303 return solstice;
304 }
305 };
306 let resolved_solstice = if iso_month < 12 || iso_day < 20 {
307 fixed_from_iso(iso_year, 12, 20)
308 } else if iso_day > 23 {
309 fixed_from_iso(iso_year, 12, 23)
310 } else {
311 solstice
312 };
313 if resolved_solstice != solstice {
314 if !(0..=4000).contains(&iso_year) {
315 #[cfg(feature = "logging")]
316 log::trace!("({}) Solstice out of bounds: {solstice:?}", C::DEBUG_NAME);
317 } else {
318 debug_assert!(
319 false,
320 "({}) Solstice out of bounds: {solstice:?}",
321 C::DEBUG_NAME
322 );
323 }
324 }
325 resolved_solstice
326 }
327
328 /// Get the fixed date of the nearest winter solstice, in the Chinese time zone,
329 /// on or before a given fixed date.
330 ///
331 /// This is valid for several thousand years, but it drifts for large positive
332 /// and negative years. See [`bind_winter_solstice`].
333 ///
334 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
335 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5359-L5368
336 pub(crate) fn winter_solstice_on_or_before<C: ChineseBased>(date: RataDie) -> RataDie {
337 let approx = Astronomical::estimate_prior_solar_longitude(
338 astronomy::WINTER,
339 midnight::<C>((date + 1).as_moment()),
340 );
341 let mut iters = 0;
342 let mut day = Moment::new((approx.inner() - 1.0).floor());
343 while iters < MAX_ITERS_FOR_MONTHS_OF_YEAR
344 && astronomy::WINTER
345 >= Astronomical::solar_longitude(Astronomical::julian_centuries(midnight::<C>(
346 day + 1.0,
347 )))
348 {
349 iters += 1;
350 day += 1.0;
351 }
352 debug_assert!(
353 iters < MAX_ITERS_FOR_MONTHS_OF_YEAR,
354 "Number of iterations was higher than expected"
355 );
356 day.as_rata_die()
357 }
358
359 /// Get the fixed date of the nearest Lunar New Year on or before a given fixed date.
360 /// This function also returns the solstice following a given date for optimization (see #3743).
361 ///
362 /// To call this function you must precompute the value of the prior solstice, which
363 /// is the result of winter_solstice_on_or_before
364 ///
365 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz.
366 /// Lisp reference code: https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5396-L5405
367 pub(crate) fn new_year_on_or_before_fixed_date<C: ChineseBased>(
368 date: RataDie,
369 prior_solstice: RataDie,
370 ) -> (RataDie, RataDie) {
371 let new_year = new_year_in_sui::<C>(prior_solstice);
372 if date >= new_year.0 {
373 new_year
374 } else {
375 // This happens when we're at the end of the current lunar year
376 // and the solstice has already happened. Thus the relevant solstice
377 // for the current lunar year is the previous one, which we calculate by offsetting
378 // back by a year.
379 let date_in_last_sui = date - 180; // This date is in the current lunar year, but the last solar year
380 let prior_solstice = winter_solstice_on_or_before::<C>(date_in_last_sui);
381 new_year_in_sui::<C>(prior_solstice)
382 }
383 }
384
385 /// Get a RataDie in the middle of a year; this is not necessarily meant for direct use in
386 /// calculations; rather, it is useful for getting a RataDie guaranteed to be in a given year
387 /// as input for other calculations like calculating the leap month in a year.
388 ///
389 /// Based on functions from _Calendrical Calculations_ by Reingold & Dershowitz
390 /// Lisp reference code: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5469-L5475>
391 pub fn fixed_mid_year_from_year<C: ChineseBased>(elapsed_years: i32) -> RataDie {
392 let cycle = (elapsed_years - 1).div_euclid(60) + 1;
393 let year = (elapsed_years - 1).rem_euclid(60) + 1;
394 C::EPOCH + ((((cycle - 1) * 60 + year - 1) as f64 + 0.5) * MEAN_TROPICAL_YEAR) as i64
395 }
396
397 /// Whether this year is a leap year
398 pub fn is_leap_year<C: ChineseBased>(year: i32) -> bool {
399 let mid_year = fixed_mid_year_from_year::<C>(year);
400 YearBounds::compute::<C>(mid_year).is_leap()
401 }
402
403 /// The last month and day in this year
404 pub fn last_month_day_in_year<C: ChineseBased>(year: i32) -> (u8, u8) {
405 let mid_year = fixed_mid_year_from_year::<C>(year);
406 let year_bounds = YearBounds::compute::<C>(mid_year);
407 let last_day = year_bounds.next_new_year - 1;
408 let month = if year_bounds.is_leap() { 13 } else { 12 };
409 let day = last_day - new_moon_before::<C>(last_day.as_moment()) + 1;
410 (month, day as u8)
411 }
412
413 /// Calculated the numbers of days in the given year
414 pub fn days_in_provided_year<C: ChineseBased>(year: i32) -> u16 {
415 let mid_year = fixed_mid_year_from_year::<C>(year);
416 let bounds = YearBounds::compute::<C>(mid_year);
417
418 bounds.count_days()
419 }
420
421 /// chinese_based_date_from_fixed returns extra things for use in caching
422 #[derive(Debug)]
423 #[non_exhaustive]
424 pub struct ChineseFromFixedResult {
425 /// The chinese year
426 pub year: i32,
427 /// The chinese month
428 pub month: u8,
429 /// The chinese day
430 pub day: u8,
431 /// The bounds of the current lunar year
432 pub year_bounds: YearBounds,
433 /// The index of the leap month, if any
434 pub leap_month: Option<NonZeroU8>,
435 }
436
437 /// Get a chinese based date from a fixed date, with the related ISO year
438 ///
439 /// Months are calculated by iterating through the dates of new moons until finding the last month which
440 /// does not exceed the given fixed date. The day of month is calculated by subtracting the fixed date
441 /// from the fixed date of the beginning of the month.
442 ///
443 /// The calculation for `elapsed_years` and `month` in this function are based on code from _Calendrical Calculations_ by Reingold & Dershowitz.
444 /// Lisp reference code: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5414-L5459>
445 pub fn chinese_based_date_from_fixed<C: ChineseBased>(date: RataDie) -> ChineseFromFixedResult {
446 let year_bounds = YearBounds::compute::<C>(date);
447 let first_day_of_year = year_bounds.new_year;
448
449 let year_float =
450 (1.5 - 1.0 / 12.0 + ((first_day_of_year - C::EPOCH) as f64) / MEAN_TROPICAL_YEAR).floor();
451 let year_int = i64_to_i32(year_float as i64);
452 debug_assert!(year_int.is_ok(), "Year should be in range of i32");
453 let year = year_int.unwrap_or_else(|e| e.saturate());
454
455 let new_moon = new_moon_before::<C>((date + 1).as_moment());
456 let month_i64 = ((new_moon - first_day_of_year) as f64 / MEAN_SYNODIC_MONTH).round() as i64 + 1;
457 debug_assert!(
458 ((u8::MIN as i64)..=(u8::MAX as i64)).contains(&month_i64),
459 "Month should be in range of u8! Value {month_i64} failed for RD {date:?}"
460 );
461 let month = month_i64 as u8;
462 let day_i64 = date - new_moon + 1;
463 debug_assert!(
464 ((u8::MIN as i64)..=(u8::MAX as i64)).contains(&month_i64),
465 "Day should be in range of u8! Value {month_i64} failed for RD {date:?}"
466 );
467 let day = day_i64 as u8;
468 let leap_month = if year_bounds.is_leap() {
469 // This doesn't need to be checked for `None`, since `get_leap_month_from_new_year`
470 // will always return a number greater than or equal to 1, and less than 14.
471 NonZeroU8::new(get_leap_month_from_new_year::<C>(first_day_of_year))
472 } else {
473 None
474 };
475
476 ChineseFromFixedResult {
477 year,
478 month,
479 day,
480 year_bounds,
481 leap_month,
482 }
483 }
484
485 /// Given that `new_year` is the first day of a leap year, find which month in the year is a leap month.
486 /// Since the first month in which there are no major solar terms is a leap month, this function
487 /// cycles through months until it finds the leap month, then returns the number of that month. This
488 /// function assumes the date passed in is in a leap year and tests to ensure this is the case in debug
489 /// mode by asserting that no more than thirteen months are analyzed.
490 ///
491 /// Calls to `no_major_solar_term` have been inlined for increased efficiency.
492 ///
493 /// Conceptually similar to code from _Calendrical Calculations_ by Reingold & Dershowitz
494 /// Lisp reference code: <https://github.com/EdReingold/calendar-code2/blob/main/calendar.l#L5443-L5450>
495 pub fn get_leap_month_from_new_year<C: ChineseBased>(new_year: RataDie) -> u8 {
496 let mut cur = new_year;
497 let mut result = 1;
498 let mut solar_term = major_solar_term_from_fixed::<C>(cur);
499 loop {
500 let next = new_moon_on_or_after::<C>((cur + 1).as_moment());
501 let next_solar_term = major_solar_term_from_fixed::<C>(next);
502 if result >= MAX_ITERS_FOR_MONTHS_OF_YEAR || solar_term == next_solar_term {
503 break;
504 }
505 cur = next;
506 solar_term = next_solar_term;
507 result += 1;
508 }
509 debug_assert!(result < MAX_ITERS_FOR_MONTHS_OF_YEAR, "The given year was not a leap year and an unexpected number of iterations occurred searching for a leap month.");
510 result
511 }
512
513 /// Returns the number of days in the given (year, month). In the Chinese calendar, months start at each
514 /// new moon, so this function finds the number of days between the new moon at the beginning of the given
515 /// month and the new moon at the beginning of the next month.
516 pub fn month_days<C: ChineseBased>(year: i32, month: u8) -> u8 {
517 let mid_year = fixed_mid_year_from_year::<C>(year);
518 let prev_solstice = winter_solstice_on_or_before::<C>(mid_year);
519 let new_year = new_year_on_or_before_fixed_date::<C>(mid_year, prev_solstice).0;
520 days_in_month::<C>(month, new_year, None).0
521 }
522
523 /// Returns the number of days in the given `month` after the given `new_year`.
524 /// Also returns the RataDie of the new moon beginning the next month.
525 pub fn days_in_month<C: ChineseBased>(
526 month: u8,
527 new_year: RataDie,
528 prev_new_moon: Option<RataDie>,
529 ) -> (u8, RataDie) {
530 let approx = new_year + ((month - 1) as i64 * 29);
531 let prev_new_moon = if let Some(prev_moon) = prev_new_moon {
532 prev_moon
533 } else {
534 new_moon_before::<C>((approx + 15).as_moment())
535 };
536 let next_new_moon = new_moon_on_or_after::<C>((approx + 15).as_moment());
537 let result = (next_new_moon - prev_new_moon) as u8;
538 debug_assert!(result == 29 || result == 30);
539 (result, next_new_moon)
540 }
541
542 /// Given a new year, calculate the number of days in the previous year
543 pub fn days_in_prev_year<C: ChineseBased>(new_year: RataDie) -> u16 {
544 let date = new_year - 300;
545 let prev_solstice = winter_solstice_on_or_before::<C>(date);
546 let (prev_new_year, _) = new_year_on_or_before_fixed_date::<C>(date, prev_solstice);
547 u16::try_from(new_year - prev_new_year).unwrap_or(360)
548 }
549
550 /// Returns the length of each month in the year, as well as a leap month index (1-indexed) if any.
551 /// Month lengths are stored as true for 30-day, false for 29-day.
552 /// In the case of no leap months, month 13 will have value false.
553 pub fn month_structure_for_year<C: ChineseBased>(
554 new_year: RataDie,
555 next_new_year: RataDie,
556 ) -> ([bool; 13], Option<NonZeroU8>) {
557 let mut ret = [false; 13];
558
559 let mut current_month_start = new_year;
560 let mut current_month_major_solar_term = major_solar_term_from_fixed::<C>(new_year);
561 let mut leap_month_index = None;
562 for i in 0u8..12 {
563 let next_month_start = new_moon_on_or_after::<C>((current_month_start + 28).as_moment());
564 let next_month_major_solar_term = major_solar_term_from_fixed::<C>(next_month_start);
565
566 if next_month_major_solar_term == current_month_major_solar_term {
567 leap_month_index = NonZeroU8::new(i + 1);
568 }
569
570 let diff = next_month_start - current_month_start;
571 debug_assert!(diff == 29 || diff == 30);
572 #[allow(clippy::indexing_slicing)] // array is of length 13, we iterate till i=11
573 if diff == 30 {
574 ret[usize::from(i)] = true;
575 }
576
577 current_month_start = next_month_start;
578 current_month_major_solar_term = next_month_major_solar_term;
579 }
580
581 if current_month_start == next_new_year {
582 // not all months without solar terms are leap months; they are only leap months if
583 // the year can admit them
584 //
585 // From Reingold & Dershowitz (p 311):
586 //
587 // The leap month of a 13-month winter-solstice-to-winter-solstice period is the first month
588 // that does not contain a major solar term — that is, the first lunar month that is wholly within a solar month.
589 //
590 // As such, if a month without a solar term is found in a non-leap year, we just ingnore it.
591 leap_month_index = None;
592 } else {
593 let diff = next_new_year - current_month_start;
594 debug_assert!(diff == 29 || diff == 30);
595 if diff == 30 {
596 ret[12] = true;
597 }
598 }
599 if current_month_start != next_new_year && leap_month_index.is_none() {
600 leap_month_index = NonZeroU8::new(13); // The last month is a leap month
601 debug_assert!(
602 major_solar_term_from_fixed::<C>(current_month_start) == current_month_major_solar_term,
603 "A leap month is required here, but it had a major solar term!"
604 );
605 }
606
607 (ret, leap_month_index)
608 }
609
610 /// Given the new year and a month/day pair, calculate the number of days until the first day of the given month
611 pub fn days_until_month<C: ChineseBased>(new_year: RataDie, month: u8) -> u16 {
612 let month_approx = 28_u16.saturating_mul(u16::from(month) - 1);
613
614 let new_moon = new_moon_on_or_after::<C>(new_year.as_moment() + (month_approx as f64));
615 let result = new_moon - new_year;
616 debug_assert!(((u16::MIN as i64)..=(u16::MAX as i64)).contains(&result), "Result {result} from new moon: {new_moon:?} and new year: {new_year:?} should be in range of u16!");
617 result as u16
618 }
619
620 #[cfg(test)]
621 mod test {
622
623 use super::*;
624 use crate::rata_die::Moment;
625
626 #[test]
627 fn test_chinese_new_moon_directionality() {
628 for i in (-1000..1000).step_by(31) {
629 let moment = Moment::new(i as f64);
630 let before = new_moon_before::<Chinese>(moment);
631 let after = new_moon_on_or_after::<Chinese>(moment);
632 assert!(before < after, "Chinese new moon directionality failed for Moment: {moment:?}, with:\n\tBefore: {before:?}\n\tAfter: {after:?}");
633 }
634 }
635
636 #[test]
637 fn test_chinese_new_year_on_or_before() {
638 let fixed = crate::iso::fixed_from_iso(2023, 6, 22);
639 let prev_solstice = winter_solstice_on_or_before::<Chinese>(fixed);
640 let result_fixed = new_year_on_or_before_fixed_date::<Chinese>(fixed, prev_solstice).0;
641 let (y, m, d) = crate::iso::iso_from_fixed(result_fixed).unwrap();
642 assert_eq!(y, 2023);
643 assert_eq!(m, 1);
644 assert_eq!(d, 22);
645 }
646
647 fn seollal_on_or_before(fixed: RataDie) -> RataDie {
648 let prev_solstice = winter_solstice_on_or_before::<Dangi>(fixed);
649 new_year_on_or_before_fixed_date::<Dangi>(fixed, prev_solstice).0
650 }
651
652 #[test]
653 fn test_month_structure() {
654 // Mostly just tests that the assertions aren't hit
655 for year in 1900..2050 {
656 let fixed = crate::iso::fixed_from_iso(year, 1, 1);
657 let chinese_year = chinese_based_date_from_fixed::<Chinese>(fixed);
658 let (month_lengths, leap) = month_structure_for_year::<Chinese>(
659 chinese_year.year_bounds.new_year,
660 chinese_year.year_bounds.next_new_year,
661 );
662
663 for (i, month_is_30) in month_lengths.into_iter().enumerate() {
664 if leap.is_none() && i == 12 {
665 // month_days has no defined behavior for month 13 on non-leap-years
666 continue;
667 }
668 let month_len = 29 + i32::from(month_is_30);
669 let month_days = month_days::<Chinese>(chinese_year.year, i as u8 + 1);
670 assert_eq!(
671 month_len,
672 i32::from(month_days),
673 "Month length for month {} must be the same",
674 i + 1
675 );
676 }
677 println!(
678 "{year} (chinese {}): {month_lengths:?} {leap:?}",
679 chinese_year.year
680 );
681 }
682 }
683
684 #[test]
685 fn test_seollal() {
686 #[derive(Debug)]
687 struct TestCase {
688 iso_year: i32,
689 iso_month: u8,
690 iso_day: u8,
691 expected_year: i32,
692 expected_month: u8,
693 expected_day: u8,
694 }
695
696 let cases = [
697 TestCase {
698 iso_year: 2024,
699 iso_month: 6,
700 iso_day: 6,
701 expected_year: 2024,
702 expected_month: 2,
703 expected_day: 10,
704 },
705 TestCase {
706 iso_year: 2024,
707 iso_month: 2,
708 iso_day: 9,
709 expected_year: 2023,
710 expected_month: 1,
711 expected_day: 22,
712 },
713 TestCase {
714 iso_year: 2023,
715 iso_month: 1,
716 iso_day: 22,
717 expected_year: 2023,
718 expected_month: 1,
719 expected_day: 22,
720 },
721 TestCase {
722 iso_year: 2023,
723 iso_month: 1,
724 iso_day: 21,
725 expected_year: 2022,
726 expected_month: 2,
727 expected_day: 1,
728 },
729 TestCase {
730 iso_year: 2022,
731 iso_month: 6,
732 iso_day: 6,
733 expected_year: 2022,
734 expected_month: 2,
735 expected_day: 1,
736 },
737 TestCase {
738 iso_year: 2021,
739 iso_month: 6,
740 iso_day: 6,
741 expected_year: 2021,
742 expected_month: 2,
743 expected_day: 12,
744 },
745 TestCase {
746 iso_year: 2020,
747 iso_month: 6,
748 iso_day: 6,
749 expected_year: 2020,
750 expected_month: 1,
751 expected_day: 25,
752 },
753 TestCase {
754 iso_year: 2019,
755 iso_month: 6,
756 iso_day: 6,
757 expected_year: 2019,
758 expected_month: 2,
759 expected_day: 5,
760 },
761 TestCase {
762 iso_year: 2018,
763 iso_month: 6,
764 iso_day: 6,
765 expected_year: 2018,
766 expected_month: 2,
767 expected_day: 16,
768 },
769 TestCase {
770 iso_year: 2025,
771 iso_month: 6,
772 iso_day: 6,
773 expected_year: 2025,
774 expected_month: 1,
775 expected_day: 29,
776 },
777 TestCase {
778 iso_year: 2026,
779 iso_month: 8,
780 iso_day: 8,
781 expected_year: 2026,
782 expected_month: 2,
783 expected_day: 17,
784 },
785 TestCase {
786 iso_year: 2027,
787 iso_month: 4,
788 iso_day: 4,
789 expected_year: 2027,
790 expected_month: 2,
791 expected_day: 7,
792 },
793 TestCase {
794 iso_year: 2028,
795 iso_month: 9,
796 iso_day: 21,
797 expected_year: 2028,
798 expected_month: 1,
799 expected_day: 27,
800 },
801 ];
802
803 for case in cases {
804 let fixed = crate::iso::fixed_from_iso(case.iso_year, case.iso_month, case.iso_day);
805 let seollal = seollal_on_or_before(fixed);
806 let (y, m, d) = crate::iso::iso_from_fixed(seollal).unwrap();
807 assert_eq!(
808 y, case.expected_year,
809 "Year check failed for case: {case:?}"
810 );
811 assert_eq!(
812 m, case.expected_month,
813 "Month check failed for case: {case:?}"
814 );
815 assert_eq!(d, case.expected_day, "Day check failed for case: {case:?}");
816 }
817 }
818 }