From af9c48a19229cd138d20d5700490b460d19a7bdd Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Wed, 22 May 2024 09:40:07 -0700 Subject: [PATCH] Fix year 2319 in the Chinese calendar --- components/calendar/src/chinese.rs | 14 +++++++++++++- components/calendar/src/provider/chinese_based.rs | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/components/calendar/src/chinese.rs b/components/calendar/src/chinese.rs index db6ca23bc98..a8385186fc1 100644 --- a/components/calendar/src/chinese.rs +++ b/components/calendar/src/chinese.rs @@ -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, @@ -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, diff --git a/components/calendar/src/provider/chinese_based.rs b/components/calendar/src/provider/chinese_based.rs index 6ddef840063..0eae8012ac6 100644 --- a/components/calendar/src/provider/chinese_based.rs +++ b/components/calendar/src/provider/chinese_based.rs @@ -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. /// ///
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, @@ -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"