//! This module contains types and implementations for the Hebrew calendar. //! //! ```rust //! use icu::calendar::{Date, DateTime}; //! //! // `Date` type //! let hebrew_date = Date::try_new_hebrew_date(3425, 10, 11) //! .expect("Failed to initialize hebrew Date instance."); //! //! // `DateTime` type //! let hebrew_datetime = //! DateTime::try_new_hebrew_datetime(3425, 10, 11, 13, 1, 0) //! .expect("Failed to initialize hebrew DateTime instance."); //! //! // `Date` checks //! assert_eq!(hebrew_date.year().number, 3425); //! assert_eq!(hebrew_date.month().ordinal, 10); //! assert_eq!(hebrew_date.day_of_month().0, 11); //! //! // `DateTime` checks //! assert_eq!(hebrew_datetime.date.year().number, 3425); //! assert_eq!(hebrew_datetime.date.month().ordinal, 10); //! assert_eq!(hebrew_datetime.date.day_of_month().0, 11); //! assert_eq!(hebrew_datetime.time.hour.number(), 13); //! assert_eq!(hebrew_datetime.time.minute.number(), 1); //! assert_eq!(hebrew_datetime.time.second.number(), 0); //! ```
usecrate::calendar_arithmetic::PrecomputedDataSource; usecrate::calendar_arithmetic::{ArithmeticDate, CalendarArithmetic}; usecrate::types::FormattableMonth; usecrate::AnyCalendarKind; usecrate::AsCalendar; usecrate::Iso; usecrate::{types, Calendar, CalendarError, Date, DateDuration, DateDurationUnit, DateTime, Time}; use ::tinystr::tinystr; use calendrical_calculations::hebrew_keviyah::{Keviyah, YearInfo};
/// The Civil Hebrew Calendar /// /// The [Hebrew calendar] is a lunisolar calendar used as the Jewish liturgical calendar /// as well as an official calendar in Israel. /// /// This calendar is the _civil_ Hebrew calendar, with the year starting at in the month of Tishrei. /// /// # Era codes /// /// This calendar supports a single era code, Anno Mundi, with code `"am"` /// /// # Month codes /// /// This calendar is a lunisolar calendar and thus has a leap month. It supports codes `"M01"-"M12"` /// for regular months, and the leap month Adar I being coded as `"M05L"`. /// /// [`FormattableMonth`] has slightly divergent behavior: because the regular month Adar is formatted /// as "Adar II" in a leap year, this calendar will produce the special code `"M06L"` in any [`FormattableMonth`] /// objects it creates. /// /// [Hebrew calendar]: https://en.wikipedia.org/wiki/Hebrew_calendar #[derive(Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Default)] #[allow(clippy::exhaustive_structs)] // unit struct pubstruct Hebrew;
/// The inner date type used for representing [`Date`]s of [`Hebrew`]. See [`Date`] and [`Hebrew`] for more details. #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] pubstruct HebrewDateInner(ArithmeticDate<Hebrew>);
impl Hebrew { /// Construct a new [`Hebrew`] pubfn new() -> Self {
Hebrew
}
/// Construct a new [`Hebrew`] /// /// This is deprecated since the new implementation does not need precomputed data. #[deprecated(since = "1.5.0", note = "Use Hebrew::new()")] pubfn new_always_calculating() -> Self {
Hebrew
}
}
impl HebrewYearInfo { /// Convenience method to compute for a given year. Don't use this if you actually need /// a YearInfo that you want to call .new_year() on. /// /// This can potentially be optimized with adjacent-year knowledge, but it's complex #[inline] fn compute(h_year: i32) -> Self { let keviyah = YearInfo::compute_for(h_year).keviyah; Self::compute_with_keviyah(keviyah, h_year)
} /// Compute for a given year when the keviyah is already known #[inline] fn compute_with_keviyah(keviyah: Keviyah, h_year: i32) -> Self { let prev_keviyah = YearInfo::compute_for(h_year - 1).keviyah; Self {
keviyah,
prev_keviyah,
}
}
} // HEBREW CALENDAR
impl CalendarArithmetic for Hebrew { type YearInfo = HebrewYearInfo;
fn date_from_iso(&self, iso: Date<Iso>) -> Self::DateInner { let fixed_iso = Iso::fixed_from_iso(*iso.inner()); let (year_info, h_year) = YearInfo::year_containing_rd(fixed_iso); // Obtaining a 1-indexed day-in-year value let day = fixed_iso - year_info.new_year() + 1; let day = u16::try_from(day).unwrap_or(u16::MAX);
let year_info = HebrewYearInfo::compute_with_keviyah(year_info.keviyah, h_year); let (month, day) = year_info.keviyah.month_day_for(day);
HebrewDateInner(ArithmeticDate::new_unchecked_with_info(
h_year, month, day, year_info,
))
}
impl Date<Hebrew> { /// Construct new Hebrew Date. /// /// This datetime will not use any precomputed calendrical calculations, /// one that loads such data from a provider will be added in the future (#3933) /// /// /// ```rust /// use icu::calendar::Date; /// /// let date_hebrew = Date::try_new_hebrew_date(3425, 4, 25) /// .expect("Failed to initialize Hebrew Date instance."); /// /// assert_eq!(date_hebrew.year().number, 3425); /// assert_eq!(date_hebrew.month().ordinal, 4); /// assert_eq!(date_hebrew.day_of_month().0, 25); /// ``` pubfn try_new_hebrew_date(
year: i32,
month: u8,
day: u8,
) -> Result<Date<Hebrew>, CalendarError> { let year_info = HebrewYearInfo::compute(year);
impl<A: AsCalendar<Calendar = Hebrew>> Date<A> { /// Construct new Hebrew Date given a calendar. /// /// This is deprecated since `Hebrew` is a zero-sized type, /// but if you find yourself needing this functionality please let us know. #[deprecated(since = "1.5.0", note = "Use Date::try_new_hebrew_date()")] pubfn try_new_hebrew_date_with_calendar(
year: i32,
month: u8,
day: u8,
calendar: A,
) -> Result<Date<A>, CalendarError> { let year_info = HebrewYearInfo::compute(year);
impl<A: AsCalendar<Calendar = Hebrew>> DateTime<A> { /// Construct new Hebrew DateTime given a calendar. /// /// This is deprecated since `Hebrew` is a zero-sized type, /// but if you find yourself needing this functionality please let us know. #[deprecated(since = "1.5.0", note = "Use DateTime::try_new_hebrew_datetime()")] pubfn try_new_hebrew_datetime_with_calendar(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
calendar: A,
) -> Result<DateTime<A>, CalendarError> { #[allow(deprecated)]
Ok(DateTime {
date: Date::try_new_hebrew_date_with_calendar(year, month, day, calendar)?,
time: Time::try_new(hour, minute, second, 0)?,
})
}
}
#[cfg(test)] mod tests {
usesuper::*; usecrate::types::{Era, MonthCode}; use calendrical_calculations::hebrew_keviyah::*;
// Sentinel value for Adar I // We're using normalized month values here so that we can use constants. These do not // distinguish between the different Adars. We add an out-of-range sentinel value of 13 to // specifically talk about Adar I in a leap year const ADARI: u8 = 13;
#[test] fn test_conversions() { for ((iso_y, iso_m, iso_d), (y, m, d)) in ISO_HEBREW_DATE_PAIRS.into_iter() { let iso_date = Date::try_new_iso_date(iso_y, iso_m, iso_d).unwrap(); let month_code = if m == ADARI {
MonthCode(tinystr!(4, "M05L"))
} else {
MonthCode::new_normal(m).unwrap()
}; let hebrew_date =
Date::try_new_from_codes(tinystr!(16, "am").into(), y, month_code, d, Hebrew)
.expect("Date should parse");
let iso_to_hebrew = iso_date.to_calendar(Hebrew);
let hebrew_to_iso = hebrew_date.to_calendar(Iso);
assert_eq!(
hebrew_to_iso, iso_date, "Failed comparing to-ISO value for {hebrew_date:?} => {iso_date:?}"
);
assert_eq!(
iso_to_hebrew, hebrew_date, "Failed comparing to-hebrew value for {iso_date:?} => {hebrew_date:?}"
);
let ordinal_month = if LEAP_YEARS_IN_TESTS.contains(&y) { if m == ADARI {
ADAR
} elseif m >= ADAR {
m + 1
} else {
m
}
} else {
assert!(m != ADARI);
m
};
let ordinal_hebrew_date = Date::try_new_hebrew_date(y, ordinal_month, d)
.expect("Construction of date must succeed");
assert_eq!(ordinal_hebrew_date, hebrew_date, "Hebrew date construction from codes and ordinals should work the same for {hebrew_date:?}");
}
}
#[test] fn test_icu_bug_22441() { let yi = YearInfo::compute_for(88369);
assert_eq!(yi.keviyah.year_length(), 383);
}
#[test] fn test_weekdays() { // https://github.com/unicode-org/icu4x/issues/4893 let cal = Hebrew::new(); let era = "am".parse::<Era>().unwrap(); let month_code = "M01".parse::<MonthCode>().unwrap(); let dt = cal.date_from_codes(era, 3760, month_code, 1).unwrap();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.