| 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 calendrical_calculations::rata_die::RataDie; |
| 6 |
|
| 7 |
use crate::Iso; |
| 8 |
use crate::cal::iso::IsoDateInner; |
| 9 |
use crate::error::{DateAddError, DateError, DateFromFieldsError, DateNewError}; |
| 10 |
use crate::options::DateFromFieldsOptions; |
| 11 |
use crate::options::{DateAddOptions, DateDifferenceOptions}; |
| 12 |
use crate::types::{self, Month, YearInput}; |
| 13 |
use core::fmt; |
| 14 |
|
| 15 |
#[cfg(doc)] |
| 16 |
use crate::Date; |
| 17 |
|
| 18 |
/// A calendar implementation |
| 19 |
/// |
| 20 |
/// Only implementors of [`Calendar`] should care about these methods, in general users of |
| 21 |
/// these calendars should use the methods on [`Date`](crate::Date) instead. |
| 22 |
/// |
| 23 |
/// Individual [`Calendar`] implementations may have inherent utility methods |
| 24 |
/// allowing for direct construction, etc. |
| 25 |
/// |
| 26 |
/// <div class="stab unstable"> |
| 27 |
/// 🚫 This trait is sealed; it should not be implemented by user code. If an API requests an item that implements this |
| 28 |
/// trait, please consider using a type from the implementors listed below. |
| 29 |
/// |
| 30 |
/// It is still possible to implement this trait in userland (since `UnstableSealed` is public), |
| 31 |
/// do not do so unless you are prepared for things to occasionally break. |
| 32 |
/// </div> |
| 33 |
pub trait Calendar: crate::cal::scaffold::UnstableSealed { |
| 34 |
/// The internal type used to represent dates |
| 35 |
/// |
| 36 |
/// Using the [`Eq`] or [`PartialOrd`] implementations requires |
| 37 |
/// the associated calendars to have passed [`Self::check_date_compatibility`]. |
| 38 |
type DateInner: Eq + Copy + PartialOrd + fmt::Debug; |
| 39 |
/// The type of year info returned by the date |
| 40 |
type Year: fmt::Debug + Into<types::YearInfo>; |
| 41 |
/// The error that is returned by [`Self::check_date_compatibility`]. |
| 42 |
/// |
| 43 |
/// Set this to [`core::convert::Infallible`] if the type is a singleton or |
| 44 |
/// the parameterization does not affect date semantics. |
| 45 |
type DateCompatibilityError: fmt::Debug; |
| 46 |
|
| 47 |
/// Construct a date from era/month codes and fields |
| 48 |
/// |
| 49 |
/// The year is the [extended year](crate::Date::extended_year) if no era is provided |
| 50 |
/// |
| 51 |
/// This is used by the deprecated [`Date::try_new_from_codes()`]. Implementors |
| 52 |
/// should rely on the default impl which adapts this to using [`Self::new_date()`]. |
| 53 |
#[expect(clippy::wrong_self_convention)] |
| 54 |
#[deprecated(since = "2.2.0", note = "use `new_date`")] |
| 55 |
fn from_codes( |
| 56 |
&self, |
| 57 |
era: Option<&str>, |
| 58 |
year: i32, |
| 59 |
month_code: types::MonthCode, |
| 60 |
day: u8, |
| 61 |
) -> Result<Self::DateInner, DateError> { |
| 62 |
let input_year = match era { |
| 63 |
Some(e) => YearInput::EraYear(e, year), |
| 64 |
None => year.into(), |
| 65 |
}; |
| 66 |
let month = match Month::try_from_utf8(month_code.0.as_bytes()) { |
| 67 |
Ok(m) => m, |
| 68 |
Err(_) => return Err(DateError::UnknownMonthCode(month_code)), |
| 69 |
}; |
| 70 |
let result = self.new_date(input_year, month, day); |
| 71 |
|
| 72 |
match result { |
| 73 |
Ok(date) => Ok(date), |
| 74 |
Err(codes_error) => Err(match codes_error { |
| 75 |
DateNewError::InvalidDay { max } => DateError::Range { |
| 76 |
field: "day", |
| 77 |
value: day as i32, |
| 78 |
min: 1, |
| 79 |
max: max as i32, |
| 80 |
}, |
| 81 |
DateNewError::MonthNotInCalendar | DateNewError::MonthNotInYear => { |
| 82 |
DateError::UnknownMonthCode(month_code) |
| 83 |
} |
| 84 |
DateNewError::InvalidEra => DateError::UnknownEra, |
| 85 |
DateNewError::InvalidYear => DateError::Range { |
| 86 |
field: "year", |
| 87 |
value: year, |
| 88 |
min: -9999, |
| 89 |
max: 9999, |
| 90 |
}, |
| 91 |
}), |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/// Construct a date from a [`YearInput`], [`Month`], and a day. |
| 96 |
/// |
| 97 |
/// This is used by [`Date::try_new()`]. |
| 98 |
fn new_date( |
| 99 |
&self, |
| 100 |
year: YearInput, |
| 101 |
month: Month, |
| 102 |
day: u8, |
| 103 |
) -> Result<Self::DateInner, DateNewError>; |
| 104 |
|
| 105 |
/// Construct a date from a bag of date fields. |
| 106 |
#[expect(clippy::wrong_self_convention)] |
| 107 |
fn from_fields( |
| 108 |
&self, |
| 109 |
fields: types::DateFields, |
| 110 |
options: DateFromFieldsOptions, |
| 111 |
) -> Result<Self::DateInner, DateFromFieldsError>; |
| 112 |
|
| 113 |
/// Whether `from_iso`/`to_iso` is more efficient |
| 114 |
/// than `from_rata_die`/`to_rata_die`. |
| 115 |
fn has_cheap_iso_conversion(&self) -> bool; |
| 116 |
|
| 117 |
/// Construct the date from an ISO date. |
| 118 |
/// |
| 119 |
/// Only called if `HAS_CHEAP_ISO_CONVERSION` is set. |
| 120 |
#[expect(clippy::wrong_self_convention)] |
| 121 |
fn from_iso(&self, iso: IsoDateInner) -> Self::DateInner { |
| 122 |
// `from_rata_die` precondition is satified by `to_rata_die` |
| 123 |
self.from_rata_die(Iso.to_rata_die(&iso)) |
| 124 |
} |
| 125 |
/// Obtain an ISO date from this date. |
| 126 |
/// |
| 127 |
/// Only called if `HAS_CHEAP_ISO_CONVERSION` is set. |
| 128 |
fn to_iso(&self, date: &Self::DateInner) -> IsoDateInner { |
| 129 |
// `from_rata_die` precondition is satified by `to_rata_die` |
| 130 |
Iso.from_rata_die(self.to_rata_die(date)) |
| 131 |
} |
| 132 |
|
| 133 |
/// Construct the date from a [`RataDie`] |
| 134 |
/// |
| 135 |
/// Precondition: `rd` needs to be in the `VALID_RD_RANGE` |
| 136 |
#[expect(clippy::wrong_self_convention)] |
| 137 |
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner; |
| 138 |
/// Obtain a [`RataDie`] from this date |
| 139 |
/// |
| 140 |
/// The result is guaranteed to be in `VALID_RD_RANGE` |
| 141 |
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie; |
| 142 |
|
| 143 |
/// Count the number of months in a given year, specified by providing a date |
| 144 |
/// from that year |
| 145 |
fn months_in_year(&self, date: &Self::DateInner) -> u8; |
| 146 |
/// Count the number of days in a given year, specified by providing a date |
| 147 |
/// from that year |
| 148 |
fn days_in_year(&self, date: &Self::DateInner) -> u16; |
| 149 |
/// Count the number of days in a given month, specified by providing a date |
| 150 |
/// from that year/month |
| 151 |
fn days_in_month(&self, date: &Self::DateInner) -> u8; |
| 152 |
/// Calculate if a date is in a leap year |
| 153 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool; |
| 154 |
|
| 155 |
/// Information about the year |
| 156 |
fn year_info(&self, date: &Self::DateInner) -> Self::Year; |
| 157 |
|
| 158 |
/// The [extended year](crate::Date::extended_year). |
| 159 |
fn extended_year(&self, date: &Self::DateInner) -> i32 { |
| 160 |
self.year_info(date).into().extended_year() |
| 161 |
} |
| 162 |
/// The calendar-specific month represented by `date` |
| 163 |
fn month(&self, date: &Self::DateInner) -> types::MonthInfo; |
| 164 |
/// The calendar-specific day-of-month represented by `date` |
| 165 |
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth; |
| 166 |
/// Information of the day of the year |
| 167 |
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear; |
| 168 |
|
| 169 |
/// Add `duration` to `date` |
| 170 |
fn add( |
| 171 |
&self, |
| 172 |
date: &Self::DateInner, |
| 173 |
duration: types::DateDuration, |
| 174 |
options: DateAddOptions, |
| 175 |
) -> Result<Self::DateInner, DateAddError>; |
| 176 |
|
| 177 |
/// Calculate `date2 - date` as a duration. |
| 178 |
/// |
| 179 |
/// This requires the associated calendars to have passed [`Self::check_date_compatibility`]. |
| 180 |
fn until( |
| 181 |
&self, |
| 182 |
date1: &Self::DateInner, |
| 183 |
date2: &Self::DateInner, |
| 184 |
options: DateDifferenceOptions, |
| 185 |
) -> types::DateDuration; |
| 186 |
|
| 187 |
/// Returns whether [`Self::DateInner`] represents the same date in both calendars. |
| 188 |
/// |
| 189 |
/// This is checked by [`Date::try_until_with_options`](crate::Date::try_until_with_options), |
| 190 |
/// `impl PartialEq for Date<C>`, `impl PartialOrd for Date<C>`, and `impl Ord for Date<C>`. |
| 191 |
fn check_date_compatibility(&self, other: &Self) -> Result<(), Self::DateCompatibilityError>; |
| 192 |
|
| 193 |
/// Returns the [`CalendarAlgorithm`](crate::preferences::CalendarAlgorithm) that is required to match |
| 194 |
/// when parsing into this calendar. |
| 195 |
/// |
| 196 |
/// If left empty, any algorithm will parse successfully. |
| 197 |
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm>; |
| 198 |
|
| 199 |
/// Obtain a name for the calendar for debug printing |
| 200 |
fn debug_name(&self) -> &'static str; |
| 201 |
} |