use crate::cal::abstract_gregorian::{impl_with_abstract_gregorian, GregorianYears};
use crate::calendar_arithmetic::ArithmeticDate;
use crate::error::UnknownEraError;
use crate::preferences::CalendarAlgorithm;
use crate::{types, Date, DateError, RangeError};
use tinystr::tinystr;
impl_with_abstract_gregorian!(crate::cal::Gregorian, GregorianDateInner, CeBce, _x, CeBce);
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct CeBce;
impl GregorianYears for CeBce {
fn extended_from_era_year(
&self,
era: Option<&[u8]>,
year: i32,
) -> Result<i32, UnknownEraError> {
match era {
None => Ok(year),
Some(b"ad" | b"ce") => Ok(year),
Some(b"bce" | b"bc") => Ok(1 - year),
Some(_) => Err(UnknownEraError),
}
}
fn era_year_from_extended(&self, extended_year: i32, _month: u8, _day: u8) -> types::EraYear {
if extended_year > 0 {
types::EraYear {
era: tinystr!(16, "ce"),
era_index: Some(1),
year: extended_year,
extended_year,
ambiguity: match extended_year {
..=999 => types::YearAmbiguity::EraAndCenturyRequired,
1000..=1949 => types::YearAmbiguity::CenturyRequired,
1950..=2049 => types::YearAmbiguity::Unambiguous,
2050.. => types::YearAmbiguity::CenturyRequired,
},
}
} else {
types::EraYear {
era: tinystr!(16, "bce"),
era_index: Some(0),
year: 1 - extended_year,
extended_year,
ambiguity: types::YearAmbiguity::EraAndCenturyRequired,
}
}
}
fn debug_name(&self) -> &'static str {
"Gregorian"
}
fn calendar_algorithm(&self) -> Option<CalendarAlgorithm> {
Some(CalendarAlgorithm::Gregory)
}
}
#[derive(Copy, Clone, Debug, Default)]
#[allow(clippy::exhaustive_structs)] pub struct Gregorian;
impl Date<Gregorian> {
pub fn try_new_gregorian(year: i32, month: u8, day: u8) -> Result<Date<Gregorian>, RangeError> {
ArithmeticDate::new_gregorian::<CeBce>(year, month, day)
.map(GregorianDateInner)
.map(|i| Date::from_raw(i, Gregorian))
}
}
impl Gregorian {
pub fn easter(year: i32) -> Date<Self> {
Date::from_rata_die(calendrical_calculations::gregorian::easter(year), Self)
}
}
#[cfg(test)]
mod test {
use crate::cal::Iso;
use calendrical_calculations::rata_die::RataDie;
use super::*;
#[derive(Debug)]
struct TestCase {
rd: RataDie,
iso_year: i32,
iso_month: u8,
iso_day: u8,
expected_year: i32,
expected_era: &'static str,
expected_month: u8,
expected_day: u8,
}
fn check_test_case(case: TestCase) {
let iso_from_rd = Date::from_rata_die(case.rd, Iso);
let greg_date_from_rd = Date::from_rata_die(case.rd, Gregorian);
assert_eq!(greg_date_from_rd.era_year().year, case.expected_year,
"Failed year check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}");
assert_eq!(
greg_date_from_rd.era_year().era,
case.expected_era,
"Failed era check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}"
);
assert_eq!(greg_date_from_rd.month().ordinal, case.expected_month,
"Failed month check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}");
assert_eq!(
greg_date_from_rd.day_of_month().0,
case.expected_day,
"Failed day check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}"
);
let iso_date_man: Date<Iso> =
Date::try_new_iso(case.iso_year, case.iso_month, case.iso_day)
.expect("Failed to initialize ISO date for {case:?}");
let greg_date_man: Date<Gregorian> = Date::new_from_iso(iso_date_man, Gregorian);
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!(greg_date_from_rd, greg_date_man,
"Greg. date from RD not equal to Greg. generated from manually-input ymd\nCase: {case:?}\nRD: {greg_date_from_rd:?}\nMan: {greg_date_man:?}");
}
#[test]
fn test_gregorian_ce() {
let cases = [
TestCase {
rd: RataDie::new(1),
iso_year: 1,
iso_month: 1,
iso_day: 1,
expected_year: 1,
expected_era: "ce",
expected_month: 1,
expected_day: 1,
},
TestCase {
rd: RataDie::new(181),
iso_year: 1,
iso_month: 6,
iso_day: 30,
expected_year: 1,
expected_era: "ce",
expected_month: 6,
expected_day: 30,
},
TestCase {
rd: RataDie::new(1155),
iso_year: 4,
iso_month: 2,
iso_day: 29,
expected_year: 4,
expected_era: "ce",
expected_month: 2,
expected_day: 29,
},
TestCase {
rd: RataDie::new(1344),
iso_year: 4,
iso_month: 9,
iso_day: 5,
expected_year: 4,
expected_era: "ce",
expected_month: 9,
expected_day: 5,
},
TestCase {
rd: RataDie::new(36219),
iso_year: 100,
iso_month: 3,
iso_day: 1,
expected_year: 100,
expected_era: "ce",
expected_month: 3,
expected_day: 1,
},
];
for case in cases {
check_test_case(case);
}
}
#[test]
fn test_gregorian_bce() {
let cases = [
TestCase {
rd: RataDie::new(0),
iso_year: 0,
iso_month: 12,
iso_day: 31,
expected_year: 1,
expected_era: "bce",
expected_month: 12,
expected_day: 31,
},
TestCase {
rd: RataDie::new(-365), iso_year: 0,
iso_month: 1,
iso_day: 1,
expected_year: 1,
expected_era: "bce",
expected_month: 1,
expected_day: 1,
},
TestCase {
rd: RataDie::new(-366),
iso_year: -1,
iso_month: 12,
iso_day: 31,
expected_year: 2,
expected_era: "bce",
expected_month: 12,
expected_day: 31,
},
TestCase {
rd: RataDie::new(-1461),
iso_year: -4,
iso_month: 12,
iso_day: 31,
expected_year: 5,
expected_era: "bce",
expected_month: 12,
expected_day: 31,
},
TestCase {
rd: RataDie::new(-1826),
iso_year: -4,
iso_month: 1,
iso_day: 1,
expected_year: 5,
expected_era: "bce",
expected_month: 1,
expected_day: 1,
},
];
for case in cases {
check_test_case(case);
}
}
#[test]
fn check_gregorian_directionality() {
for i in -100..100 {
for j in -100..100 {
let iso_i = Date::from_rata_die(RataDie::new(i), Iso);
let iso_j = Date::from_rata_die(RataDie::new(j), Iso);
let greg_i = iso_i.to_calendar(Gregorian);
let greg_j = iso_j.to_calendar(Gregorian);
assert_eq!(
i.cmp(&j),
iso_i.cmp(&iso_j),
"ISO directionality inconsistent with directionality for i: {i}, j: {j}"
);
assert_eq!(
i.cmp(&j),
greg_i.cmp(&greg_j),
"Gregorian directionality inconsistent with directionality for i: {i}, j: {j}"
);
}
}
}
}