Skip to content

Commit

Permalink
Fix year 2319 in the Chinese calendar (#4929)
Browse files Browse the repository at this point in the history
Increases the offset from 5 bits to 6 bits.
  • Loading branch information
sffc authored May 22, 2024
1 parent f5b1fcb commit bceda9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion components/calendar/src/chinese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ mod test {

use super::*;
use crate::types::MonthCode;
use calendrical_calculations::rata_die::RataDie;
use calendrical_calculations::{iso::fixed_from_iso, rata_die::RataDie};
/// Run a test twice, with two calendars
fn do_twice(
chinese_calculating: &Chinese,
Expand Down Expand Up @@ -503,6 +503,18 @@ mod test {
expected_month: 6,
expected_day: 12,
},
TestCase {
fixed: fixed_from_iso(2319, 2, 20).to_i64_date(),
expected_year: 2319 + 2636,
expected_month: 13,
expected_day: 30,
},
TestCase {
fixed: fixed_from_iso(2319, 2, 21).to_i64_date(),
expected_year: 2319 + 2636 + 1,
expected_month: 1,
expected_day: 1,
},
TestCase {
fixed: 738718,
expected_year: 4660,
Expand Down
5 changes: 3 additions & 2 deletions components/calendar/src/provider/chinese_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ impl<'data> ChineseBasedCacheV1<'data> {
/// Bit: 0 1 2 3 4 5 6 7
/// Byte 0: [ month lengths .............
/// Byte 1: .. month lengths ] | [ leap month index ..
/// Byte 2: ] | [ NY offset ] | unused
/// Byte 2: ] | [ NY offset ] | unused
/// ```
///
/// Where the New Year Offset is the offset from ISO Jan 21 of that year for Chinese New Year,
/// the month lengths are stored as 1 = 30, 0 = 29 for each month including the leap month.
/// The largest possible offset is 33, which requires 6 bits of storage.
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
Expand Down Expand Up @@ -162,7 +163,7 @@ impl PackedChineseBasedYearInfo {
!month_lengths[12] || leap_month_idx.is_some(),
"Last month length should not be set for non-leap years"
);
debug_assert!(ny_offset < 32, "Year offset too big to store");
debug_assert!(ny_offset < 33, "Year offset too big to store");
debug_assert!(
leap_month_idx.map(|l| l.get() <= 13).unwrap_or(true),
"Leap month indices must be 1 <= i <= 13"
Expand Down

0 comments on commit bceda9b

Please sign in to comment.