use crate::cal::coptic::CopticDateInner;
use crate::cal::Coptic;
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
use crate::error::{
DateError, DateFromFieldsError, EcmaReferenceYearError, MonthCodeError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::types::DateFields;
use crate::{types, Calendar, Date, RangeError};
use calendrical_calculations::rata_die::RataDie;
use tinystr::tinystr;
const AMETE_MIHRET_OFFSET: i32 = -276;
const AMETE_ALEM_OFFSET: i32 = -5776;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
#[non_exhaustive]
pub enum EthiopianEraStyle {
AmeteMihret,
AmeteAlem,
}
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Ethiopian(EthiopianEraStyle);
impl Default for Ethiopian {
fn default() -> Self {
Self(EthiopianEraStyle::AmeteMihret)
}
}
#[allow(missing_docs)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct EthiopianDateInner(CopticDateInner);
impl DateFieldsResolver for Ethiopian {
type YearInfo = i32;
fn days_in_provided_month(year: Self::YearInfo, month: u8) -> u8 {
Coptic::days_in_provided_month(year, month)
}
fn months_in_provided_year(year: Self::YearInfo) -> u8 {
Coptic::months_in_provided_year(year)
}
#[inline]
fn year_info_from_era(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
match (self.era_style(), era) {
(EthiopianEraStyle::AmeteMihret, b"am") => Ok(era_year + AMETE_MIHRET_OFFSET),
(_, b"aa") => Ok(era_year + AMETE_ALEM_OFFSET),
(_, _) => Err(UnknownEraError),
}
}
#[inline]
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo {
extended_year
+ if self.0 == EthiopianEraStyle::AmeteMihret {
AMETE_MIHRET_OFFSET
} else {
AMETE_ALEM_OFFSET
}
}
#[inline]
fn reference_year_from_month_day(
&self,
month_code: types::ValidMonthCode,
day: u8,
) -> Result<Self::YearInfo, EcmaReferenceYearError> {
crate::cal::Coptic::reference_year_from_month_day(month_code, day)
}
#[inline]
fn ordinal_month_from_code(
&self,
_year: &Self::YearInfo,
month_code: types::ValidMonthCode,
_options: DateFromFieldsOptions,
) -> Result<u8, MonthCodeError> {
match month_code.to_tuple() {
(month_number @ 1..=13, false) => Ok(month_number),
_ => Err(MonthCodeError::NotInCalendar),
}
}
}
impl crate::cal::scaffold::UnstableSealed for Ethiopian {}
impl Calendar for Ethiopian {
type DateInner = EthiopianDateInner;
type Year = <Coptic as Calendar>::Year;
type DifferenceError = <Coptic as Calendar>::DifferenceError;
fn from_codes(
&self,
era: Option<&str>,
year: i32,
month_code: types::MonthCode,
day: u8,
) -> Result<Self::DateInner, DateError> {
ArithmeticDate::from_codes(era, year, month_code, day, self)
.map(ArithmeticDate::cast)
.map(CopticDateInner)
.map(EthiopianDateInner)
}
#[cfg(feature = "unstable")]
fn from_fields(
&self,
fields: DateFields,
options: DateFromFieldsOptions,
) -> Result<Self::DateInner, DateFromFieldsError> {
ArithmeticDate::from_fields(fields, options, self)
.map(ArithmeticDate::cast)
.map(CopticDateInner)
.map(EthiopianDateInner)
}
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner {
EthiopianDateInner(Coptic.from_rata_die(rd))
}
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie {
Coptic.to_rata_die(&date.0)
}
fn has_cheap_iso_conversion(&self) -> bool {
false
}
fn months_in_year(&self, date: &Self::DateInner) -> u8 {
Coptic.months_in_year(&date.0)
}
fn days_in_year(&self, date: &Self::DateInner) -> u16 {
Coptic.days_in_year(&date.0)
}
fn days_in_month(&self, date: &Self::DateInner) -> u8 {
Coptic.days_in_month(&date.0)
}
#[cfg(feature = "unstable")]
fn add(
&self,
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
Coptic
.add(&date.0, duration, options)
.map(EthiopianDateInner)
}
#[cfg(feature = "unstable")]
fn until(
&self,
date1: &Self::DateInner,
date2: &Self::DateInner,
options: DateDifferenceOptions,
) -> Result<types::DateDuration, Self::DifferenceError> {
Coptic.until(&date1.0, &date2.0, options)
}
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
let coptic_year = date.0 .0.year;
let extended_year = if self.0 == EthiopianEraStyle::AmeteAlem {
coptic_year - AMETE_ALEM_OFFSET
} else {
coptic_year - AMETE_MIHRET_OFFSET
};
if self.0 == EthiopianEraStyle::AmeteAlem || extended_year <= 0 {
types::EraYear {
era: tinystr!(16, "aa"),
era_index: Some(0),
year: coptic_year - AMETE_ALEM_OFFSET,
extended_year,
ambiguity: types::YearAmbiguity::CenturyRequired,
}
} else {
types::EraYear {
era: tinystr!(16, "am"),
era_index: Some(1),
year: coptic_year - AMETE_MIHRET_OFFSET,
extended_year,
ambiguity: types::YearAmbiguity::CenturyRequired,
}
}
}
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Coptic.is_in_leap_year(&date.0)
}
fn month(&self, date: &Self::DateInner) -> types::MonthInfo {
Coptic.month(&date.0)
}
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth {
Coptic.day_of_month(&date.0)
}
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear {
Coptic.day_of_year(&date.0)
}
fn debug_name(&self) -> &'static str {
"Ethiopian"
}
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm> {
Some(crate::preferences::CalendarAlgorithm::Ethiopic)
}
}
impl Ethiopian {
pub const fn new() -> Self {
Self(EthiopianEraStyle::AmeteMihret)
}
pub const fn new_with_era_style(era_style: EthiopianEraStyle) -> Self {
Self(era_style)
}
pub fn era_style(&self) -> EthiopianEraStyle {
self.0
}
}
impl Date<Ethiopian> {
pub fn try_new_ethiopian(
era_style: EthiopianEraStyle,
year: i32,
month: u8,
day: u8,
) -> Result<Date<Ethiopian>, RangeError> {
let year = Ethiopian(era_style).year_info_from_extended(year);
ArithmeticDate::try_from_ymd(year, month, day)
.map(CopticDateInner)
.map(EthiopianDateInner)
.map(|inner| Date::from_raw(inner, Ethiopian(era_style)))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_leap_year() {
let iso_date = Date::try_new_iso(2023, 9, 11).unwrap();
let date_ethiopian = Date::new_from_iso(iso_date, Ethiopian::new());
assert_eq!(date_ethiopian.extended_year(), 2015);
assert_eq!(date_ethiopian.month().ordinal, 13);
assert_eq!(date_ethiopian.day_of_month().0, 6);
}
#[test]
fn test_iso_to_ethiopian_conversion_and_back() {
let iso_date = Date::try_new_iso(1970, 1, 2).unwrap();
let date_ethiopian = Date::new_from_iso(iso_date, Ethiopian::new());
assert_eq!(date_ethiopian.extended_year(), 1962);
assert_eq!(date_ethiopian.month().ordinal, 4);
assert_eq!(date_ethiopian.day_of_month().0, 24);
assert_eq!(
date_ethiopian.to_iso(),
Date::try_new_iso(1970, 1, 2).unwrap()
);
}
#[test]
fn test_iso_to_ethiopian_aa_conversion_and_back() {
let iso_date = Date::try_new_iso(1970, 1, 2).unwrap();
let date_ethiopian = Date::new_from_iso(iso_date, Ethiopian(EthiopianEraStyle::AmeteAlem));
assert_eq!(date_ethiopian.extended_year(), 7462);
assert_eq!(date_ethiopian.month().ordinal, 4);
assert_eq!(date_ethiopian.day_of_month().0, 24);
assert_eq!(
date_ethiopian.to_iso(),
Date::try_new_iso(1970, 1, 2).unwrap()
);
}
#[test]
fn test_roundtrip_negative() {
let iso_date = Date::try_new_iso(-1000, 3, 3).unwrap();
let ethiopian = iso_date.to_calendar(Ethiopian::new());
let recovered_iso = ethiopian.to_iso();
assert_eq!(iso_date, recovered_iso);
}
#[test]
fn extended_year() {
assert_eq!(
Date::new_from_iso(
Date::try_new_iso(-5500 + 9, 1, 1).unwrap(),
Ethiopian(EthiopianEraStyle::AmeteAlem)
)
.extended_year(),
1
);
assert_eq!(
Date::new_from_iso(
Date::try_new_iso(9, 1, 1).unwrap(),
Ethiopian(EthiopianEraStyle::AmeteAlem)
)
.extended_year(),
5501
);
assert_eq!(
Date::new_from_iso(
Date::try_new_iso(-5500 + 9, 1, 1).unwrap(),
Ethiopian(EthiopianEraStyle::AmeteMihret)
)
.extended_year(),
-5499
);
assert_eq!(
Date::new_from_iso(
Date::try_new_iso(9, 1, 1).unwrap(),
Ethiopian(EthiopianEraStyle::AmeteMihret)
)
.extended_year(),
1
);
}
}