| 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::cal::Coptic; |
| 6 |
use crate::cal::coptic::{CopticDateInner, CopticYear}; |
| 7 |
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver}; |
| 8 |
use crate::error::{ |
| 9 |
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, UnknownEraError, |
| 10 |
}; |
| 11 |
use crate::options::DateFromFieldsOptions; |
| 12 |
use crate::options::{DateAddOptions, DateDifferenceOptions}; |
| 13 |
use crate::types::DateFields; |
| 14 |
use crate::{Calendar, Date, RangeError, types}; |
| 15 |
use calendrical_calculations::rata_die::RataDie; |
| 16 |
use tinystr::tinystr; |
| 17 |
|
| 18 |
/// The Coptic year of the Amete Mihret epoch |
| 19 |
const AMETE_MIHRET_OFFSET: i32 = -276; |
| 20 |
|
| 21 |
/// The Coptic year of the Amete Alem epoch |
| 22 |
const AMETE_ALEM_OFFSET: i32 = -5776; |
| 23 |
|
| 24 |
/// Which era style the ethiopian calendar uses |
| 25 |
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] |
| 26 |
#[non_exhaustive] |
| 27 |
pub enum EthiopianEraStyle { |
| 28 |
/// Use the Anno Mundi era, anchored at the date of Creation, followed by the |
| 29 |
/// Incarnation era, anchored at the date of the Incarnation of Jesus |
| 30 |
AmeteMihret, |
| 31 |
/// Use the single Anno Mundi era, anchored at the date of Creation |
| 32 |
AmeteAlem, |
| 33 |
} |
| 34 |
|
| 35 |
impl EthiopianEraStyle { |
| 36 |
pub(crate) fn coptic_year_offset(self) -> i32 { |
| 37 |
match self { |
| 38 |
Self::AmeteMihret => AMETE_MIHRET_OFFSET, |
| 39 |
Self::AmeteAlem => AMETE_ALEM_OFFSET, |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/// The [Ethiopian Calendar](https://en.wikipedia.org/wiki/Ethiopian_calendar) |
| 45 |
/// |
| 46 |
/// The Ethiopian calendar is a variant of the [`Coptic`] calendar. It differs |
| 47 |
/// from the Coptic calendar by the names of the months as well as the era. |
| 48 |
/// |
| 49 |
/// This implementation can be constructed in two modes: using the Amete Alem era |
| 50 |
/// scheme, or the Amete Mihret era scheme (the default), see [`EthiopianEraStyle`] |
| 51 |
/// for more info. |
| 52 |
/// |
| 53 |
/// This implementation extends proleptically for dates before the calendar's creation. |
| 54 |
/// |
| 55 |
/// This corresponds to the `"ethiopic"` and `"ethioaa"` [CLDR calendars](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier), |
| 56 |
/// with `"ethiopic"` being for [`EthiopianEraStyle::AmeteMihret`] |
| 57 |
/// |
| 58 |
/// # Era codes |
| 59 |
/// |
| 60 |
/// This calendar always uses the `aa` era, where 1 Amete Alem is 5493 BCE. Dates before this era use negative years. |
| 61 |
/// Dates before that use negative year numbers. |
| 62 |
/// |
| 63 |
/// In the Amete Mihret scheme it uses the additional `am` era, 1 Amete Mihret is 9 CE. |
| 64 |
/// |
| 65 |
/// # Months and days |
| 66 |
/// |
| 67 |
/// The 13 months are called Mäskäräm (`M01`, 30 days), Ṭəqəmt (`M02`, 30 days), |
| 68 |
/// Ḫədar (`M03`, 30 days), Taḫśaś (`M04`, 30 days), Ṭərr (`M05`, 30 days), Yäkatit (`M06`, 30 days), |
| 69 |
/// Mägabit (`M07`, 30 days), Miyazya (`M08`, 30 days), Gənbo (`M09`, 30 days), |
| 70 |
/// Säne (`M10`, 30 days), Ḥamle (`M11`, 30 days), Nähase (`M12`, 30 days), Ṗagʷəmen (`M13`, 5 days). |
| 71 |
/// |
| 72 |
/// In leap years (years divisible by 4), Ṗagʷəmen gains a 6th day. |
| 73 |
/// |
| 74 |
/// Standard years thus have 365 days, and leap years 366. |
| 75 |
/// |
| 76 |
/// # Calendar drift |
| 77 |
/// |
| 78 |
/// The Ethiopian calendar has the same year lengths and leap year rules as the [`Coptic`] and |
| 79 |
/// [`Julian`](crate::cal::Julian) calendars, so it experiences the same drift of 1 day in ~128 |
| 80 |
/// years with respect to the seasons. |
| 81 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 82 |
pub struct Ethiopian(EthiopianEraStyle); |
| 83 |
|
| 84 |
impl Default for Ethiopian { |
| 85 |
fn default() -> Self { |
| 86 |
Self(EthiopianEraStyle::AmeteMihret) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[allow(missing_docs)] // not actually public |
| 91 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 92 |
pub struct EthiopianDateInner(CopticDateInner); |
| 93 |
|
| 94 |
impl DateFieldsResolver for Ethiopian { |
| 95 |
// Coptic year |
| 96 |
type YearInfo = CopticYear; |
| 97 |
|
| 98 |
fn days_in_provided_month(year: Self::YearInfo, month: u8) -> u8 { |
| 99 |
Coptic::days_in_provided_month(year, month) |
| 100 |
} |
| 101 |
|
| 102 |
fn months_in_provided_year(year: Self::YearInfo) -> u8 { |
| 103 |
Coptic::months_in_provided_year(year) |
| 104 |
} |
| 105 |
|
| 106 |
#[inline] |
| 107 |
fn min_months_from(start: Self::YearInfo, years: i32) -> i32 { |
| 108 |
Coptic::min_months_from(start, years) |
| 109 |
} |
| 110 |
|
| 111 |
#[inline] |
| 112 |
fn extended_year_from_era_year_unchecked( |
| 113 |
&self, |
| 114 |
era: &[u8], |
| 115 |
era_year: i32, |
| 116 |
) -> Result<i32, UnknownEraError> { |
| 117 |
match (self.era_style(), era) { |
| 118 |
(EthiopianEraStyle::AmeteMihret, b"am") => Ok(era_year), |
| 119 |
(EthiopianEraStyle::AmeteMihret, b"aa") => { |
| 120 |
Ok(era_year - AMETE_MIHRET_OFFSET + AMETE_ALEM_OFFSET) |
| 121 |
} |
| 122 |
(EthiopianEraStyle::AmeteAlem, b"aa") => Ok(era_year), |
| 123 |
(_, _) => Err(UnknownEraError), |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[inline] |
| 128 |
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo { |
| 129 |
CopticYear::from_ethiopian_year(extended_year, self.0) |
| 130 |
} |
| 131 |
|
| 132 |
#[inline] |
| 133 |
fn extended_from_year_info(&self, year_info: Self::YearInfo) -> i32 { |
| 134 |
year_info.to_ethiopian_year(self.0) |
| 135 |
} |
| 136 |
|
| 137 |
#[inline] |
| 138 |
fn reference_year_from_month_day( |
| 139 |
&self, |
| 140 |
month: types::Month, |
| 141 |
day: u8, |
| 142 |
) -> Result<Self::YearInfo, EcmaReferenceYearError> { |
| 143 |
Coptic::reference_year_from_month_day(month, day) |
| 144 |
} |
| 145 |
|
| 146 |
fn to_rata_die_inner(year: Self::YearInfo, month: u8, day: u8) -> RataDie { |
| 147 |
Coptic::to_rata_die_inner(year, month, day) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl crate::cal::scaffold::UnstableSealed for Ethiopian {} |
| 152 |
impl Calendar for Ethiopian { |
| 153 |
type DateInner = EthiopianDateInner; |
| 154 |
type Year = <Coptic as Calendar>::Year; |
| 155 |
type DateCompatibilityError = <Coptic as Calendar>::DateCompatibilityError; |
| 156 |
|
| 157 |
fn new_date( |
| 158 |
&self, |
| 159 |
year: types::YearInput, |
| 160 |
month: types::Month, |
| 161 |
day: u8, |
| 162 |
) -> Result<Self::DateInner, DateNewError> { |
| 163 |
ArithmeticDate::from_input_year_month_code_day(year, month, day, self) |
| 164 |
.map(ArithmeticDate::cast) |
| 165 |
.map(CopticDateInner) |
| 166 |
.map(EthiopianDateInner) |
| 167 |
} |
| 168 |
|
| 169 |
fn from_fields( |
| 170 |
&self, |
| 171 |
fields: DateFields, |
| 172 |
options: DateFromFieldsOptions, |
| 173 |
) -> Result<Self::DateInner, DateFromFieldsError> { |
| 174 |
ArithmeticDate::from_fields(fields, options, self) |
| 175 |
.map(ArithmeticDate::cast) |
| 176 |
.map(CopticDateInner) |
| 177 |
.map(EthiopianDateInner) |
| 178 |
} |
| 179 |
|
| 180 |
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner { |
| 181 |
EthiopianDateInner(Coptic.from_rata_die(rd)) |
| 182 |
} |
| 183 |
|
| 184 |
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie { |
| 185 |
date.0.0.to_rata_die() |
| 186 |
} |
| 187 |
|
| 188 |
fn has_cheap_iso_conversion(&self) -> bool { |
| 189 |
false |
| 190 |
} |
| 191 |
|
| 192 |
fn months_in_year(&self, date: &Self::DateInner) -> u8 { |
| 193 |
Coptic.months_in_year(&date.0) |
| 194 |
} |
| 195 |
|
| 196 |
fn days_in_year(&self, date: &Self::DateInner) -> u16 { |
| 197 |
Coptic.days_in_year(&date.0) |
| 198 |
} |
| 199 |
|
| 200 |
fn days_in_month(&self, date: &Self::DateInner) -> u8 { |
| 201 |
Coptic.days_in_month(&date.0) |
| 202 |
} |
| 203 |
|
| 204 |
fn add( |
| 205 |
&self, |
| 206 |
date: &Self::DateInner, |
| 207 |
duration: types::DateDuration, |
| 208 |
options: DateAddOptions, |
| 209 |
) -> Result<Self::DateInner, DateAddError> { |
| 210 |
Coptic |
| 211 |
.add(&date.0, duration, options) |
| 212 |
.map(EthiopianDateInner) |
| 213 |
} |
| 214 |
|
| 215 |
fn until( |
| 216 |
&self, |
| 217 |
date1: &Self::DateInner, |
| 218 |
date2: &Self::DateInner, |
| 219 |
options: DateDifferenceOptions, |
| 220 |
) -> types::DateDuration { |
| 221 |
Coptic.until(&date1.0, &date2.0, options) |
| 222 |
} |
| 223 |
|
| 224 |
fn check_date_compatibility(&self, other: &Self) -> Result<(), Self::DateCompatibilityError> { |
| 225 |
let _does_not_affect_semantics = other.era_style(); |
| 226 |
Ok(()) |
| 227 |
} |
| 228 |
|
| 229 |
fn year_info(&self, date: &Self::DateInner) -> Self::Year { |
| 230 |
let coptic_year = date.0.0.year(); |
| 231 |
let extended_year = coptic_year.to_ethiopian_year(self.0); |
| 232 |
|
| 233 |
if self.0 == EthiopianEraStyle::AmeteAlem || extended_year <= 0 { |
| 234 |
types::EraYear { |
| 235 |
era: tinystr!(16, "aa"), |
| 236 |
era_index: Some(0), |
| 237 |
year: coptic_year.to_ethiopian_year(EthiopianEraStyle::AmeteAlem), |
| 238 |
extended_year, |
| 239 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 240 |
} |
| 241 |
} else { |
| 242 |
types::EraYear { |
| 243 |
era: tinystr!(16, "am"), |
| 244 |
era_index: Some(1), |
| 245 |
year: coptic_year.to_ethiopian_year(EthiopianEraStyle::AmeteMihret), |
| 246 |
extended_year, |
| 247 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool { |
| 253 |
Coptic.is_in_leap_year(&date.0) |
| 254 |
} |
| 255 |
|
| 256 |
fn month(&self, date: &Self::DateInner) -> types::MonthInfo { |
| 257 |
Coptic.month(&date.0) |
| 258 |
} |
| 259 |
|
| 260 |
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth { |
| 261 |
Coptic.day_of_month(&date.0) |
| 262 |
} |
| 263 |
|
| 264 |
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear { |
| 265 |
Coptic.day_of_year(&date.0) |
| 266 |
} |
| 267 |
|
| 268 |
fn debug_name(&self) -> &'static str { |
| 269 |
match self.0 { |
| 270 |
EthiopianEraStyle::AmeteMihret => "Ethiopian", |
| 271 |
EthiopianEraStyle::AmeteAlem => "Ethiopian (Amete Alem)", |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm> { |
| 276 |
match self.0 { |
| 277 |
EthiopianEraStyle::AmeteMihret => Some(crate::preferences::CalendarAlgorithm::Ethiopic), |
| 278 |
EthiopianEraStyle::AmeteAlem => Some(crate::preferences::CalendarAlgorithm::Ethioaa), |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl Ethiopian { |
| 284 |
/// Construct a new Ethiopian Calendar for the Amete Mihret era naming scheme |
| 285 |
pub const fn new() -> Self { |
| 286 |
Self(EthiopianEraStyle::AmeteMihret) |
| 287 |
} |
| 288 |
|
| 289 |
/// Construct a new Ethiopian Calendar with an explicit [`EthiopianEraStyle`]. |
| 290 |
pub const fn new_with_era_style(era_style: EthiopianEraStyle) -> Self { |
| 291 |
Self(era_style) |
| 292 |
} |
| 293 |
|
| 294 |
/// Returns the [`EthiopianEraStyle`] used by this calendar. |
| 295 |
pub fn era_style(&self) -> EthiopianEraStyle { |
| 296 |
self.0 |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
impl Date<Ethiopian> { |
| 301 |
/// Construct new Ethiopian [`Date`]. |
| 302 |
/// |
| 303 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 304 |
/// valid range of `-9999..=9999`. |
| 305 |
/// |
| 306 |
/// Years are interpreted according to the provided `era_style`. |
| 307 |
/// |
| 308 |
/// ```rust |
| 309 |
/// use icu::calendar::Date; |
| 310 |
/// use icu::calendar::cal::EthiopianEraStyle; |
| 311 |
/// |
| 312 |
/// let date_ethiopian = |
| 313 |
/// Date::try_new_ethiopian(EthiopianEraStyle::AmeteMihret, 2014, 8, 25) |
| 314 |
/// .expect("Failed to initialize Ethopic Date instance."); |
| 315 |
/// |
| 316 |
/// assert_eq!(date_ethiopian.era_year().year, 2014); |
| 317 |
/// assert_eq!(date_ethiopian.month().ordinal, 8); |
| 318 |
/// assert_eq!(date_ethiopian.day_of_month().0, 25); |
| 319 |
/// ``` |
| 320 |
pub fn try_new_ethiopian( |
| 321 |
era_style: EthiopianEraStyle, |
| 322 |
year: i32, |
| 323 |
month: u8, |
| 324 |
day: u8, |
| 325 |
) -> Result<Date<Ethiopian>, RangeError> { |
| 326 |
ArithmeticDate::from_year_month_day(year, month, day, &Ethiopian(era_style)) |
| 327 |
.map(ArithmeticDate::cast) |
| 328 |
.map(CopticDateInner) |
| 329 |
.map(EthiopianDateInner) |
| 330 |
.map(|inner| Date::from_raw(inner, Ethiopian(era_style))) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
#[cfg(test)] |
| 335 |
mod test { |
| 336 |
use super::*; |
| 337 |
|
| 338 |
#[test] |
| 339 |
fn test_leap_year() { |
| 340 |
// 2023-09-11 ISO is 2025-13-06 Ethiopian |
| 341 |
let rd = Date::try_new_iso(2023, 9, 11).unwrap().to_rata_die(); |
| 342 |
let date_ethiopian = Date::from_rata_die(rd, Ethiopian::new()); |
| 343 |
assert_eq!(date_ethiopian.year().extended_year(), 2015); |
| 344 |
assert_eq!(date_ethiopian.month().ordinal, 13); |
| 345 |
assert_eq!(date_ethiopian.day_of_month().0, 6); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn test_ethiopian_conversion_and_back() { |
| 350 |
let rd = Date::try_new_iso(1970, 1, 2).unwrap().to_rata_die(); |
| 351 |
let date_ethiopian = Date::from_rata_die(rd, Ethiopian::new()); |
| 352 |
|
| 353 |
assert_eq!(date_ethiopian.year().extended_year(), 1962); |
| 354 |
assert_eq!(date_ethiopian.month().ordinal, 4); |
| 355 |
assert_eq!(date_ethiopian.day_of_month().0, 24); |
| 356 |
|
| 357 |
assert_eq!(date_ethiopian.to_rata_die(), rd); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn test_ethiopian_aa_conversion_and_back() { |
| 362 |
let rd = Date::try_new_iso(1970, 1, 2).unwrap().to_rata_die(); |
| 363 |
let date_ethiopian = Date::from_rata_die(rd, Ethiopian(EthiopianEraStyle::AmeteAlem)); |
| 364 |
|
| 365 |
assert_eq!(date_ethiopian.year().extended_year(), 7462); |
| 366 |
assert_eq!(date_ethiopian.month().ordinal, 4); |
| 367 |
assert_eq!(date_ethiopian.day_of_month().0, 24); |
| 368 |
|
| 369 |
assert_eq!(date_ethiopian.to_rata_die(), rd); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn test_roundtrip_negative() { |
| 374 |
// https://github.com/unicode-org/icu4x/issues/2254 |
| 375 |
let rd = Date::try_new_iso(-1000, 3, 3).unwrap().to_rata_die(); |
| 376 |
let date_ethiopian = Date::from_rata_die(rd, Ethiopian::new()); |
| 377 |
assert_eq!(date_ethiopian.to_rata_die(), rd); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn extended_year() { |
| 382 |
assert_eq!( |
| 383 |
Date::try_new_iso(-5500 + 9, 1, 1) |
| 384 |
.unwrap() |
| 385 |
.to_calendar(Ethiopian(EthiopianEraStyle::AmeteAlem)) |
| 386 |
.year() |
| 387 |
.extended_year(), |
| 388 |
1 |
| 389 |
); |
| 390 |
assert_eq!( |
| 391 |
Date::try_new_iso(9, 1, 1) |
| 392 |
.unwrap() |
| 393 |
.to_calendar(Ethiopian(EthiopianEraStyle::AmeteAlem)) |
| 394 |
.year() |
| 395 |
.extended_year(), |
| 396 |
5501 |
| 397 |
); |
| 398 |
|
| 399 |
assert_eq!( |
| 400 |
Date::try_new_iso(-5500 + 9, 1, 1) |
| 401 |
.unwrap() |
| 402 |
.to_calendar(Ethiopian(EthiopianEraStyle::AmeteMihret)) |
| 403 |
.year() |
| 404 |
.extended_year(), |
| 405 |
-5499 |
| 406 |
); |
| 407 |
assert_eq!( |
| 408 |
Date::try_new_iso(9, 1, 1) |
| 409 |
.unwrap() |
| 410 |
.to_calendar(Ethiopian(EthiopianEraStyle::AmeteMihret)) |
| 411 |
.year() |
| 412 |
.extended_year(), |
| 413 |
1 |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
#[test] |
| 418 |
fn test_constructor_roundtrip() { |
| 419 |
let rds = crate::tests::get_interesting_rds(); |
| 420 |
for rd in rds { |
| 421 |
let date = Date::from_rata_die( |
| 422 |
rd, |
| 423 |
Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteMihret), |
| 424 |
); |
| 425 |
let reconstructed = Date::try_new_ethiopian( |
| 426 |
EthiopianEraStyle::AmeteMihret, |
| 427 |
date.year().extended_year(), |
| 428 |
date.month().ordinal, |
| 429 |
date.day_of_month().0, |
| 430 |
) |
| 431 |
.unwrap(); |
| 432 |
assert_eq!( |
| 433 |
reconstructed.to_rata_die(), |
| 434 |
rd, |
| 435 |
"Ethiopian failed for RD {:?}", |
| 436 |
rd |
| 437 |
); |
| 438 |
} |
| 439 |
} |
| 440 |
} |