usecrate::calendar_arithmetic::ArithmeticDate; usecrate::calendar_arithmetic::DateFieldsResolver; usecrate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError}; usecrate::options::DateFromFieldsOptions; usecrate::options::{DateAddOptions, DateDifferenceOptions}; usecrate::types::DateFields; usecrate::{types, Calendar, Date, RangeError}; use calendrical_calculations::helpers::I32CastError; use calendrical_calculations::rata_die::RataDie; use tinystr::tinystr;
/// The [Julian Calendar](https://en.wikipedia.org/wiki/Julian_calendar). /// /// The Julian calendar is a solar calendar that was introduced in the Roman Republic under /// Julius Caesar in 45 BCE, and used in Europe and much of the western world until it was /// eventually replaced by the more accurate [`Gregorian`](super::Gregorian) calendar. /// /// This implementation extends proleptically for dates before the calendar's creation. /// /// While no country uses the Julian calendar as its civil calendar today, it is still /// used by eastern Christian churches to determine lithurgical dates like Christmas and /// Easter. /// /// # Era codes /// /// This calendar uses two era codes: `bce` (alias `bc`), and `ce` (alias `ad`), corresponding to the BCE and CE eras. /// /// # Months and days /// /// The 12 months are called January (`M01`, 31 days), February (`M02`, 28 days), /// March (`M03`, 31 days), April (`M04`, 30 days), May (`M05`, 31 days), June (`M06`, 30 days), /// July (`M07`, 31 days), August (`M08`, 31 days), September (`M09`, 30 days), /// October (`M10`, 31 days), November (`M11`, 30 days), December (`M12`, 31 days). /// /// In leap years (years divisible by 4), February gains a 29th day. /// /// Standard years thus have 365 days, and leap years 366. /// /// # Calendar drift /// /// The Julian calendar has an average year length of 365.25, slightly longer than /// the mean solar year, so this calendar drifts 1 day in ~128 years with /// respect to the seasons. This significant drift was the reason for its replacement /// by the Gregorian calendar. The Julian calendar is currently 14 days ahead of the /// Gregorian calendar and the solar year. /// /// # Historical accuracy /// /// Historically, a variety of year reckoning schemes have been used with the Julian /// calendar, such as Roman consular years, regnal years, [indictions]( /// https://en.wikipedia.org/wiki/Indiction), [Anno Mundi]( /// https://en.wikipedia.org/wiki/Anno_Mundi#Byzantine_era), the [Diocletian era]( /// https://en.wikipedia.org/wiki/Era_of_the_Martyrs), [Anno Domini]( /// https://en.wikipedia.org/wiki/Anno_Domini), and the (equivalent) [Common era]( /// https://en.wikipedia.org/wiki/Common_Era). /// The latter, which is used today and by this implementation, has been used by /// western European authorities since the early middle ages, however some eastern /// European countries/churches have not adopted it until fairly recently, or, in /// some cases, are still using a different year reckoning scheme. /// /// Also during the middle ages, [some countries](https://en.wikipedia.org/wiki/New_Year#Historical_European_new_year_dates) /// used different dates for the first day of the year, ranging from late December to /// late March. Care has to be taken when interpreting year numbers with dates in this /// range. /// /// The calendar was used [incorrectly](https://en.wikipedia.org/wiki/Julian_calendar#Leap_year_error) /// for a while after adoption, so the first year where the months align with this proleptic /// implementation is probably 4 CE. #[derive(Copy, Clone, Debug, Hash, Default, Eq, PartialEq, PartialOrd, Ord)] #[allow(clippy::exhaustive_structs)] // this type is stable pubstruct Julian;
/// The inner date type used for representing [`Date`]s of [`Julian`]. See [`Date`] and [`Julian`] for more details. #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] // The inner date type used for representing Date<Julian> pubstruct JulianDateInner(pub(crate) ArithmeticDate<Julian>);
impl DateFieldsResolver for Julian { type YearInfo = i32;
#[inline] fn reference_year_from_month_day(
&self,
month_code: types::ValidMonthCode,
day: u8,
) -> Result<Self::YearInfo, EcmaReferenceYearError> { let (ordinal_month, false) = month_code.to_tuple() else { return Err(EcmaReferenceYearError::MonthCodeNotInCalendar);
}; // December 31, 1972 occurs on 12th month, 18th day, 1972 Old Style // Note: 1972 is a leap year let julian_year = if ordinal_month < 12 || (ordinal_month == 12 && day <= 18) { 1972
} else { 1971
};
Ok(julian_year)
}
}
implcrate::cal::scaffold::UnstableSealed for Julian {} impl Calendar for Julian { type DateInner = JulianDateInner; type Year = types::EraYear; type DifferenceError = core::convert::Infallible;
impl Julian { /// Construct a new Julian Calendar pubfn new() -> Self { Self
}
/// Returns the date of (Orthodox) Easter in the given year. pubfn easter(year: i32) -> Date<Self> {
Date::from_rata_die(calendrical_calculations::julian::easter(year), Self)
}
}
impl Date<Julian> { /// Construct new Julian Date. /// /// Years are arithmetic, meaning there is a year 0. Zero and negative years are in BC, with year 0 = 1 BC /// /// ```rust /// use icu::calendar::Date; /// /// let date_julian = Date::try_new_julian(1969, 12, 20) /// .expect("Failed to initialize Julian Date instance."); /// /// assert_eq!(date_julian.era_year().year, 1969); /// assert_eq!(date_julian.month().ordinal, 12); /// assert_eq!(date_julian.day_of_month().0, 20); /// ``` pubfn try_new_julian(year: i32, month: u8, day: u8) -> Result<Date<Julian>, RangeError> {
ArithmeticDate::try_from_ymd(year, month, day)
.map(JulianDateInner)
.map(|inner| Date::from_raw(inner, Julian))
}
}
#[cfg(test)] mod test { usesuper::*;
#[test] fn test_day_iso_to_julian() { // March 1st 200 is same on both calendars let iso_date = Date::try_new_iso(200, 3, 1).unwrap(); let julian_date = Date::new_from_iso(iso_date, Julian).inner;
assert_eq!(julian_date.0.year, 200);
assert_eq!(julian_date.0.month, 3);
assert_eq!(julian_date.0.day, 1);
// Feb 28th, 200 (iso) = Feb 29th, 200 (julian) let iso_date = Date::try_new_iso(200, 2, 28).unwrap(); let julian_date = Date::new_from_iso(iso_date, Julian).inner;
assert_eq!(julian_date.0.year, 200);
assert_eq!(julian_date.0.month, 2);
assert_eq!(julian_date.0.day, 29);
// March 1st 400 (iso) = Feb 29th, 400 (julian) let iso_date = Date::try_new_iso(400, 3, 1).unwrap(); let julian_date = Date::new_from_iso(iso_date, Julian).inner;
assert_eq!(julian_date.0.year, 400);
assert_eq!(julian_date.0.month, 2);
assert_eq!(julian_date.0.day, 29);
// Jan 1st, 2022 (iso) = Dec 19, 2021 (julian) let iso_date = Date::try_new_iso(2022, 1, 1).unwrap(); let julian_date = Date::new_from_iso(iso_date, Julian).inner;
assert_eq!(julian_date.0.year, 2021);
assert_eq!(julian_date.0.month, 12);
assert_eq!(julian_date.0.day, 19);
}
#[test] fn test_day_julian_to_iso() { // March 1st 200 is same on both calendars let julian_date = Date::try_new_julian(200, 3, 1).unwrap(); let iso_date = julian_date.to_iso(); let iso_expected_date = Date::try_new_iso(200, 3, 1).unwrap();
assert_eq!(iso_date, iso_expected_date);
// Feb 28th, 200 (iso) = Feb 29th, 200 (julian) let julian_date = Date::try_new_julian(200, 2, 29).unwrap(); let iso_date = julian_date.to_iso(); let iso_expected_date = Date::try_new_iso(200, 2, 28).unwrap();
assert_eq!(iso_date, iso_expected_date);
// March 1st 400 (iso) = Feb 29th, 400 (julian) let julian_date = Date::try_new_julian(400, 2, 29).unwrap(); let iso_date = julian_date.to_iso(); let iso_expected_date = Date::try_new_iso(400, 3, 1).unwrap();
assert_eq!(iso_date, iso_expected_date);
// Jan 1st, 2022 (iso) = Dec 19, 2021 (julian) let julian_date = Date::try_new_julian(2021, 12, 19).unwrap(); let iso_date = julian_date.to_iso(); let iso_expected_date = Date::try_new_iso(2022, 1, 1).unwrap();
assert_eq!(iso_date, iso_expected_date);
// March 1st, 2022 (iso) = Feb 16, 2022 (julian) let julian_date = Date::try_new_julian(2022, 2, 16).unwrap(); let iso_date = julian_date.to_iso(); let iso_expected_date = Date::try_new_iso(2022, 3, 1).unwrap();
assert_eq!(iso_date, iso_expected_date);
}
#[test] fn test_roundtrip_negative() { // https://github.com/unicode-org/icu4x/issues/2254 let iso_date = Date::try_new_iso(-1000, 3, 3).unwrap(); let julian = iso_date.to_calendar(Julian::new()); let recovered_iso = julian.to_iso();
assert_eq!(iso_date, recovered_iso);
}
#[test] fn test_julian_near_era_change() { // Tests that the Julian calendar gives the correct expected // day, month, and year for positive years (CE)
for case in cases { let iso_from_rd = Date::from_rata_die(RataDie::new(case.rd), crate::Iso); let julian_from_rd = Date::from_rata_die(RataDie::new(case.rd), Julian);
assert_eq!(julian_from_rd.era_year().year, case.expected_year, "Failed year check from RD: {case:?}\nISO: {iso_from_rd:?}\nJulian: {julian_from_rd:?}");
assert_eq!(julian_from_rd.era_year().era, case.expected_era, "Failed era check from RD: {case:?}\nISO: {iso_from_rd:?}\nJulian: {julian_from_rd:?}");
assert_eq!(julian_from_rd.month().ordinal, case.expected_month, "Failed month check from RD: {case:?}\nISO: {iso_from_rd:?}\nJulian: {julian_from_rd:?}");
assert_eq!(julian_from_rd.day_of_month().0, case.expected_day, "Failed day check from RD: {case:?}\nISO: {iso_from_rd:?}\nJulian: {julian_from_rd:?}");
let iso_date_man = Date::try_new_iso(case.iso_year, case.iso_month, case.iso_day)
.expect("Failed to initialize ISO date for {case:?}"); let julian_date_man = Date::new_from_iso(iso_date_man, Julian);
assert_eq!(iso_from_rd, iso_date_man, "ISO from RD not equal to ISO generated from manually-input ymd\nCase: {case:?}\nRD: {iso_from_rd:?}\nMan: {iso_date_man:?}");
assert_eq!(julian_from_rd, julian_date_man, "Julian from RD not equal to Julian generated from manually-input ymd\nCase: {case:?}\nRD: {julian_from_rd:?}\nMan: {julian_date_man:?}");
}
}
#[test] fn test_julian_rd_date_conversion() { // Tests that converting from RD to Julian then // back to RD yields the same RD for i in -10000..=10000 { let rd = RataDie::new(i); let julian = Date::from_rata_die(rd, Julian); let new_rd = julian.to_rata_die();
assert_eq!(rd, new_rd);
}
}
#[test] fn test_julian_directionality() { // Tests that for a large range of RDs, if a RD // is less than another, the corresponding YMD should also be less // than the other, without exception. for i in -100..=100 { for j in -100..=100 { let julian_i = Date::from_rata_die(RataDie::new(i), Julian); let julian_j = Date::from_rata_die(RataDie::new(j), Julian);
assert_eq!(
i.cmp(&j),
julian_i.inner.0.cmp(&julian_j.inner.0), "Julian directionality inconsistent with directionality for i: {i}, j: {j}"
);
}
}
}
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.