| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::RangeError; |
| 6 |
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver, PackWithMD}; |
| 7 |
use crate::error::{ |
| 8 |
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, LunisolarDateError, |
| 9 |
MonthError, UnknownEraError, |
| 10 |
}; |
| 11 |
use crate::options::{DateAddOptions, DateDifferenceOptions}; |
| 12 |
use crate::options::{DateFromFieldsOptions, Overflow}; |
| 13 |
use crate::types::{DateFields, LeapStatus, Month, MonthInfo}; |
| 14 |
use crate::{Calendar, Date, types}; |
| 15 |
use ::tinystr::tinystr; |
| 16 |
use calendrical_calculations::hebrew_keviyah::{Keviyah, YearInfo}; |
| 17 |
use calendrical_calculations::rata_die::RataDie; |
| 18 |
use core::cmp::Ordering; |
| 19 |
|
| 20 |
/// The [Hebrew Calendar](https://en.wikipedia.org/wiki/Hebrew_calendar) |
| 21 |
/// |
| 22 |
/// The Hebrew calendar is a lunisolar calendar used as the Jewish liturgical calendar |
| 23 |
/// as well as an official calendar in Israel. |
| 24 |
/// |
| 25 |
/// This implementation uses civil month numbering, where Tishrei is the first month of the year. |
| 26 |
/// |
| 27 |
/// The precise algorithm used to calculate the Hebrew Calendar has [changed over time], with |
| 28 |
/// the modern one being in place since about 4536 AM (776 CE). This implementation extends |
| 29 |
/// proleptically for dates before that. |
| 30 |
/// |
| 31 |
/// [changed over time]: https://hakirah.org/vol20AjdlerAppendices.pdf |
| 32 |
/// |
| 33 |
/// This corresponds to the `"hebrew"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 34 |
/// |
| 35 |
/// # Era codes |
| 36 |
/// |
| 37 |
/// This calendar uses a single era code `am`, Anno Mundi. Dates before this era use negative years. |
| 38 |
/// |
| 39 |
/// # Months and days |
| 40 |
/// |
| 41 |
/// The 12 months are called Tishrei (`M01`, 30 days), Ḥešvan (`M02`, 29/30 days), |
| 42 |
/// Kīslev (`M03`, 30/29 days), Ṭevet (`M04`, 29 days), Šəvaṭ (`M05`, 30 days), ʾĂdār (`M06`, 29 days), |
| 43 |
/// Nīsān (`M07`, 30 days), ʾĪyyar (`M08`, 29 days), Sivan (`M09`, 30 days), Tammūz (`M10`, 29 days), |
| 44 |
/// ʾAv (`M11`, 30 days), ʾElūl (`M12`, 29 days). |
| 45 |
/// |
| 46 |
/// Due to Rosh Hashanah postponement rules, Ḥešvan and Kislev vary in length. |
| 47 |
/// |
| 48 |
/// In leap years (years 3, 6, 8, 11, 17, 19 in a 19-year cycle), the leap month Adar I (`M05L`, 30 days) |
| 49 |
/// is inserted before Adar (`M06`), which is then called Adar II ([`MonthInfo::leap_status`] will be |
| 50 |
/// [`LeapStatus::Base`] to mark this). |
| 51 |
/// |
| 52 |
/// Standard years thus have 353-355 days, and leap years 383-385. |
| 53 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Default)] |
| 54 |
#[allow(clippy::exhaustive_structs)] // unit struct |
| 55 |
pub struct Hebrew; |
| 56 |
|
| 57 |
/// The inner date type used for representing [`Date`]s of [`Hebrew`]. See [`Date`] and [`Hebrew`] for more details. |
| 58 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 59 |
pub struct HebrewDateInner(ArithmeticDate<Hebrew>); |
| 60 |
|
| 61 |
impl Hebrew { |
| 62 |
/// Construct a new [`Hebrew`] |
| 63 |
pub fn new() -> Self { |
| 64 |
Hebrew |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Copy, Clone, Debug)] |
| 69 |
pub(crate) struct HebrewYear { |
| 70 |
keviyah: Keviyah, |
| 71 |
/// The Hebrew extended year |
| 72 |
value: i32, |
| 73 |
} |
| 74 |
|
| 75 |
impl PartialEq for HebrewYear { |
| 76 |
fn eq(&self, other: &Self) -> bool { |
| 77 |
self.value == other.value |
| 78 |
} |
| 79 |
} |
| 80 |
impl Eq for HebrewYear {} |
| 81 |
impl core::hash::Hash for HebrewYear { |
| 82 |
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 83 |
self.value.hash(state); |
| 84 |
} |
| 85 |
} |
| 86 |
impl PartialOrd for HebrewYear { |
| 87 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 88 |
Some(self.cmp(other)) |
| 89 |
} |
| 90 |
} |
| 91 |
impl Ord for HebrewYear { |
| 92 |
fn cmp(&self, other: &Self) -> Ordering { |
| 93 |
self.value.cmp(&other.value) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl core::ops::Sub<HebrewYear> for HebrewYear { |
| 98 |
type Output = i32; |
| 99 |
#[inline] |
| 100 |
fn sub(self, rhs: HebrewYear) -> Self::Output { |
| 101 |
self.value - rhs.value |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl PackWithMD for HebrewYear { |
| 106 |
/// The first byte is the [`Keviyah`], the remaining four the YMD as encoded by [`i32::pack`]. |
| 107 |
type Packed = [u8; 5]; |
| 108 |
|
| 109 |
fn pack(self, month: u8, day: u8) -> Self::Packed { |
| 110 |
let a = self.keviyah as u8; |
| 111 |
let [b, c, d, e] = self.value.pack(month, day); |
| 112 |
[a, b, c, d, e] |
| 113 |
} |
| 114 |
|
| 115 |
fn unpack_year([a, b, c, d, e]: Self::Packed) -> Self { |
| 116 |
let value = i32::unpack_year([b, c, d, e]); |
| 117 |
let keviyah = Keviyah::from_integer(a); |
| 118 |
Self { keviyah, value } |
| 119 |
} |
| 120 |
|
| 121 |
fn unpack_month([_, b, c, d, e]: Self::Packed) -> u8 { |
| 122 |
i32::unpack_month([b, c, d, e]) |
| 123 |
} |
| 124 |
|
| 125 |
fn unpack_day([_, b, c, d, e]: Self::Packed) -> u8 { |
| 126 |
i32::unpack_day([b, c, d, e]) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl HebrewYear { |
| 131 |
/// Convenience method to compute for a given year. Don't use this if you actually need |
| 132 |
/// a [`YearInfo`] that you want to call `.new_year()` on. |
| 133 |
fn compute(value: i32) -> Self { |
| 134 |
Self { |
| 135 |
keviyah: YearInfo::compute_for(value).keviyah, |
| 136 |
value, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
fn for_rd(rd: RataDie) -> Self { |
| 141 |
let (year, value) = YearInfo::year_containing_rd(rd); |
| 142 |
Self { |
| 143 |
keviyah: year.keviyah, |
| 144 |
value, |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
fn new_year(self) -> RataDie { |
| 149 |
self.keviyah.year_info(self.value).new_year() |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl DateFieldsResolver for Hebrew { |
| 154 |
type YearInfo = HebrewYear; |
| 155 |
fn days_in_provided_month(year: HebrewYear, ordinal_month: u8) -> u8 { |
| 156 |
year.keviyah.month_len(ordinal_month) |
| 157 |
} |
| 158 |
|
| 159 |
fn months_in_provided_year(year: HebrewYear) -> u8 { |
| 160 |
12 + year.keviyah.is_leap() as u8 |
| 161 |
} |
| 162 |
|
| 163 |
#[inline] |
| 164 |
fn min_months_from(_start: HebrewYear, years: i32) -> i32 { |
| 165 |
// The Hebrew Metonic cycle is 7 leap years every 19 years, |
| 166 |
// which comes out to 235 months per 19 years. |
| 167 |
// |
| 168 |
// We need to ensure that this is always *lower or equal to* the number of |
| 169 |
// months in a given year span. |
| 170 |
// |
| 171 |
// Firstly, note that this math will produce exactly the number of months in any given period |
| 172 |
// that spans a whole number of cycles. Note that we are only performing integer |
| 173 |
// ops here, and our SAFE_YEAR_RANGE is well within the range of allowed values |
| 174 |
// for multiplying by 235. |
| 175 |
// |
| 176 |
// So we only need to verify that this math produces the right results within a single cycle. |
| 177 |
// |
| 178 |
// The Hebrew Metonic cycle has leap years in year 3, 6, 8, 11, 14, 17, and 19 (starting counting at year 1), |
| 179 |
// i.e., leap year gaps of +3, +3, +2, +3, +3, +3, +2. |
| 180 |
// |
| 181 |
// 235 / 19 is ≈12 7/19 months per year, which leads to one leap month every three years plus 2/19 |
| 182 |
// months left over. So this is correct as long as it does not predict a leap month in 2 years |
| 183 |
// where the Hebrew calendar expects one in 3. |
| 184 |
// |
| 185 |
// The longest sequence of "three year leap months" in the Hebrew calendar |
| 186 |
// is 4: year 8->11->14->17. In that time the error will accumulate to 6/19, which is not |
| 187 |
// enough to create a "two year leap month" in our calculation. So this calculation cannot go past |
| 188 |
// the actual cycle of the Hebrew calendar. |
| 189 |
235 * years / 19 |
| 190 |
} |
| 191 |
|
| 192 |
#[inline] |
| 193 |
fn extended_year_from_era_year_unchecked( |
| 194 |
&self, |
| 195 |
era: &[u8], |
| 196 |
era_year: i32, |
| 197 |
) -> Result<i32, UnknownEraError> { |
| 198 |
match era { |
| 199 |
b"am" => Ok(era_year), |
| 200 |
_ => Err(UnknownEraError), |
| 201 |
} |
| 202 |
} |
| 203 |
#[inline] |
| 204 |
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo { |
| 205 |
HebrewYear::compute(extended_year) |
| 206 |
} |
| 207 |
|
| 208 |
#[inline] |
| 209 |
fn extended_from_year_info(&self, year_info: Self::YearInfo) -> i32 { |
| 210 |
year_info.value |
| 211 |
} |
| 212 |
|
| 213 |
fn reference_year_from_month_day( |
| 214 |
&self, |
| 215 |
month: Month, |
| 216 |
day: u8, |
| 217 |
) -> Result<Self::YearInfo, EcmaReferenceYearError> { |
| 218 |
// December 31, 1972 occurs on 4th month, 26th day, 5733 AM |
| 219 |
let hebrew_year = match (month.number(), month.is_leap()) { |
| 220 |
(1, false) => 5733, |
| 221 |
(2, false) => match day { |
| 222 |
// There is no day 30 in 5733 (there is in 5732) |
| 223 |
..=29 => 5733, |
| 224 |
// Note (here and below): this must be > 29, not just == 30, |
| 225 |
// since we have not yet applied a potential Overflow::Constrain. |
| 226 |
_ => 5732, |
| 227 |
}, |
| 228 |
(3, false) => match day { |
| 229 |
// There is no day 30 in 5733 (there is in 5732) |
| 230 |
..=29 => 5733, |
| 231 |
_ => 5732, |
| 232 |
}, |
| 233 |
(4, false) => match day { |
| 234 |
..=26 => 5733, |
| 235 |
_ => 5732, |
| 236 |
}, |
| 237 |
(5..=12, false) => 5732, |
| 238 |
// Neither 5731 nor 5732 is a leap year |
| 239 |
(5, true) => 5730, |
| 240 |
_ => { |
| 241 |
return Err(EcmaReferenceYearError::MonthNotInCalendar); |
| 242 |
} |
| 243 |
}; |
| 244 |
Ok(HebrewYear::compute(hebrew_year)) |
| 245 |
} |
| 246 |
|
| 247 |
fn ordinal_from_month( |
| 248 |
&self, |
| 249 |
year: Self::YearInfo, |
| 250 |
month: Month, |
| 251 |
overflow: Overflow, |
| 252 |
) -> Result<u8, MonthError> { |
| 253 |
let is_leap_year = year.keviyah.is_leap(); |
| 254 |
let ordinal_month = match (month.number(), month.is_leap()) { |
| 255 |
(n @ 1..=12, false) => n + (n >= 6 && is_leap_year) as u8, |
| 256 |
(5, true) => { |
| 257 |
if is_leap_year { |
| 258 |
6 |
| 259 |
} else { |
| 260 |
// Requesting Adar 1 in non-leap year, handle constrain/reject behavior |
| 261 |
match overflow { |
| 262 |
Overflow::Constrain => 6, |
| 263 |
Overflow::Reject => return Err(MonthError::NotInYear), |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
_ => return Err(MonthError::NotInCalendar), |
| 268 |
}; |
| 269 |
Ok(ordinal_month) |
| 270 |
} |
| 271 |
|
| 272 |
fn month_from_ordinal(&self, year: Self::YearInfo, ordinal_month: u8) -> Month { |
| 273 |
let is_leap = year.keviyah.is_leap(); |
| 274 |
Month::new_unchecked( |
| 275 |
ordinal_month - (is_leap && ordinal_month >= 6) as u8, |
| 276 |
ordinal_month == 6 && is_leap, |
| 277 |
) |
| 278 |
} |
| 279 |
|
| 280 |
fn to_rata_die_inner(year: Self::YearInfo, month: u8, day: u8) -> RataDie { |
| 281 |
year.new_year() + year.keviyah.days_preceding(month) as i64 + (day - 1) as i64 |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
impl crate::cal::scaffold::UnstableSealed for Hebrew {} |
| 286 |
impl Calendar for Hebrew { |
| 287 |
type DateInner = HebrewDateInner; |
| 288 |
type Year = types::EraYear; |
| 289 |
type DateCompatibilityError = core::convert::Infallible; |
| 290 |
|
| 291 |
fn new_date( |
| 292 |
&self, |
| 293 |
year: types::YearInput, |
| 294 |
month: Month, |
| 295 |
day: u8, |
| 296 |
) -> Result<Self::DateInner, DateNewError> { |
| 297 |
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(HebrewDateInner) |
| 298 |
} |
| 299 |
|
| 300 |
fn from_fields( |
| 301 |
&self, |
| 302 |
fields: DateFields, |
| 303 |
options: DateFromFieldsOptions, |
| 304 |
) -> Result<Self::DateInner, DateFromFieldsError> { |
| 305 |
ArithmeticDate::from_fields(fields, options, self).map(HebrewDateInner) |
| 306 |
} |
| 307 |
|
| 308 |
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner { |
| 309 |
let year = HebrewYear::for_rd(rd); |
| 310 |
|
| 311 |
// Clamp the RD to our year |
| 312 |
let rd = rd.clamp( |
| 313 |
year.new_year(), |
| 314 |
year.new_year() + year.keviyah.year_length() as i64, |
| 315 |
); |
| 316 |
|
| 317 |
let (month, day) = year |
| 318 |
.keviyah |
| 319 |
.month_day_for((rd - year.new_year()) as u16 + 1); |
| 320 |
|
| 321 |
// date is in the valid RD range |
| 322 |
HebrewDateInner(ArithmeticDate::new_unchecked(year, month, day)) |
| 323 |
} |
| 324 |
|
| 325 |
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie { |
| 326 |
date.0.to_rata_die() |
| 327 |
} |
| 328 |
|
| 329 |
fn has_cheap_iso_conversion(&self) -> bool { |
| 330 |
false |
| 331 |
} |
| 332 |
|
| 333 |
fn months_in_year(&self, date: &Self::DateInner) -> u8 { |
| 334 |
Self::months_in_provided_year(date.0.year()) |
| 335 |
} |
| 336 |
|
| 337 |
fn days_in_year(&self, date: &Self::DateInner) -> u16 { |
| 338 |
date.0.year().keviyah.year_length() |
| 339 |
} |
| 340 |
|
| 341 |
fn days_in_month(&self, date: &Self::DateInner) -> u8 { |
| 342 |
Self::days_in_provided_month(date.0.year(), date.0.month()) |
| 343 |
} |
| 344 |
|
| 345 |
fn add( |
| 346 |
&self, |
| 347 |
date: &Self::DateInner, |
| 348 |
duration: types::DateDuration, |
| 349 |
options: DateAddOptions, |
| 350 |
) -> Result<Self::DateInner, DateAddError> { |
| 351 |
date.0.added(duration, self, options).map(HebrewDateInner) |
| 352 |
} |
| 353 |
|
| 354 |
fn until( |
| 355 |
&self, |
| 356 |
date1: &Self::DateInner, |
| 357 |
date2: &Self::DateInner, |
| 358 |
options: DateDifferenceOptions, |
| 359 |
) -> types::DateDuration { |
| 360 |
date1.0.until(&date2.0, self, options) |
| 361 |
} |
| 362 |
|
| 363 |
fn check_date_compatibility(&self, &Self: &Self) -> Result<(), Self::DateCompatibilityError> { |
| 364 |
Ok(()) |
| 365 |
} |
| 366 |
|
| 367 |
fn debug_name(&self) -> &'static str { |
| 368 |
"Hebrew" |
| 369 |
} |
| 370 |
|
| 371 |
fn year_info(&self, date: &Self::DateInner) -> Self::Year { |
| 372 |
let extended_year = date.0.year().value; |
| 373 |
types::EraYear { |
| 374 |
era_index: Some(0), |
| 375 |
era: tinystr!(16, "am"), |
| 376 |
year: extended_year, |
| 377 |
extended_year, |
| 378 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool { |
| 383 |
date.0.year().keviyah.is_leap() |
| 384 |
} |
| 385 |
|
| 386 |
fn month(&self, date: &Self::DateInner) -> MonthInfo { |
| 387 |
let mut m = MonthInfo::new(self, date.0); |
| 388 |
// Even though the leap month is modeled as M05L, |
| 389 |
// the actual leap base is M06. |
| 390 |
if m.number() == 6 && m.ordinal == 7 { |
| 391 |
m.leap_status = LeapStatus::Base; |
| 392 |
#[allow(deprecated)] |
| 393 |
{ |
| 394 |
// This is an ICU4X invention, it's not needed by |
| 395 |
// formatting anymore, but we keep producing it |
| 396 |
// for now. |
| 397 |
m.formatting_code = Month::leap(6).code(); |
| 398 |
} |
| 399 |
} |
| 400 |
m |
| 401 |
} |
| 402 |
|
| 403 |
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth { |
| 404 |
types::DayOfMonth(date.0.day()) |
| 405 |
} |
| 406 |
|
| 407 |
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear { |
| 408 |
types::DayOfYear(date.0.year().keviyah.days_preceding(date.0.month()) + date.0.day() as u16) |
| 409 |
} |
| 410 |
|
| 411 |
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm> { |
| 412 |
Some(crate::preferences::CalendarAlgorithm::Hebrew) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
impl Date<Hebrew> { |
| 417 |
/// Construct a new Hebrew [`Date`]. |
| 418 |
/// |
| 419 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 420 |
/// valid range of `-9999..=9999`. |
| 421 |
/// |
| 422 |
/// ```rust |
| 423 |
/// use icu::calendar::Date; |
| 424 |
/// use icu::calendar::types::Month; |
| 425 |
/// |
| 426 |
/// let date = Date::try_new_hebrew_v2(5782, Month::leap(5), 7) |
| 427 |
/// .expect("Failed to initialize Date instance."); |
| 428 |
/// |
| 429 |
/// assert_eq!(date.era_year().year, 5782); |
| 430 |
/// // Adar I |
| 431 |
/// assert_eq!(date.month().number(), 5); |
| 432 |
/// assert!(date.month().to_input().is_leap()); |
| 433 |
/// assert_eq!(date.day_of_month().0, 7); |
| 434 |
/// ``` |
| 435 |
pub fn try_new_hebrew_v2( |
| 436 |
year: i32, |
| 437 |
month: Month, |
| 438 |
day: u8, |
| 439 |
) -> Result<Date<Hebrew>, LunisolarDateError> { |
| 440 |
ArithmeticDate::try_from_ymd_lunisolar(year, month, day, &Hebrew) |
| 441 |
.map(HebrewDateInner) |
| 442 |
.map(|inner| Date::from_raw(inner, Hebrew)) |
| 443 |
} |
| 444 |
|
| 445 |
/// This method uses an ordinal month, which is probably not what you want. |
| 446 |
/// |
| 447 |
/// Use [`Date::try_new_hebrew_v2`] |
| 448 |
#[deprecated(since = "2.1.0", note = "use `Date::try_new_hebrew_v2`")] |
| 449 |
pub fn try_new_hebrew( |
| 450 |
year: i32, |
| 451 |
ordinal_month: u8, |
| 452 |
day: u8, |
| 453 |
) -> Result<Date<Hebrew>, RangeError> { |
| 454 |
ArithmeticDate::from_year_month_day(year, ordinal_month, day, &Hebrew) |
| 455 |
.map(HebrewDateInner) |
| 456 |
.map(|inner| Date::from_raw(inner, Hebrew)) |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
#[cfg(test)] |
| 461 |
mod tests { |
| 462 |
use super::*; |
| 463 |
use crate::types::Weekday; |
| 464 |
|
| 465 |
pub const TISHREI: Month = Month::new(1); |
| 466 |
pub const ḤESHVAN: Month = Month::new(2); |
| 467 |
pub const KISLEV: Month = Month::new(3); |
| 468 |
pub const TEVET: Month = Month::new(4); |
| 469 |
pub const SHEVAT: Month = Month::new(5); |
| 470 |
pub const ADARI: Month = Month::leap(5); |
| 471 |
pub const ADAR: Month = Month::new(6); |
| 472 |
pub const NISAN: Month = Month::new(7); |
| 473 |
pub const IYYAR: Month = Month::new(8); |
| 474 |
pub const SIVAN: Month = Month::new(9); |
| 475 |
pub const TAMMUZ: Month = Month::new(10); |
| 476 |
pub const AV: Month = Month::new(11); |
| 477 |
pub const ELUL: Month = Month::new(12); |
| 478 |
|
| 479 |
const LEAP_YEARS_IN_TESTS: [i32; 1] = [5782]; |
| 480 |
// If any of the years here are leap years, add them to |
| 481 |
// [`LEAP_YEARS_IN_TESTS`] (we have this manually so we don't |
| 482 |
// end up exercising potentially buggy codepaths to test this) |
| 483 |
#[expect(clippy::type_complexity)] |
| 484 |
const ISO_HEBREW_DATE_PAIRS: [((i32, u8, u8), (i32, Month, u8)); 48] = [ |
| 485 |
((2021, 1, 10), (5781, TEVET, 26)), |
| 486 |
((2021, 1, 25), (5781, SHEVAT, 12)), |
| 487 |
((2021, 2, 10), (5781, SHEVAT, 28)), |
| 488 |
((2021, 2, 25), (5781, ADAR, 13)), |
| 489 |
((2021, 3, 10), (5781, ADAR, 26)), |
| 490 |
((2021, 3, 25), (5781, NISAN, 12)), |
| 491 |
((2021, 4, 10), (5781, NISAN, 28)), |
| 492 |
((2021, 4, 25), (5781, IYYAR, 13)), |
| 493 |
((2021, 5, 10), (5781, IYYAR, 28)), |
| 494 |
((2021, 5, 25), (5781, SIVAN, 14)), |
| 495 |
((2021, 6, 10), (5781, SIVAN, 30)), |
| 496 |
((2021, 6, 25), (5781, TAMMUZ, 15)), |
| 497 |
((2021, 7, 10), (5781, AV, 1)), |
| 498 |
((2021, 7, 25), (5781, AV, 16)), |
| 499 |
((2021, 8, 10), (5781, ELUL, 2)), |
| 500 |
((2021, 8, 25), (5781, ELUL, 17)), |
| 501 |
((2021, 9, 10), (5782, TISHREI, 4)), |
| 502 |
((2021, 9, 25), (5782, TISHREI, 19)), |
| 503 |
((2021, 10, 10), (5782, ḤESHVAN, 4)), |
| 504 |
((2021, 10, 25), (5782, ḤESHVAN, 19)), |
| 505 |
((2021, 11, 10), (5782, KISLEV, 6)), |
| 506 |
((2021, 11, 25), (5782, KISLEV, 21)), |
| 507 |
((2021, 12, 10), (5782, TEVET, 6)), |
| 508 |
((2021, 12, 25), (5782, TEVET, 21)), |
| 509 |
((2022, 1, 10), (5782, SHEVAT, 8)), |
| 510 |
((2022, 1, 25), (5782, SHEVAT, 23)), |
| 511 |
((2022, 2, 10), (5782, ADARI, 9)), |
| 512 |
((2022, 2, 25), (5782, ADARI, 24)), |
| 513 |
((2022, 3, 10), (5782, ADAR, 7)), |
| 514 |
((2022, 3, 25), (5782, ADAR, 22)), |
| 515 |
((2022, 4, 10), (5782, NISAN, 9)), |
| 516 |
((2022, 4, 25), (5782, NISAN, 24)), |
| 517 |
((2022, 5, 10), (5782, IYYAR, 9)), |
| 518 |
((2022, 5, 25), (5782, IYYAR, 24)), |
| 519 |
((2022, 6, 10), (5782, SIVAN, 11)), |
| 520 |
((2022, 6, 25), (5782, SIVAN, 26)), |
| 521 |
((2022, 7, 10), (5782, TAMMUZ, 11)), |
| 522 |
((2022, 7, 25), (5782, TAMMUZ, 26)), |
| 523 |
((2022, 8, 10), (5782, AV, 13)), |
| 524 |
((2022, 8, 25), (5782, AV, 28)), |
| 525 |
((2022, 9, 10), (5782, ELUL, 14)), |
| 526 |
((2022, 9, 25), (5782, ELUL, 29)), |
| 527 |
((2022, 10, 10), (5783, TISHREI, 15)), |
| 528 |
((2022, 10, 25), (5783, TISHREI, 30)), |
| 529 |
((2022, 11, 10), (5783, ḤESHVAN, 16)), |
| 530 |
((2022, 11, 25), (5783, KISLEV, 1)), |
| 531 |
((2022, 12, 10), (5783, KISLEV, 16)), |
| 532 |
((2022, 12, 25), (5783, TEVET, 1)), |
| 533 |
]; |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn test_conversions() { |
| 537 |
for ((iso_y, iso_m, iso_d), (y, m, d)) in ISO_HEBREW_DATE_PAIRS.into_iter() { |
| 538 |
let rd = Date::try_new_iso(iso_y, iso_m, iso_d) |
| 539 |
.unwrap() |
| 540 |
.to_rata_die(); |
| 541 |
|
| 542 |
let date = Date::from_rata_die(rd, Hebrew); |
| 543 |
|
| 544 |
assert_eq!(date.to_rata_die(), rd, "{date:?}"); |
| 545 |
|
| 546 |
assert_eq!(date.era_year().year, y, "{date:?}"); |
| 547 |
assert_eq!( |
| 548 |
date.month().ordinal, |
| 549 |
if (m == ADARI || m.number() >= ADAR.number()) && LEAP_YEARS_IN_TESTS.contains(&y) { |
| 550 |
m.number() + 1 |
| 551 |
} else { |
| 552 |
assert!(m != ADARI); |
| 553 |
m.number() |
| 554 |
}, |
| 555 |
"{date:?}" |
| 556 |
); |
| 557 |
assert_eq!(date.day_of_month().0, d, "{date:?}"); |
| 558 |
|
| 559 |
assert_eq!( |
| 560 |
Date::try_new( |
| 561 |
types::YearInput::EraYear(&date.era_year().era, date.era_year().year), |
| 562 |
date.month().to_input(), |
| 563 |
date.day_of_month().0, |
| 564 |
Hebrew |
| 565 |
), |
| 566 |
Ok(date) |
| 567 |
); |
| 568 |
|
| 569 |
assert_eq!( |
| 570 |
Date::try_new_hebrew_v2( |
| 571 |
date.era_year().year, |
| 572 |
date.month().to_input(), |
| 573 |
date.day_of_month().0, |
| 574 |
), |
| 575 |
Ok(date) |
| 576 |
); |
| 577 |
|
| 578 |
#[allow(deprecated)] // should still test |
| 579 |
{ |
| 580 |
assert_eq!( |
| 581 |
Date::try_new_hebrew( |
| 582 |
date.era_year().extended_year, |
| 583 |
date.month().ordinal, |
| 584 |
date.day_of_month().0 |
| 585 |
), |
| 586 |
Ok(date) |
| 587 |
); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn test_icu_bug_22441() { |
| 594 |
let yi = HebrewYear::compute(88369); |
| 595 |
assert_eq!(yi.keviyah.year_length(), 383); |
| 596 |
} |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn test_negative_era_years() { |
| 600 |
let greg_date = Date::try_new_gregorian(-5000, 1, 1).unwrap(); |
| 601 |
let greg_year = greg_date.era_year(); |
| 602 |
assert_eq!(greg_year.extended_year, -5000); |
| 603 |
assert_eq!(greg_year.era, "bce"); |
| 604 |
// In Gregorian, era year is 1 - extended year |
| 605 |
assert_eq!(greg_year.year, 5001); |
| 606 |
let hebr_date = greg_date.to_calendar(Hebrew); |
| 607 |
let hebr_year = hebr_date.era_year(); |
| 608 |
assert_eq!(hebr_year.extended_year, -1240); |
| 609 |
assert_eq!(hebr_year.era, "am"); |
| 610 |
// In Hebrew, there is no inverse era, so negative extended years are negative era years |
| 611 |
assert_eq!(hebr_year.year, -1240); |
| 612 |
} |
| 613 |
|
| 614 |
#[test] |
| 615 |
fn test_weekdays() { |
| 616 |
// https://github.com/unicode-org/icu4x/issues/4893 |
| 617 |
let dt = Date::try_new_hebrew_v2(3760, Month::new(1), 1).unwrap(); |
| 618 |
|
| 619 |
// Should be Saturday per: |
| 620 |
// https://www.hebcal.com/converter?hd=1&hm=Tishrei&hy=3760&h2g=1 |
| 621 |
assert_eq!(dt.weekday(), Weekday::Saturday); |
| 622 |
} |
| 623 |
|
| 624 |
#[test] |
| 625 |
fn test_constructor_roundtrip() { |
| 626 |
let rds = crate::tests::get_interesting_rds(); |
| 627 |
for rd in rds { |
| 628 |
let date = Date::from_rata_die(rd, Hebrew); |
| 629 |
let reconstructed = Date::try_new_hebrew_v2( |
| 630 |
date.year().extended_year(), |
| 631 |
date.month().to_input(), |
| 632 |
date.day_of_month().0, |
| 633 |
) |
| 634 |
.unwrap(); |
| 635 |
assert_eq!( |
| 636 |
reconstructed.to_rata_die(), |
| 637 |
rd, |
| 638 |
"Hebrew failed for RD {:?}", |
| 639 |
rd |
| 640 |
); |
| 641 |
} |
| 642 |
} |
| 643 |
} |