| 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::EthiopianEraStyle; |
| 6 |
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver, PackWithMD}; |
| 7 |
use crate::error::{ |
| 8 |
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, UnknownEraError, |
| 9 |
}; |
| 10 |
use crate::options::DateFromFieldsOptions; |
| 11 |
use crate::options::{DateAddOptions, DateDifferenceOptions}; |
| 12 |
use crate::{Calendar, Date, RangeError, types}; |
| 13 |
use calendrical_calculations::rata_die::RataDie; |
| 14 |
use tinystr::tinystr; |
| 15 |
|
| 16 |
/// The [Coptic Calendar](https://en.wikipedia.org/wiki/Coptic_calendar) |
| 17 |
/// |
| 18 |
/// The Coptic calendar, also called the Alexandrian Calendar, is a solar calendar that |
| 19 |
/// is influenced by both the ancient Egpytian calendar and the [`Julian`](crate::cal::Julian) |
| 20 |
/// calendar. It was introduced in Egypt under Roman rule in the first century BCE, and |
| 21 |
/// replaced for civil use in 1875, however continues to be used liturgically. |
| 22 |
/// |
| 23 |
/// This implementation extends proleptically for dates before the calendar's creation. |
| 24 |
/// |
| 25 |
/// This corresponds to the `"coptic"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 26 |
/// |
| 27 |
/// # Era codes |
| 28 |
/// |
| 29 |
/// This calendar uses a single code: `am`, corresponding to the After Diocletian/Anno Martyrum |
| 30 |
/// era. 1 A.M. is equivalent to 284 C.E. |
| 31 |
/// |
| 32 |
/// # Months and days |
| 33 |
/// |
| 34 |
/// The 13 months are called Thout (`M01`, 30 days), Paopi (`M02`, 30 days), Hathor (`M03`, 30 days), |
| 35 |
/// Koiak (`M04`, 30 days), Tobi (`M05`, 30 days), Meshir (`M06`, 30 days), Paremhat (`M07`, 30 days), |
| 36 |
/// Parmouti (`M08`, 30 days), Pashons (`M09`, 30 days), Paoni (`M10`, 30 days), Epip (`M11`, 30 days), |
| 37 |
/// Mesori (`M12`, 30 days), Pi Kogi Enavot (`M13`, 5 days). |
| 38 |
/// |
| 39 |
/// In leap years (years divisible by 4), Pi Kogi Enavot gains a 6th day. |
| 40 |
/// |
| 41 |
/// Standard years thus have 365 days, and leap years 366. |
| 42 |
/// |
| 43 |
/// # Calendar drift |
| 44 |
/// |
| 45 |
/// The Coptic calendar has the same year lengths and leap year rules as the Julian calendar, |
| 46 |
/// so it experiences the same drift of 1 day in ~128 years with respect to the seasons. |
| 47 |
#[derive(Copy, Clone, Debug, Hash, Default, Eq, PartialEq, PartialOrd, Ord)] |
| 48 |
#[allow(clippy::exhaustive_structs)] // this type is stable |
| 49 |
pub struct Coptic; |
| 50 |
|
| 51 |
/// The inner date type used for representing [`Date`]s of [`Coptic`]. See [`Date`] and [`Coptic`] for more details. |
| 52 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 53 |
pub struct CopticDateInner(pub(crate) ArithmeticDate<Coptic>); |
| 54 |
|
| 55 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 56 |
pub(crate) struct CopticYear { |
| 57 |
coptic_year: i32, |
| 58 |
} |
| 59 |
|
| 60 |
impl core::ops::Sub<CopticYear> for CopticYear { |
| 61 |
type Output = i32; |
| 62 |
fn sub(self, rhs: CopticYear) -> Self::Output { |
| 63 |
self.coptic_year - rhs.coptic_year |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl CopticYear { |
| 68 |
pub(crate) fn from_ethiopian_year(extended_year: i32, era_style: EthiopianEraStyle) -> Self { |
| 69 |
let coptic_year = extended_year + era_style.coptic_year_offset(); |
| 70 |
Self { coptic_year } |
| 71 |
} |
| 72 |
pub(crate) fn from_coptic_anno_martyrum_year(coptic_year: i32) -> Self { |
| 73 |
Self { coptic_year } |
| 74 |
} |
| 75 |
pub(crate) fn to_ethiopian_year(self, era_style: EthiopianEraStyle) -> i32 { |
| 76 |
self.coptic_year - era_style.coptic_year_offset() |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl PackWithMD for CopticYear { |
| 81 |
type Packed = <i32 as PackWithMD>::Packed; |
| 82 |
#[inline] |
| 83 |
fn pack(self, month: u8, day: u8) -> Self::Packed { |
| 84 |
<i32 as PackWithMD>::pack(self.coptic_year, month, day) |
| 85 |
} |
| 86 |
#[inline] |
| 87 |
fn unpack_day(packed: Self::Packed) -> u8 { |
| 88 |
<i32 as PackWithMD>::unpack_day(packed) |
| 89 |
} |
| 90 |
#[inline] |
| 91 |
fn unpack_month(packed: Self::Packed) -> u8 { |
| 92 |
<i32 as PackWithMD>::unpack_month(packed) |
| 93 |
} |
| 94 |
#[inline] |
| 95 |
fn unpack_year(packed: Self::Packed) -> Self { |
| 96 |
let coptic_year = <i32 as PackWithMD>::unpack_year(packed); |
| 97 |
Self { coptic_year } |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl DateFieldsResolver for Coptic { |
| 102 |
type YearInfo = CopticYear; |
| 103 |
|
| 104 |
fn days_in_provided_month(year: CopticYear, month: u8) -> u8 { |
| 105 |
if month == 13 { |
| 106 |
5 + calendrical_calculations::coptic::is_leap_year(year.coptic_year) as u8 |
| 107 |
} else { |
| 108 |
30 |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
fn months_in_provided_year(_: CopticYear) -> u8 { |
| 113 |
13 |
| 114 |
} |
| 115 |
|
| 116 |
#[inline] |
| 117 |
fn min_months_from(_start: Self::YearInfo, years: i32) -> i32 { |
| 118 |
13 * years |
| 119 |
} |
| 120 |
|
| 121 |
#[inline] |
| 122 |
fn extended_year_from_era_year_unchecked( |
| 123 |
&self, |
| 124 |
era: &[u8], |
| 125 |
era_year: i32, |
| 126 |
) -> Result<i32, UnknownEraError> { |
| 127 |
match era { |
| 128 |
b"am" => Ok(era_year), |
| 129 |
_ => Err(UnknownEraError), |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[inline] |
| 134 |
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo { |
| 135 |
CopticYear::from_coptic_anno_martyrum_year(extended_year) |
| 136 |
} |
| 137 |
|
| 138 |
#[inline] |
| 139 |
fn extended_from_year_info(&self, year_info: Self::YearInfo) -> i32 { |
| 140 |
year_info.coptic_year |
| 141 |
} |
| 142 |
|
| 143 |
#[inline] |
| 144 |
fn reference_year_from_month_day( |
| 145 |
&self, |
| 146 |
month: types::Month, |
| 147 |
day: u8, |
| 148 |
) -> Result<Self::YearInfo, EcmaReferenceYearError> { |
| 149 |
Coptic::reference_year_from_month_day(month, day) |
| 150 |
} |
| 151 |
|
| 152 |
fn to_rata_die_inner(year: Self::YearInfo, month: u8, day: u8) -> RataDie { |
| 153 |
calendrical_calculations::coptic::fixed_from_coptic(year.coptic_year, month, day) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
impl Coptic { |
| 158 |
pub(crate) fn reference_year_from_month_day( |
| 159 |
month: types::Month, |
| 160 |
day: u8, |
| 161 |
) -> Result<CopticYear, EcmaReferenceYearError> { |
| 162 |
let (ordinal_month, false) = (month.number(), month.is_leap()) else { |
| 163 |
return Err(EcmaReferenceYearError::MonthNotInCalendar); |
| 164 |
}; |
| 165 |
// December 31, 1972 occurs on 4th month, 22nd day, 1689 AM |
| 166 |
let anno_martyrum_year = if ordinal_month < 4 || (ordinal_month == 4 && day <= 22) { |
| 167 |
1689 |
| 168 |
// Note: this must be >=6, not just == 6, since we have not yet |
| 169 |
// applied a potential Overflow::Constrain. |
| 170 |
} else if ordinal_month == 13 && day >= 6 { |
| 171 |
// 1687 AM is a leap year |
| 172 |
1687 |
| 173 |
} else { |
| 174 |
1688 |
| 175 |
}; |
| 176 |
Ok(CopticYear::from_coptic_anno_martyrum_year( |
| 177 |
anno_martyrum_year, |
| 178 |
)) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl crate::cal::scaffold::UnstableSealed for Coptic {} |
| 183 |
impl Calendar for Coptic { |
| 184 |
type DateInner = CopticDateInner; |
| 185 |
type Year = types::EraYear; |
| 186 |
type DateCompatibilityError = core::convert::Infallible; |
| 187 |
|
| 188 |
fn new_date( |
| 189 |
&self, |
| 190 |
year: types::YearInput, |
| 191 |
month: types::Month, |
| 192 |
day: u8, |
| 193 |
) -> Result<Self::DateInner, DateNewError> { |
| 194 |
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(CopticDateInner) |
| 195 |
} |
| 196 |
|
| 197 |
fn from_fields( |
| 198 |
&self, |
| 199 |
fields: types::DateFields, |
| 200 |
options: DateFromFieldsOptions, |
| 201 |
) -> Result<Self::DateInner, DateFromFieldsError> { |
| 202 |
ArithmeticDate::from_fields(fields, options, self).map(CopticDateInner) |
| 203 |
} |
| 204 |
|
| 205 |
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner { |
| 206 |
// by precondition the year cannot exceed i32, so the error case is unreachable |
| 207 |
let (coptic_year, month, day) = |
| 208 |
calendrical_calculations::coptic::coptic_from_fixed(rd).unwrap_or((1, 1, 1)); |
| 209 |
|
| 210 |
// date is in the valid RD range |
| 211 |
CopticDateInner(ArithmeticDate::new_unchecked( |
| 212 |
CopticYear::from_coptic_anno_martyrum_year(coptic_year), |
| 213 |
month, |
| 214 |
day, |
| 215 |
)) |
| 216 |
} |
| 217 |
|
| 218 |
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie { |
| 219 |
date.0.to_rata_die() |
| 220 |
} |
| 221 |
|
| 222 |
fn has_cheap_iso_conversion(&self) -> bool { |
| 223 |
false |
| 224 |
} |
| 225 |
|
| 226 |
fn months_in_year(&self, date: &Self::DateInner) -> u8 { |
| 227 |
Self::months_in_provided_year(date.0.year()) |
| 228 |
} |
| 229 |
|
| 230 |
fn days_in_year(&self, date: &Self::DateInner) -> u16 { |
| 231 |
if self.is_in_leap_year(date) { 366 } else { 365 } |
| 232 |
} |
| 233 |
|
| 234 |
fn days_in_month(&self, date: &Self::DateInner) -> u8 { |
| 235 |
Self::days_in_provided_month(date.0.year(), date.0.month()) |
| 236 |
} |
| 237 |
|
| 238 |
fn add( |
| 239 |
&self, |
| 240 |
date: &Self::DateInner, |
| 241 |
duration: types::DateDuration, |
| 242 |
options: DateAddOptions, |
| 243 |
) -> Result<Self::DateInner, DateAddError> { |
| 244 |
date.0.added(duration, self, options).map(CopticDateInner) |
| 245 |
} |
| 246 |
|
| 247 |
fn until( |
| 248 |
&self, |
| 249 |
date1: &Self::DateInner, |
| 250 |
date2: &Self::DateInner, |
| 251 |
options: DateDifferenceOptions, |
| 252 |
) -> types::DateDuration { |
| 253 |
date1.0.until(&date2.0, self, options) |
| 254 |
} |
| 255 |
|
| 256 |
fn check_date_compatibility(&self, &Self: &Self) -> Result<(), Self::DateCompatibilityError> { |
| 257 |
Ok(()) |
| 258 |
} |
| 259 |
|
| 260 |
fn year_info(&self, date: &Self::DateInner) -> Self::Year { |
| 261 |
let year = date.0.year(); |
| 262 |
types::EraYear { |
| 263 |
era: tinystr!(16, "am"), |
| 264 |
era_index: Some(0), |
| 265 |
year: year.coptic_year, // era year: only one era |
| 266 |
extended_year: year.coptic_year, |
| 267 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool { |
| 272 |
date.0.year().coptic_year.rem_euclid(4) == 3 |
| 273 |
} |
| 274 |
|
| 275 |
fn month(&self, date: &Self::DateInner) -> types::MonthInfo { |
| 276 |
types::MonthInfo::new(self, date.0) |
| 277 |
} |
| 278 |
|
| 279 |
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth { |
| 280 |
types::DayOfMonth(date.0.day()) |
| 281 |
} |
| 282 |
|
| 283 |
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear { |
| 284 |
types::DayOfYear(30 * (date.0.month() as u16 - 1) + date.0.day() as u16) |
| 285 |
} |
| 286 |
|
| 287 |
fn debug_name(&self) -> &'static str { |
| 288 |
"Coptic" |
| 289 |
} |
| 290 |
|
| 291 |
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm> { |
| 292 |
Some(crate::preferences::CalendarAlgorithm::Coptic) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
impl Date<Coptic> { |
| 297 |
/// Construct new Coptic [`Date`]. |
| 298 |
/// |
| 299 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 300 |
/// valid range of `-9999..=9999`. |
| 301 |
/// |
| 302 |
/// ```rust |
| 303 |
/// use icu::calendar::Date; |
| 304 |
/// |
| 305 |
/// let date_coptic = Date::try_new_coptic(1686, 5, 6) |
| 306 |
/// .expect("Failed to initialize Coptic Date instance."); |
| 307 |
/// |
| 308 |
/// assert_eq!(date_coptic.era_year().year, 1686); |
| 309 |
/// assert_eq!(date_coptic.month().ordinal, 5); |
| 310 |
/// assert_eq!(date_coptic.day_of_month().0, 6); |
| 311 |
/// ``` |
| 312 |
pub fn try_new_coptic(year: i32, month: u8, day: u8) -> Result<Date<Coptic>, RangeError> { |
| 313 |
ArithmeticDate::from_year_month_day(year, month, day, &Coptic) |
| 314 |
.map(CopticDateInner) |
| 315 |
.map(|inner| Date::from_raw(inner, Coptic)) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
#[cfg(test)] |
| 320 |
mod tests { |
| 321 |
use super::*; |
| 322 |
use crate::options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow}; |
| 323 |
use crate::types::{DateFields, Month}; |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn test_coptic_regression() { |
| 327 |
// https://github.com/unicode-org/icu4x/issues/2254 |
| 328 |
let rd = Date::try_new_iso(-100, 3, 3).unwrap().to_rata_die(); |
| 329 |
assert_eq!(Date::from_rata_die(rd, Coptic).to_rata_die(), rd); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn test_from_fields_monthday_constrain() { |
| 334 |
// M13-7 is not a real day, however this should resolve to M13-6 |
| 335 |
// with Overflow::Constrain |
| 336 |
let fields = DateFields { |
| 337 |
month: Some(Month::new(13)), |
| 338 |
day: Some(7), |
| 339 |
..Default::default() |
| 340 |
}; |
| 341 |
let options = DateFromFieldsOptions { |
| 342 |
overflow: Some(Overflow::Constrain), |
| 343 |
missing_fields_strategy: Some(MissingFieldsStrategy::Ecma), |
| 344 |
..Default::default() |
| 345 |
}; |
| 346 |
|
| 347 |
let date = Date::try_from_fields(fields, options, Coptic).unwrap(); |
| 348 |
assert_eq!(date.day_of_month().0, 6, "Day was successfully constrained"); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn test_constructor_roundtrip() { |
| 353 |
let rds = crate::tests::get_interesting_rds(); |
| 354 |
for rd in rds { |
| 355 |
let date = Date::from_rata_die(rd, Coptic); |
| 356 |
let reconstructed = Date::try_new_coptic( |
| 357 |
date.year().extended_year(), |
| 358 |
date.month().ordinal, |
| 359 |
date.day_of_month().0, |
| 360 |
) |
| 361 |
.unwrap(); |
| 362 |
assert_eq!( |
| 363 |
reconstructed.to_rata_die(), |
| 364 |
rd, |
| 365 |
"Coptic failed for RD {:?}", |
| 366 |
rd |
| 367 |
); |
| 368 |
} |
| 369 |
} |
| 370 |
} |