| 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 |
//! Module for working with multiple calendars at once |
| 6 |
|
| 7 |
#![allow(clippy::unit_arg)] |
| 8 |
|
| 9 |
use crate::cal::*; |
| 10 |
use crate::provider::CalendarPreferredV1; |
| 11 |
use crate::{AsCalendar, Calendar, Date, Ref}; |
| 12 |
|
| 13 |
use crate::preferences::{CalendarAlgorithm, CalendarPreferences, HijriCalendarAlgorithm}; |
| 14 |
use icu_provider::prelude::*; |
| 15 |
|
| 16 |
use core::fmt; |
| 17 |
|
| 18 |
macro_rules! make_any_calendar { |
| 19 |
( |
| 20 |
$(#[$any_calendar_meta:meta])* |
| 21 |
$any_calendar_ident:ident, |
| 22 |
$(#[$any_date_meta:meta])* |
| 23 |
$any_date_ident:ident, |
| 24 |
|
| 25 |
$( |
| 26 |
$(#[$variant_meta:meta])* |
| 27 |
$variant:ident($ty:ty), |
| 28 |
)+ |
| 29 |
) => { |
| 30 |
$(#[$any_calendar_meta])* |
| 31 |
#[derive(Debug, Clone)] |
| 32 |
#[allow(deprecated)] |
| 33 |
pub enum $any_calendar_ident { |
| 34 |
$( |
| 35 |
#[doc = concat!("A [`", stringify!($ty), "`] calendar")] |
| 36 |
$(#[$variant_meta])* |
| 37 |
$variant($ty), |
| 38 |
)+ |
| 39 |
} |
| 40 |
|
| 41 |
$(#[$any_date_meta])* |
| 42 |
#[doc = concat!("The inner date type for [`", stringify!($any_calendar_ident), "`]")] |
| 43 |
#[derive(Clone, PartialEq, Eq, Debug, Copy)] |
| 44 |
#[allow(deprecated)] |
| 45 |
pub enum $any_date_ident { |
| 46 |
$( |
| 47 |
#[doc = concat!("A date for a [`", stringify!($variant), "`] calendar")] |
| 48 |
$variant(<$ty as $crate::Calendar>::DateInner), |
| 49 |
)+ |
| 50 |
} |
| 51 |
|
| 52 |
impl PartialOrd for $any_date_ident { |
| 53 |
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
| 54 |
use $any_date_ident::*; |
| 55 |
match (self, other) { |
| 56 |
$( |
| 57 |
($variant(d1), $variant(d2)) => d1.partial_cmp(d2), |
| 58 |
)+ |
| 59 |
_ => None, |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl $crate::cal::scaffold::UnstableSealed for $any_calendar_ident {} |
| 65 |
#[allow(deprecated)] |
| 66 |
impl $crate::Calendar for $any_calendar_ident { |
| 67 |
type DateInner = $any_date_ident; |
| 68 |
type Year = $crate::types::YearInfo; |
| 69 |
type DateCompatibilityError = $crate::error::MismatchedCalendarError; |
| 70 |
|
| 71 |
fn new_date( |
| 72 |
&self, |
| 73 |
year: $crate::types::YearInput, |
| 74 |
month: $crate::types::Month, |
| 75 |
day: u8, |
| 76 |
) -> Result<Self::DateInner, $crate::error::DateNewError> { |
| 77 |
Ok(match self { |
| 78 |
$( |
| 79 |
&Self::$variant(c) => $any_date_ident::$variant(c.new_date(year, month, day)?), |
| 80 |
)+ |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
fn from_fields( |
| 85 |
&self, |
| 86 |
fields: $crate::types::DateFields, |
| 87 |
options: $crate::options::DateFromFieldsOptions, |
| 88 |
) -> Result<Self::DateInner, $crate::error::DateFromFieldsError> { |
| 89 |
Ok(match self { |
| 90 |
$( |
| 91 |
&Self::$variant(c) => $any_date_ident::$variant(c.from_fields(fields, options)?), |
| 92 |
)+ |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
fn has_cheap_iso_conversion(&self) -> bool { |
| 97 |
match self { |
| 98 |
$( |
| 99 |
Self::$variant(c) => c.has_cheap_iso_conversion(), |
| 100 |
)+ |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
fn from_iso(&self, iso: <$crate::Iso as $crate::Calendar>::DateInner) -> Self::DateInner { |
| 105 |
match self { |
| 106 |
$( |
| 107 |
&Self::$variant(c) => $any_date_ident::$variant(c.from_iso(iso)), |
| 108 |
)+ |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
fn to_iso(&self, date: &Self::DateInner) -> <$crate::Iso as $crate::Calendar>::DateInner { |
| 113 |
match (self, date) { |
| 114 |
$( |
| 115 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.to_iso(&d), |
| 116 |
)+ |
| 117 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 118 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
fn from_rata_die(&self, rd: $crate::types::RataDie) -> Self::DateInner { |
| 123 |
match self { |
| 124 |
$( |
| 125 |
&Self::$variant(c) => $any_date_ident::$variant(c.from_rata_die(rd)), |
| 126 |
)+ |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn to_rata_die(&self, date: &Self::DateInner) -> $crate::types::RataDie { |
| 131 |
match (self, date) { |
| 132 |
$( |
| 133 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.to_rata_die(&d), |
| 134 |
)+ |
| 135 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 136 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
fn months_in_year(&self, date: &Self::DateInner) -> u8 { |
| 141 |
match (self, date) { |
| 142 |
$( |
| 143 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.months_in_year(&d), |
| 144 |
)+ |
| 145 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 146 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
fn days_in_year(&self, date: &Self::DateInner) -> u16 { |
| 151 |
match (self, date) { |
| 152 |
$( |
| 153 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.days_in_year(&d), |
| 154 |
)+ |
| 155 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 156 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
fn days_in_month(&self, date: &Self::DateInner) -> u8 { |
| 161 |
match (self, date) { |
| 162 |
$( |
| 163 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.days_in_month(&d), |
| 164 |
)+ |
| 165 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 166 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
fn year_info(&self, date: &Self::DateInner) -> $crate::types::YearInfo { |
| 171 |
match (self, date) { |
| 172 |
$( |
| 173 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.year_info(&d).into(), |
| 174 |
)+ |
| 175 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 176 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/// The calendar-specific check if `date` is in a leap year |
| 181 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool { |
| 182 |
match (self, date) { |
| 183 |
$( |
| 184 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.is_in_leap_year(&d), |
| 185 |
)+ |
| 186 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 187 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/// The calendar-specific month represented by `date` |
| 192 |
fn month(&self, date: &Self::DateInner) -> $crate::types::MonthInfo { |
| 193 |
match (self, date) { |
| 194 |
$( |
| 195 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.month(&d), |
| 196 |
)+ |
| 197 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 198 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/// The calendar-specific day-of-month represented by `date` |
| 203 |
fn day_of_month(&self, date: &Self::DateInner) -> $crate::types::DayOfMonth { |
| 204 |
match (self, date) { |
| 205 |
$( |
| 206 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.day_of_month(&d), |
| 207 |
)+ |
| 208 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 209 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/// Information of the day of the year |
| 214 |
fn day_of_year(&self, date: &Self::DateInner) -> $crate::types::DayOfYear { |
| 215 |
match (self, date) { |
| 216 |
$( |
| 217 |
(&Self::$variant(c), &$any_date_ident::$variant(d)) => c.day_of_year(&d), |
| 218 |
)+ |
| 219 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 220 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
fn add( |
| 225 |
&self, |
| 226 |
date: &Self::DateInner, |
| 227 |
duration: $crate::types::DateDuration, |
| 228 |
options: $crate::options::DateAddOptions, |
| 229 |
) -> Result<Self::DateInner, $crate::error::DateAddError> { |
| 230 |
let mut date = *date; |
| 231 |
match (self, &mut date) { |
| 232 |
$( |
| 233 |
(&Self::$variant(c), $any_date_ident::$variant(d)) => { |
| 234 |
*d = c.add(d, duration, options)?; |
| 235 |
}, |
| 236 |
)+ |
| 237 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 238 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 239 |
} |
| 240 |
Ok(date) |
| 241 |
} |
| 242 |
|
| 243 |
fn until( |
| 244 |
&self, |
| 245 |
date1: &Self::DateInner, |
| 246 |
date2: &Self::DateInner, |
| 247 |
options: $crate::options::DateDifferenceOptions, |
| 248 |
) -> $crate::types::DateDuration { |
| 249 |
match (self, date1, date2) { |
| 250 |
$( |
| 251 |
(Self::$variant(c), $any_date_ident::$variant(d1), $any_date_ident::$variant(d2)) => { |
| 252 |
c.until(d1, d2, options) |
| 253 |
} |
| 254 |
)+ |
| 255 |
// This is only reached from misuse of from_raw, a semi-internal api |
| 256 |
_ => panic!(concat!(stringify!($any_calendar_ident), " with mismatched date type")), |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
fn check_date_compatibility(&self, other: &Self) -> Result<(), Self::DateCompatibilityError> { |
| 261 |
match (self, other) { |
| 262 |
$( |
| 263 |
(Self::$variant(c1), Self::$variant(c2)) if c1.check_date_compatibility(c2).is_ok() => { |
| 264 |
Ok(()) |
| 265 |
} |
| 266 |
)+ |
| 267 |
_ => Err($crate::error::MismatchedCalendarError), |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
fn debug_name(&self) -> &'static str { |
| 272 |
match self { |
| 273 |
$( |
| 274 |
&Self::$variant(_) => concat!(stringify!($any_calendar_ident), " (", stringify!($variant), ")"), |
| 275 |
)+ |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
fn calendar_algorithm(&self) -> Option<CalendarAlgorithm> { |
| 280 |
match self { |
| 281 |
$( |
| 282 |
Self::$variant(c) => c.calendar_algorithm(), |
| 283 |
)+ |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
}; |
| 288 |
} |
| 289 |
|
| 290 |
macro_rules! make_any_calendar_impls { |
| 291 |
($($variant:ident($ty:ty),)+) => { |
| 292 |
$( |
| 293 |
#[allow(deprecated)] |
| 294 |
impl IntoAnyCalendar for $ty { |
| 295 |
#[inline] |
| 296 |
fn to_any(self) -> AnyCalendar { |
| 297 |
AnyCalendar::$variant(self) |
| 298 |
} |
| 299 |
#[inline] |
| 300 |
fn kind(&self) -> AnyCalendarKind { |
| 301 |
AnyCalendar::$variant(*self).kind() |
| 302 |
} |
| 303 |
#[inline] |
| 304 |
fn from_any(any: AnyCalendar) -> Result<Self, AnyCalendar> { |
| 305 |
if let AnyCalendar::$variant(c) = any { |
| 306 |
Ok(c) |
| 307 |
} else { |
| 308 |
Err(any) |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
#[inline] |
| 313 |
fn from_any_ref(any: &AnyCalendar) -> Option<&Self> { |
| 314 |
if let AnyCalendar::$variant(c) = any { |
| 315 |
Some(c) |
| 316 |
} else { |
| 317 |
None |
| 318 |
} |
| 319 |
} |
| 320 |
#[inline] |
| 321 |
fn date_to_any(&self, d: &Self::DateInner) -> AnyDateInner { |
| 322 |
AnyDateInner::$variant(*d) |
| 323 |
} |
| 324 |
} |
| 325 |
)+ |
| 326 |
|
| 327 |
#[allow(deprecated)] |
| 328 |
impl<C: AsCalendar<Calendar = AnyCalendar>> Date<C> { |
| 329 |
/// Convert this `Date<AnyCalendar>` to another [`AnyCalendar`], if conversion is needed |
| 330 |
pub fn convert_any<'a>(&self, calendar: &'a AnyCalendar) -> Date<Ref<'a, AnyCalendar>> { |
| 331 |
match (self.calendar(), calendar) { |
| 332 |
$( |
| 333 |
(AnyCalendar::$variant(c1), AnyCalendar::$variant(c2)) if c1.check_date_compatibility(c2).is_ok() |
| 334 |
=> Date::from_raw(*self.inner(), Ref(calendar)), |
| 335 |
)+ |
| 336 |
_ => self.to_calendar(Ref(calendar)), |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
}; |
| 341 |
} |
| 342 |
|
| 343 |
make_any_calendar!( |
| 344 |
/// This is a calendar that encompasses a selection of calendars from this crate. |
| 345 |
/// |
| 346 |
/// This allows for the construction of [`Date`] objects that have their calendar known at runtime. |
| 347 |
/// |
| 348 |
/// This can be constructed by calling `.into()` on a concrete calendar type if the calendar type is known at |
| 349 |
/// compile time. When the type is known at runtime, the [`AnyCalendar::new()`] and sibling methods may be used. |
| 350 |
/// |
| 351 |
/// [`Date`] can also be converted to [`AnyCalendar`]-compatible ones |
| 352 |
/// via [`Date::to_any()`](crate::Date::to_any()). |
| 353 |
/// |
| 354 |
/// There are many ways of constructing an [`AnyCalendar`]'d date: |
| 355 |
/// ``` |
| 356 |
/// use icu::calendar::{AnyCalendar, AnyCalendarKind, Date, cal::{Japanese, Gregorian}, types::MonthCode}; |
| 357 |
/// use icu::locale::locale; |
| 358 |
/// use tinystr::tinystr; |
| 359 |
/// # use std::rc::Rc; |
| 360 |
/// |
| 361 |
/// let locale = locale!("en-u-ca-japanese"); // English with the Japanese calendar |
| 362 |
/// |
| 363 |
/// let calendar = AnyCalendar::new(AnyCalendarKind::new(locale.into())); |
| 364 |
/// |
| 365 |
/// // This is a Date<AnyCalendar> |
| 366 |
/// let any_japanese_date = Date::try_new_gregorian(2020, 9, 1) |
| 367 |
/// .expect("Failed to construct Gregorian Date.") |
| 368 |
/// .to_calendar(calendar) |
| 369 |
/// .to_any(); |
| 370 |
/// |
| 371 |
/// // Construct a date in the appropriate typed calendar and convert |
| 372 |
/// let japanese_calendar = Japanese::new(); |
| 373 |
/// let japanese_date = Date::try_new_japanese_with_calendar("reiwa", 2, 9, 1, |
| 374 |
/// japanese_calendar).unwrap(); |
| 375 |
/// assert_eq!(japanese_date.to_any(), any_japanese_date); |
| 376 |
/// |
| 377 |
/// // this is also Date<AnyCalendar>, but it uses a different calendar |
| 378 |
/// let any_gregorian_date = any_japanese_date.to_calendar(Gregorian).to_any(); |
| 379 |
/// |
| 380 |
/// // Date<AnyCalendar> does not have a total order |
| 381 |
/// assert!(any_gregorian_date <= any_gregorian_date); |
| 382 |
/// assert!(any_japanese_date <= any_japanese_date); |
| 383 |
/// assert!(!(any_gregorian_date <= any_japanese_date) && !(any_japanese_date <= any_gregorian_date)); |
| 384 |
/// ``` |
| 385 |
#[non_exhaustive] |
| 386 |
AnyCalendar, |
| 387 |
|
| 388 |
#[non_exhaustive] |
| 389 |
AnyDateInner, |
| 390 |
|
| 391 |
Buddhist(Buddhist), |
| 392 |
Chinese(ChineseTraditional), |
| 393 |
Coptic(Coptic), |
| 394 |
Dangi(KoreanTraditional), |
| 395 |
Ethiopian(Ethiopian), |
| 396 |
Gregorian(Gregorian), |
| 397 |
Hebrew(Hebrew), |
| 398 |
#[deprecated(since = "2.2.0", note = "see `HijriUmmAlQura`")] |
| 399 |
HijriSimulated(Hijri<hijri::AstronomicalSimulation>), |
| 400 |
HijriTabular(Hijri<hijri::TabularAlgorithm>), |
| 401 |
HijriUmmAlQura(Hijri<hijri::UmmAlQura>), |
| 402 |
Indian(Indian), |
| 403 |
Iso(Iso), |
| 404 |
Japanese(Japanese), |
| 405 |
#[deprecated(since = "2.2.0", note = "see `Japanese`")] |
| 406 |
JapaneseExtended(Japanese), |
| 407 |
Julian(Julian), |
| 408 |
Persian(Persian), |
| 409 |
Roc(Roc), |
| 410 |
); |
| 411 |
|
| 412 |
make_any_calendar_impls!( |
| 413 |
Buddhist(Buddhist), |
| 414 |
Chinese(ChineseTraditional), |
| 415 |
Coptic(Coptic), |
| 416 |
Dangi(KoreanTraditional), |
| 417 |
Ethiopian(Ethiopian), |
| 418 |
Gregorian(Gregorian), |
| 419 |
Hebrew(Hebrew), |
| 420 |
HijriTabular(Hijri<hijri::TabularAlgorithm>), |
| 421 |
HijriUmmAlQura(Hijri<hijri::UmmAlQura>), |
| 422 |
Indian(Indian), |
| 423 |
Iso(Iso), |
| 424 |
Japanese(Japanese), |
| 425 |
Julian(Julian), |
| 426 |
Persian(Persian), |
| 427 |
Roc(Roc), |
| 428 |
HijriSimulated(Hijri<hijri::AstronomicalSimulation>), |
| 429 |
); |
| 430 |
|
| 431 |
impl AnyCalendar { |
| 432 |
/// Constructs an [`AnyCalendar`] for a given calendar kind from compiled data. |
| 433 |
/// |
| 434 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 435 |
/// |
| 436 |
/// [📚 Help choosing a constructor](icu_provider::constructors) |
| 437 |
#[cfg(feature = "compiled_data")] |
| 438 |
pub const fn new(kind: AnyCalendarKind) -> Self { |
| 439 |
match kind { |
| 440 |
AnyCalendarKind::Buddhist => AnyCalendar::Buddhist(Buddhist), |
| 441 |
AnyCalendarKind::Chinese => AnyCalendar::Chinese(ChineseTraditional::new()), |
| 442 |
AnyCalendarKind::Coptic => AnyCalendar::Coptic(Coptic), |
| 443 |
AnyCalendarKind::Dangi => AnyCalendar::Dangi(KoreanTraditional::new()), |
| 444 |
AnyCalendarKind::Ethiopian => AnyCalendar::Ethiopian(Ethiopian::new_with_era_style( |
| 445 |
EthiopianEraStyle::AmeteMihret, |
| 446 |
)), |
| 447 |
AnyCalendarKind::EthiopianAmeteAlem => { |
| 448 |
AnyCalendar::Ethiopian(Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem)) |
| 449 |
} |
| 450 |
AnyCalendarKind::Gregorian => AnyCalendar::Gregorian(Gregorian), |
| 451 |
AnyCalendarKind::Hebrew => AnyCalendar::Hebrew(Hebrew), |
| 452 |
#[allow(deprecated)] |
| 453 |
AnyCalendarKind::HijriSimulatedMecca => { |
| 454 |
AnyCalendar::HijriSimulated(Hijri::new_simulated_mecca()) |
| 455 |
} |
| 456 |
AnyCalendarKind::HijriTabularTypeIIFriday => { |
| 457 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 458 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 459 |
hijri::TabularAlgorithmEpoch::Friday, |
| 460 |
)) |
| 461 |
} |
| 462 |
AnyCalendarKind::HijriTabularTypeIIThursday => { |
| 463 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 464 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 465 |
hijri::TabularAlgorithmEpoch::Thursday, |
| 466 |
)) |
| 467 |
} |
| 468 |
AnyCalendarKind::HijriUmmAlQura => { |
| 469 |
AnyCalendar::HijriUmmAlQura(Hijri::new_umm_al_qura()) |
| 470 |
} |
| 471 |
AnyCalendarKind::Indian => AnyCalendar::Indian(Indian), |
| 472 |
AnyCalendarKind::Iso => AnyCalendar::Iso(Iso), |
| 473 |
#[allow(deprecated)] |
| 474 |
AnyCalendarKind::Japanese | AnyCalendarKind::JapaneseExtended => { |
| 475 |
AnyCalendar::Japanese(Japanese::new()) |
| 476 |
} |
| 477 |
AnyCalendarKind::Julian => AnyCalendar::Julian(Julian), |
| 478 |
AnyCalendarKind::Persian => AnyCalendar::Persian(Persian), |
| 479 |
AnyCalendarKind::Roc => AnyCalendar::Roc(Roc), |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
#[cfg(feature = "serde")] |
| 484 |
#[doc = icu_provider::gen_buffer_unstable_docs!(BUFFER, Self::new)] |
| 485 |
pub fn try_new_with_buffer_provider<P>( |
| 486 |
provider: &P, |
| 487 |
kind: AnyCalendarKind, |
| 488 |
) -> Result<Self, DataError> |
| 489 |
where |
| 490 |
P: BufferProvider + ?Sized, |
| 491 |
{ |
| 492 |
Ok(match kind { |
| 493 |
AnyCalendarKind::Buddhist => AnyCalendar::Buddhist(Buddhist), |
| 494 |
AnyCalendarKind::Chinese => AnyCalendar::Chinese(ChineseTraditional::new()), |
| 495 |
AnyCalendarKind::Coptic => AnyCalendar::Coptic(Coptic), |
| 496 |
AnyCalendarKind::Dangi => AnyCalendar::Dangi(KoreanTraditional::new()), |
| 497 |
AnyCalendarKind::Ethiopian => AnyCalendar::Ethiopian(Ethiopian::new_with_era_style( |
| 498 |
EthiopianEraStyle::AmeteMihret, |
| 499 |
)), |
| 500 |
AnyCalendarKind::EthiopianAmeteAlem => { |
| 501 |
AnyCalendar::Ethiopian(Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem)) |
| 502 |
} |
| 503 |
AnyCalendarKind::Gregorian => AnyCalendar::Gregorian(Gregorian), |
| 504 |
AnyCalendarKind::Hebrew => AnyCalendar::Hebrew(Hebrew), |
| 505 |
#[allow(deprecated)] |
| 506 |
AnyCalendarKind::HijriSimulatedMecca => { |
| 507 |
AnyCalendar::HijriSimulated(Hijri::new_simulated_mecca()) |
| 508 |
} |
| 509 |
AnyCalendarKind::HijriTabularTypeIIFriday => { |
| 510 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 511 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 512 |
hijri::TabularAlgorithmEpoch::Friday, |
| 513 |
)) |
| 514 |
} |
| 515 |
AnyCalendarKind::HijriTabularTypeIIThursday => { |
| 516 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 517 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 518 |
hijri::TabularAlgorithmEpoch::Thursday, |
| 519 |
)) |
| 520 |
} |
| 521 |
AnyCalendarKind::HijriUmmAlQura => { |
| 522 |
AnyCalendar::HijriUmmAlQura(Hijri::new_umm_al_qura()) |
| 523 |
} |
| 524 |
AnyCalendarKind::Indian => AnyCalendar::Indian(Indian), |
| 525 |
AnyCalendarKind::Iso => AnyCalendar::Iso(Iso), |
| 526 |
#[allow(deprecated)] |
| 527 |
AnyCalendarKind::Japanese | AnyCalendarKind::JapaneseExtended => { |
| 528 |
AnyCalendar::Japanese(Japanese::try_new_with_buffer_provider(provider)?) |
| 529 |
} |
| 530 |
AnyCalendarKind::Julian => AnyCalendar::Julian(Julian), |
| 531 |
AnyCalendarKind::Persian => AnyCalendar::Persian(Persian), |
| 532 |
AnyCalendarKind::Roc => AnyCalendar::Roc(Roc), |
| 533 |
}) |
| 534 |
} |
| 535 |
|
| 536 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::new)] |
| 537 |
pub fn try_new_unstable<P>(provider: &P, kind: AnyCalendarKind) -> Result<Self, DataError> |
| 538 |
where |
| 539 |
P: DataProvider<crate::provider::CalendarJapaneseModernV1> + ?Sized, |
| 540 |
{ |
| 541 |
Ok(match kind { |
| 542 |
AnyCalendarKind::Buddhist => AnyCalendar::Buddhist(Buddhist), |
| 543 |
AnyCalendarKind::Chinese => AnyCalendar::Chinese(ChineseTraditional::new()), |
| 544 |
AnyCalendarKind::Coptic => AnyCalendar::Coptic(Coptic), |
| 545 |
AnyCalendarKind::Dangi => AnyCalendar::Dangi(KoreanTraditional::new()), |
| 546 |
AnyCalendarKind::Ethiopian => AnyCalendar::Ethiopian(Ethiopian::new_with_era_style( |
| 547 |
EthiopianEraStyle::AmeteMihret, |
| 548 |
)), |
| 549 |
AnyCalendarKind::EthiopianAmeteAlem => { |
| 550 |
AnyCalendar::Ethiopian(Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem)) |
| 551 |
} |
| 552 |
AnyCalendarKind::Gregorian => AnyCalendar::Gregorian(Gregorian), |
| 553 |
AnyCalendarKind::Hebrew => AnyCalendar::Hebrew(Hebrew), |
| 554 |
AnyCalendarKind::HijriTabularTypeIIFriday => { |
| 555 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 556 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 557 |
hijri::TabularAlgorithmEpoch::Friday, |
| 558 |
)) |
| 559 |
} |
| 560 |
#[allow(deprecated)] |
| 561 |
AnyCalendarKind::HijriSimulatedMecca => { |
| 562 |
AnyCalendar::HijriSimulated(Hijri::new_simulated_mecca()) |
| 563 |
} |
| 564 |
AnyCalendarKind::HijriTabularTypeIIThursday => { |
| 565 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 566 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 567 |
hijri::TabularAlgorithmEpoch::Thursday, |
| 568 |
)) |
| 569 |
} |
| 570 |
AnyCalendarKind::HijriUmmAlQura => { |
| 571 |
AnyCalendar::HijriUmmAlQura(Hijri::new_umm_al_qura()) |
| 572 |
} |
| 573 |
AnyCalendarKind::Indian => AnyCalendar::Indian(Indian), |
| 574 |
AnyCalendarKind::Iso => AnyCalendar::Iso(Iso), |
| 575 |
#[allow(deprecated)] |
| 576 |
AnyCalendarKind::Japanese | AnyCalendarKind::JapaneseExtended => { |
| 577 |
AnyCalendar::Japanese(Japanese::try_new_unstable(provider)?) |
| 578 |
} |
| 579 |
AnyCalendarKind::Julian => AnyCalendar::Julian(Julian), |
| 580 |
AnyCalendarKind::Persian => AnyCalendar::Persian(Persian), |
| 581 |
AnyCalendarKind::Roc => AnyCalendar::Roc(Roc), |
| 582 |
}) |
| 583 |
} |
| 584 |
|
| 585 |
#[cfg(feature = "datagen")] |
| 586 |
#[doc(hidden)] |
| 587 |
/// Used by datagen to determine era indices in the absence of any data. |
| 588 |
pub fn new_without_data(kind: AnyCalendarKind) -> Self { |
| 589 |
match kind { |
| 590 |
AnyCalendarKind::Buddhist => AnyCalendar::Buddhist(Buddhist), |
| 591 |
AnyCalendarKind::Chinese => AnyCalendar::Chinese(ChineseTraditional::new()), |
| 592 |
AnyCalendarKind::Coptic => AnyCalendar::Coptic(Coptic), |
| 593 |
AnyCalendarKind::Dangi => AnyCalendar::Dangi(KoreanTraditional::new()), |
| 594 |
AnyCalendarKind::Ethiopian => AnyCalendar::Ethiopian(Ethiopian::new_with_era_style( |
| 595 |
EthiopianEraStyle::AmeteMihret, |
| 596 |
)), |
| 597 |
AnyCalendarKind::EthiopianAmeteAlem => { |
| 598 |
AnyCalendar::Ethiopian(Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem)) |
| 599 |
} |
| 600 |
AnyCalendarKind::Gregorian => AnyCalendar::Gregorian(Gregorian), |
| 601 |
AnyCalendarKind::Hebrew => AnyCalendar::Hebrew(Hebrew), |
| 602 |
AnyCalendarKind::HijriTabularTypeIIFriday => { |
| 603 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 604 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 605 |
hijri::TabularAlgorithmEpoch::Friday, |
| 606 |
)) |
| 607 |
} |
| 608 |
#[allow(deprecated)] |
| 609 |
AnyCalendarKind::HijriSimulatedMecca => { |
| 610 |
AnyCalendar::HijriSimulated(Hijri::new_simulated_mecca()) |
| 611 |
} |
| 612 |
AnyCalendarKind::HijriTabularTypeIIThursday => { |
| 613 |
AnyCalendar::HijriTabular(Hijri::new_tabular( |
| 614 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 615 |
hijri::TabularAlgorithmEpoch::Thursday, |
| 616 |
)) |
| 617 |
} |
| 618 |
AnyCalendarKind::HijriUmmAlQura => { |
| 619 |
AnyCalendar::HijriUmmAlQura(Hijri::new_umm_al_qura()) |
| 620 |
} |
| 621 |
AnyCalendarKind::Indian => AnyCalendar::Indian(Indian), |
| 622 |
AnyCalendarKind::Iso => AnyCalendar::Iso(Iso), |
| 623 |
#[allow(deprecated)] |
| 624 |
AnyCalendarKind::Japanese | AnyCalendarKind::JapaneseExtended => { |
| 625 |
AnyCalendar::Japanese(Japanese::default()) |
| 626 |
} |
| 627 |
AnyCalendarKind::Julian => AnyCalendar::Julian(Julian), |
| 628 |
AnyCalendarKind::Persian => AnyCalendar::Persian(Persian), |
| 629 |
AnyCalendarKind::Roc => AnyCalendar::Roc(Roc), |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/// The [`AnyCalendarKind`] corresponding to the calendar this contains |
| 634 |
pub fn kind(&self) -> AnyCalendarKind { |
| 635 |
match *self { |
| 636 |
Self::Buddhist(_) => AnyCalendarKind::Buddhist, |
| 637 |
Self::Chinese(_) => AnyCalendarKind::Chinese, |
| 638 |
Self::Coptic(_) => AnyCalendarKind::Coptic, |
| 639 |
Self::Dangi(_) => AnyCalendarKind::Dangi, |
| 640 |
Self::Ethiopian(ref c) => match c.era_style() { |
| 641 |
EthiopianEraStyle::AmeteAlem => AnyCalendarKind::EthiopianAmeteAlem, |
| 642 |
EthiopianEraStyle::AmeteMihret => AnyCalendarKind::Ethiopian, |
| 643 |
}, |
| 644 |
Self::Gregorian(_) => AnyCalendarKind::Gregorian, |
| 645 |
Self::Hebrew(_) => AnyCalendarKind::Hebrew, |
| 646 |
#[allow(deprecated)] |
| 647 |
Self::HijriSimulated(_) => AnyCalendarKind::HijriSimulatedMecca, |
| 648 |
Self::HijriTabular(ref c) => match c.0 { |
| 649 |
hijri::TabularAlgorithm { |
| 650 |
leap_years: hijri::TabularAlgorithmLeapYears::TypeII, |
| 651 |
epoch: hijri::TabularAlgorithmEpoch::Friday, |
| 652 |
} => AnyCalendarKind::HijriTabularTypeIIFriday, |
| 653 |
hijri::TabularAlgorithm { |
| 654 |
leap_years: hijri::TabularAlgorithmLeapYears::TypeII, |
| 655 |
epoch: hijri::TabularAlgorithmEpoch::Thursday, |
| 656 |
} => AnyCalendarKind::HijriTabularTypeIIThursday, |
| 657 |
}, |
| 658 |
Self::HijriUmmAlQura(_) => AnyCalendarKind::HijriUmmAlQura, |
| 659 |
Self::Indian(_) => AnyCalendarKind::Indian, |
| 660 |
Self::Iso(_) => AnyCalendarKind::Iso, |
| 661 |
#[allow(deprecated)] |
| 662 |
Self::Japanese(_) | Self::JapaneseExtended(_) => AnyCalendarKind::Japanese, |
| 663 |
Self::Julian(_) => AnyCalendarKind::Julian, |
| 664 |
Self::Persian(_) => AnyCalendarKind::Persian, |
| 665 |
Self::Roc(_) => AnyCalendarKind::Roc, |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
/// Convenient type for selecting the kind of [`AnyCalendar`] to construct |
| 671 |
#[non_exhaustive] |
| 672 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 673 |
pub enum AnyCalendarKind { |
| 674 |
/// The kind of a [`Buddhist`] calendar |
| 675 |
/// |
| 676 |
/// This corresponds to the `"buddhist"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 677 |
Buddhist, |
| 678 |
/// The kind of a [`Chinese`] calendar |
| 679 |
/// |
| 680 |
/// This corresponds to the `"chinese"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 681 |
Chinese, |
| 682 |
/// The kind of a [`Coptic`] calendar |
| 683 |
/// |
| 684 |
/// This corresponds to the `"coptic"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 685 |
Coptic, |
| 686 |
/// The kind of a [`Dangi`] calendar |
| 687 |
/// |
| 688 |
/// This corresponds to the `"dangi"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 689 |
Dangi, |
| 690 |
/// The kind of an [`Ethiopian`] calendar, with Amete Mihret era |
| 691 |
/// |
| 692 |
/// This corresponds to the `"ethiopic"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 693 |
Ethiopian, |
| 694 |
/// The kind of an [`Ethiopian`] calendar, with Amete Alem era |
| 695 |
/// |
| 696 |
/// This corresponds to the `"ethioaa"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 697 |
EthiopianAmeteAlem, |
| 698 |
/// The kind of a [`Gregorian`] calendar |
| 699 |
/// |
| 700 |
/// This corresponds to the `"gregory"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 701 |
Gregorian, |
| 702 |
/// The kind of a [`Hebrew`] calendar |
| 703 |
/// |
| 704 |
/// This corresponds to the `"hebrew"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 705 |
Hebrew, |
| 706 |
/// The kind of an [`Indian`] calendar |
| 707 |
/// |
| 708 |
/// This corresponds to the `"indian"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 709 |
Indian, |
| 710 |
/// The kind of a [`HijriTabular`] calendar using [`HijriTabularLeapYears::TypeII`] and [`HijriTabularEpoch::Friday`] |
| 711 |
/// |
| 712 |
/// This corresponds to the `"islamic-civil"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 713 |
HijriTabularTypeIIFriday, |
| 714 |
/// The kind of a [`HijriSimulated`] calendar |
| 715 |
/// |
| 716 |
/// This does not correspond to a CLDR calendar. |
| 717 |
HijriSimulatedMecca, |
| 718 |
/// The kind of a [`HijriTabular`] calendar using [`HijriTabularLeapYears::TypeII`] and [`HijriTabularEpoch::Thursday`] |
| 719 |
/// |
| 720 |
/// This corresponds to the `"islamic-tbla"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 721 |
HijriTabularTypeIIThursday, |
| 722 |
/// The kind of a [`HijriUmmAlQura`] calendar |
| 723 |
/// |
| 724 |
/// This corresponds to the `"islamic-umalqura"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 725 |
HijriUmmAlQura, |
| 726 |
/// The kind of an [`Iso`] calendar |
| 727 |
/// |
| 728 |
/// This corresponds to the `"iso8601"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 729 |
Iso, |
| 730 |
/// The kind of a [`Japanese`] calendar |
| 731 |
/// |
| 732 |
/// This corresponds to the `"japanese"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 733 |
Japanese, |
| 734 |
/// Deprecated, use `Japanese`. |
| 735 |
#[deprecated(since = "2.2.0", note = "use `Japanese`")] |
| 736 |
JapaneseExtended, |
| 737 |
/// The kind of a [`Julian`] calendar |
| 738 |
Julian, |
| 739 |
/// The kind of a [`Persian`] calendar |
| 740 |
/// |
| 741 |
/// This corresponds to the `"persian"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 742 |
Persian, |
| 743 |
/// The kind of a [`Roc`] calendar |
| 744 |
/// |
| 745 |
/// This corresponds to the `"roc"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 746 |
Roc, |
| 747 |
} |
| 748 |
|
| 749 |
impl AnyCalendarKind { |
| 750 |
#[deprecated(since = "2.2.0", note = "use `AnyCalendarKind::try_new`")] |
| 751 |
/// Selects the [`AnyCalendarKind`] appropriate for the given [`CalendarPreferences`]. |
| 752 |
pub fn new(prefs: CalendarPreferences) -> Self { |
| 753 |
#[allow(deprecated)] |
| 754 |
prefs |
| 755 |
.resolved_algorithm() |
| 756 |
.try_into() |
| 757 |
.unwrap_or(Self::Gregorian) |
| 758 |
} |
| 759 |
|
| 760 |
icu_provider::gen_buffer_data_constructors!( |
| 761 |
(prefs: CalendarPreferences) -> error: DataError, |
| 762 |
/// Selects the [`AnyCalendarKind`] appropriate for the given [`CalendarPreferences`]. |
| 763 |
/// |
| 764 |
/// # Example |
| 765 |
/// |
| 766 |
/// ``` |
| 767 |
/// use icu::calendar::{AnyCalendarKind, preferences::{CalendarPreferences, HijriCalendarAlgorithm}}; |
| 768 |
/// use icu::locale::locale; |
| 769 |
/// |
| 770 |
/// assert_eq!(AnyCalendarKind::try_new(locale!("und").into()).unwrap(), AnyCalendarKind::Gregorian); |
| 771 |
/// assert_eq!(AnyCalendarKind::try_new(locale!("und-US-u-ca-hebrew").into()).unwrap(), AnyCalendarKind::Hebrew); |
| 772 |
/// assert_eq!(AnyCalendarKind::try_new(locale!("und-AF").into()).unwrap(), AnyCalendarKind::Persian); |
| 773 |
/// assert_eq!(AnyCalendarKind::try_new(locale!("fa").into()).unwrap(), AnyCalendarKind::Persian); |
| 774 |
/// assert_eq!(AnyCalendarKind::try_new(locale!("und-US-u-rg-thxxxx").into()).unwrap(), AnyCalendarKind::Buddhist); |
| 775 |
/// assert_eq!( |
| 776 |
/// AnyCalendarKind::try_new(locale!("und-US-u-ca-islamic").into()).unwrap(), |
| 777 |
/// AnyCalendarKind::HijriTabularTypeIIFriday |
| 778 |
/// ); |
| 779 |
/// # assert_eq!(AnyCalendarKind::try_new("und-US-u-ca-islamic-rgsa".parse::<icu::locale::Locale>().unwrap().into()).unwrap(), |
| 780 |
/// # AnyCalendarKind::HijriTabularTypeIIFriday |
| 781 |
/// # ); |
| 782 |
/// # assert_eq!(AnyCalendarKind::try_new("und-US-u-ca-islamic-foo".parse::<icu::locale::Locale>().unwrap().into()).unwrap(), |
| 783 |
/// # AnyCalendarKind::Gregorian |
| 784 |
/// # ); |
| 785 |
/// # assert_eq!(AnyCalendarKind::try_new("und-US-u-ca-hebrew-foo".parse::<icu::locale::Locale>().unwrap().into()).unwrap(), |
| 786 |
/// # AnyCalendarKind::Gregorian |
| 787 |
/// # ); |
| 788 |
/// ``` |
| 789 |
); |
| 790 |
|
| 791 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new)] |
| 792 |
pub fn try_new_unstable<P: DataProvider<CalendarPreferredV1>>( |
| 793 |
provider: &P, |
| 794 |
mut prefs: CalendarPreferences, |
| 795 |
) -> Result<Self, DataError> { |
| 796 |
if prefs.calendar_algorithm |
| 797 |
== Some(CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Rgsa))) |
| 798 |
{ |
| 799 |
// Resolve islamic-rgsa to the region's Hijri default. |
| 800 |
prefs.calendar_algorithm = Some(CalendarAlgorithm::Hijri(None)); |
| 801 |
} |
| 802 |
|
| 803 |
let algorithm = match prefs.calendar_algorithm { |
| 804 |
Some(algorithm) if !matches!(algorithm, CalendarAlgorithm::Hijri(None)) => algorithm, |
| 805 |
unresolved => DataProvider::<CalendarPreferredV1>::load( |
| 806 |
&provider, |
| 807 |
DataRequest { |
| 808 |
id: DataIdentifierBorrowed::for_locale(&CalendarPreferredV1::make_locale( |
| 809 |
prefs.locale_preferences, |
| 810 |
)), |
| 811 |
metadata: Default::default(), |
| 812 |
}, |
| 813 |
)? |
| 814 |
.payload |
| 815 |
.get() |
| 816 |
.resolve(unresolved), |
| 817 |
}; |
| 818 |
|
| 819 |
Ok(algorithm.try_into().unwrap_or(Self::Gregorian)) |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
impl TryFrom<CalendarAlgorithm> for AnyCalendarKind { |
| 824 |
type Error = (); |
| 825 |
fn try_from(v: CalendarAlgorithm) -> Result<Self, Self::Error> { |
| 826 |
use CalendarAlgorithm::*; |
| 827 |
match v { |
| 828 |
Buddhist => Ok(AnyCalendarKind::Buddhist), |
| 829 |
Chinese => Ok(AnyCalendarKind::Chinese), |
| 830 |
Coptic => Ok(AnyCalendarKind::Coptic), |
| 831 |
Dangi => Ok(AnyCalendarKind::Dangi), |
| 832 |
Ethioaa => Ok(AnyCalendarKind::EthiopianAmeteAlem), |
| 833 |
Ethiopic => Ok(AnyCalendarKind::Ethiopian), |
| 834 |
Gregory => Ok(AnyCalendarKind::Gregorian), |
| 835 |
Hebrew => Ok(AnyCalendarKind::Hebrew), |
| 836 |
Indian => Ok(AnyCalendarKind::Indian), |
| 837 |
Hijri(None) => Err(()), |
| 838 |
Hijri(Some(HijriCalendarAlgorithm::Umalqura)) => Ok(AnyCalendarKind::HijriUmmAlQura), |
| 839 |
Hijri(Some(HijriCalendarAlgorithm::Tbla)) => { |
| 840 |
Ok(AnyCalendarKind::HijriTabularTypeIIThursday) |
| 841 |
} |
| 842 |
Hijri(Some(HijriCalendarAlgorithm::Civil)) => { |
| 843 |
Ok(AnyCalendarKind::HijriTabularTypeIIFriday) |
| 844 |
} |
| 845 |
Hijri(Some(HijriCalendarAlgorithm::Rgsa)) => Err(()), |
| 846 |
Iso8601 => Ok(AnyCalendarKind::Iso), |
| 847 |
Japanese => Ok(AnyCalendarKind::Japanese), |
| 848 |
Persian => Ok(AnyCalendarKind::Persian), |
| 849 |
Roc => Ok(AnyCalendarKind::Roc), |
| 850 |
_ => { |
| 851 |
debug_assert!(false, "unknown calendar algorithm {v:?}"); |
| 852 |
Err(()) |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
impl fmt::Display for AnyCalendarKind { |
| 859 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 860 |
fmt::Debug::fmt(self, f) |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
/// Trait for calendars that may be converted to [`AnyCalendar`] |
| 865 |
pub trait IntoAnyCalendar: Calendar + Sized { |
| 866 |
/// Convert this calendar into an [`AnyCalendar`], moving it |
| 867 |
/// |
| 868 |
/// You should not need to call this method directly |
| 869 |
fn to_any(self) -> AnyCalendar; |
| 870 |
|
| 871 |
/// The [`AnyCalendarKind`] enum variant associated with this calendar |
| 872 |
fn kind(&self) -> AnyCalendarKind; |
| 873 |
|
| 874 |
/// Move an [`AnyCalendar`] into a `Self`, or returning it as an error |
| 875 |
/// if the types do not match. |
| 876 |
/// |
| 877 |
/// You should not need to call this method directly |
| 878 |
fn from_any(any: AnyCalendar) -> Result<Self, AnyCalendar>; |
| 879 |
|
| 880 |
/// Convert an [`AnyCalendar`] reference into a `Self` reference. |
| 881 |
/// |
| 882 |
/// You should not need to call this method directly |
| 883 |
fn from_any_ref(any: &AnyCalendar) -> Option<&Self>; |
| 884 |
|
| 885 |
/// Convert a date for this calendar into a `AnyDateInner` |
| 886 |
/// |
| 887 |
/// You should not need to call this method directly |
| 888 |
fn date_to_any(&self, d: &Self::DateInner) -> AnyDateInner; |
| 889 |
} |
| 890 |
|
| 891 |
impl IntoAnyCalendar for AnyCalendar { |
| 892 |
#[inline] |
| 893 |
fn to_any(self) -> AnyCalendar { |
| 894 |
self |
| 895 |
} |
| 896 |
#[inline] |
| 897 |
fn kind(&self) -> AnyCalendarKind { |
| 898 |
self.kind() |
| 899 |
} |
| 900 |
#[inline] |
| 901 |
fn from_any(any: AnyCalendar) -> Result<Self, AnyCalendar> { |
| 902 |
Ok(any) |
| 903 |
} |
| 904 |
#[inline] |
| 905 |
fn from_any_ref(any: &AnyCalendar) -> Option<&Self> { |
| 906 |
Some(any) |
| 907 |
} |
| 908 |
#[inline] |
| 909 |
fn date_to_any(&self, d: &Self::DateInner) -> AnyDateInner { |
| 910 |
*d |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
impl<C: IntoAnyCalendar> Date<C> { |
| 915 |
/// Type-erase the date, converting it to a date for [`AnyCalendar`] |
| 916 |
pub fn to_any(self) -> Date<AnyCalendar> { |
| 917 |
Date::from_raw( |
| 918 |
self.calendar().date_to_any(self.inner()), |
| 919 |
self.into_calendar().to_any(), |
| 920 |
) |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
#[cfg(test)] |
| 925 |
mod tests { |
| 926 |
use super::*; |
| 927 |
use crate::Ref; |
| 928 |
use crate::error::DateNewError; |
| 929 |
use crate::types::{Month, YearInput}; |
| 930 |
|
| 931 |
#[track_caller] |
| 932 |
fn single_test_roundtrip( |
| 933 |
calendar: Ref<AnyCalendar>, |
| 934 |
era: Option<(&str, Option<u8>)>, |
| 935 |
year: i32, |
| 936 |
month: Month, |
| 937 |
day: u8, |
| 938 |
) { |
| 939 |
let input = if let Some((era, _)) = era { |
| 940 |
YearInput::EraYear(era, year) |
| 941 |
} else { |
| 942 |
year.into() |
| 943 |
}; |
| 944 |
let date = Date::try_new(input, month, day, calendar).unwrap_or_else(|e| { |
| 945 |
panic!( |
| 946 |
"Failed to construct date for {} with {era:?}, {year}, {month:?}, {day}: {e:?}", |
| 947 |
calendar.debug_name(), |
| 948 |
) |
| 949 |
}); |
| 950 |
|
| 951 |
assert_eq!( |
| 952 |
(month, day), |
| 953 |
(date.month().to_input(), date.day_of_month().0), |
| 954 |
"Failed to roundtrip for calendar {}", |
| 955 |
calendar.debug_name() |
| 956 |
); |
| 957 |
|
| 958 |
if let Some((era_code, era_index)) = era { |
| 959 |
let roundtrip_era_year = date.year().era().expect("year type should be era"); |
| 960 |
|
| 961 |
assert_eq!( |
| 962 |
(era_code, era_index, year), |
| 963 |
( |
| 964 |
roundtrip_era_year.era.as_str(), |
| 965 |
roundtrip_era_year.era_index, |
| 966 |
roundtrip_era_year.year, |
| 967 |
), |
| 968 |
"Failed to roundtrip era for calendar {}", |
| 969 |
calendar.debug_name() |
| 970 |
) |
| 971 |
} else { |
| 972 |
assert_eq!( |
| 973 |
year, |
| 974 |
date.year().extended_year(), |
| 975 |
"Failed to roundtrip year for calendar {}", |
| 976 |
calendar.debug_name() |
| 977 |
); |
| 978 |
} |
| 979 |
|
| 980 |
assert_eq!( |
| 981 |
Date::from_rata_die(date.to_rata_die(), calendar), |
| 982 |
date, |
| 983 |
"Failed to roundtrip via iso with {era:?}, {year}, {month:?}, {day}" |
| 984 |
) |
| 985 |
} |
| 986 |
|
| 987 |
#[track_caller] |
| 988 |
fn single_test_error( |
| 989 |
calendar: Ref<AnyCalendar>, |
| 990 |
era: Option<(&str, Option<u8>)>, |
| 991 |
year: i32, |
| 992 |
month: Month, |
| 993 |
day: u8, |
| 994 |
error: DateNewError, |
| 995 |
) { |
| 996 |
let input = if let Some((era, _)) = era { |
| 997 |
YearInput::EraYear(era, year) |
| 998 |
} else { |
| 999 |
year.into() |
| 1000 |
}; |
| 1001 |
let date = Date::try_new(input, month, day, calendar); |
| 1002 |
assert_eq!( |
| 1003 |
date, |
| 1004 |
Err(error), |
| 1005 |
"Construction with {era:?}, {year}, {month:?}, {day} did not return {error:?}" |
| 1006 |
) |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[test] |
| 1010 |
fn buddhist() { |
| 1011 |
let buddhist = AnyCalendar::new(AnyCalendarKind::Buddhist); |
| 1012 |
let buddhist = Ref(&buddhist); |
| 1013 |
single_test_roundtrip(buddhist, Some(("be", Some(0))), 100, Month::new(3), 1); |
| 1014 |
single_test_roundtrip(buddhist, None, 100, Month::new(3), 1); |
| 1015 |
single_test_roundtrip(buddhist, None, -100, Month::new(3), 1); |
| 1016 |
single_test_roundtrip(buddhist, Some(("be", Some(0))), -100, Month::new(3), 1); |
| 1017 |
single_test_error( |
| 1018 |
buddhist, |
| 1019 |
Some(("be", Some(0))), |
| 1020 |
100, |
| 1021 |
Month::new(13), |
| 1022 |
1, |
| 1023 |
DateNewError::MonthNotInCalendar, |
| 1024 |
); |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[test] |
| 1028 |
fn coptic() { |
| 1029 |
let coptic = AnyCalendar::new(AnyCalendarKind::Coptic); |
| 1030 |
let coptic = Ref(&coptic); |
| 1031 |
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, Month::new(3), 1); |
| 1032 |
single_test_roundtrip(coptic, None, 2000, Month::new(3), 1); |
| 1033 |
single_test_roundtrip(coptic, None, -100, Month::new(3), 1); |
| 1034 |
single_test_roundtrip(coptic, Some(("am", Some(0))), -99, Month::new(3), 1); |
| 1035 |
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, Month::new(13), 1); |
| 1036 |
single_test_error( |
| 1037 |
coptic, |
| 1038 |
Some(("am", Some(0))), |
| 1039 |
100, |
| 1040 |
Month::new(14), |
| 1041 |
1, |
| 1042 |
DateNewError::MonthNotInCalendar, |
| 1043 |
); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[test] |
| 1047 |
fn ethiopian() { |
| 1048 |
let ethiopian = AnyCalendar::new(AnyCalendarKind::Ethiopian); |
| 1049 |
let ethiopian = Ref(ðiopian); |
| 1050 |
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 100, Month::new(3), 1); |
| 1051 |
single_test_roundtrip(ethiopian, None, 2000, Month::new(3), 1); |
| 1052 |
single_test_roundtrip(ethiopian, None, -100, Month::new(3), 1); |
| 1053 |
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 2000, Month::new(13), 1); |
| 1054 |
single_test_roundtrip(ethiopian, Some(("aa", Some(0))), 5400, Month::new(3), 1); |
| 1055 |
// Since #6910, the era range is not enforced in try_new |
| 1056 |
/* |
| 1057 |
single_test_error( |
| 1058 |
ethiopian, |
| 1059 |
Some(("am", Some(0))), |
| 1060 |
0, |
| 1061 |
Month::new(3), |
| 1062 |
1, |
| 1063 |
DateError::Range { |
| 1064 |
field: "year", |
| 1065 |
value: 0, |
| 1066 |
min: 1, |
| 1067 |
max: i32::MAX, |
| 1068 |
}, |
| 1069 |
); |
| 1070 |
single_test_error( |
| 1071 |
ethiopian, |
| 1072 |
Some(("aa", Some(0))), |
| 1073 |
5600, |
| 1074 |
Month::new(3), |
| 1075 |
1, |
| 1076 |
DateError::Range { |
| 1077 |
field: "year", |
| 1078 |
value: 5600, |
| 1079 |
min: i32::MIN, |
| 1080 |
max: 5500, |
| 1081 |
}, |
| 1082 |
); |
| 1083 |
*/ |
| 1084 |
single_test_error( |
| 1085 |
ethiopian, |
| 1086 |
Some(("am", Some(0))), |
| 1087 |
100, |
| 1088 |
Month::new(14), |
| 1089 |
1, |
| 1090 |
DateNewError::MonthNotInCalendar, |
| 1091 |
); |
| 1092 |
} |
| 1093 |
|
| 1094 |
#[test] |
| 1095 |
fn ethiopian_amete_alem() { |
| 1096 |
let ethiopian_amete_alem = AnyCalendar::new(AnyCalendarKind::EthiopianAmeteAlem); |
| 1097 |
let ethiopian_amete_alem = Ref(ðiopian_amete_alem); |
| 1098 |
single_test_roundtrip( |
| 1099 |
ethiopian_amete_alem, |
| 1100 |
Some(("aa", Some(0))), |
| 1101 |
7000, |
| 1102 |
Month::new(13), |
| 1103 |
1, |
| 1104 |
); |
| 1105 |
single_test_roundtrip(ethiopian_amete_alem, None, 7000, Month::new(13), 1); |
| 1106 |
single_test_roundtrip(ethiopian_amete_alem, None, -100, Month::new(13), 1); |
| 1107 |
single_test_roundtrip( |
| 1108 |
ethiopian_amete_alem, |
| 1109 |
Some(("aa", Some(0))), |
| 1110 |
100, |
| 1111 |
Month::new(3), |
| 1112 |
1, |
| 1113 |
); |
| 1114 |
single_test_error( |
| 1115 |
ethiopian_amete_alem, |
| 1116 |
Some(("aa", Some(0))), |
| 1117 |
100, |
| 1118 |
Month::new(14), |
| 1119 |
1, |
| 1120 |
DateNewError::MonthNotInCalendar, |
| 1121 |
); |
| 1122 |
} |
| 1123 |
|
| 1124 |
#[test] |
| 1125 |
fn gregorian() { |
| 1126 |
let gregorian = AnyCalendar::new(AnyCalendarKind::Gregorian); |
| 1127 |
let gregorian = Ref(&gregorian); |
| 1128 |
single_test_roundtrip(gregorian, Some(("ce", Some(1))), 100, Month::new(3), 1); |
| 1129 |
single_test_roundtrip(gregorian, None, 2000, Month::new(3), 1); |
| 1130 |
single_test_roundtrip(gregorian, None, -100, Month::new(3), 1); |
| 1131 |
single_test_roundtrip(gregorian, Some(("bce", Some(0))), 100, Month::new(3), 1); |
| 1132 |
// Since #6910, the era range is not enforced in try_new |
| 1133 |
/* |
| 1134 |
single_test_error( |
| 1135 |
gregorian, |
| 1136 |
Some(("ce", Some(1))), |
| 1137 |
0, |
| 1138 |
Month::new(3), |
| 1139 |
1, |
| 1140 |
DateError::Range { |
| 1141 |
field: "year", |
| 1142 |
value: 0, |
| 1143 |
min: 1, |
| 1144 |
max: i32::MAX, |
| 1145 |
}, |
| 1146 |
); |
| 1147 |
single_test_error( |
| 1148 |
gregorian, |
| 1149 |
Some(("bce", Some(0))), |
| 1150 |
0, |
| 1151 |
Month::new(3), |
| 1152 |
1, |
| 1153 |
DateError::Range { |
| 1154 |
field: "year", |
| 1155 |
value: 0, |
| 1156 |
min: 1, |
| 1157 |
max: i32::MAX, |
| 1158 |
}, |
| 1159 |
); |
| 1160 |
*/ |
| 1161 |
single_test_error( |
| 1162 |
gregorian, |
| 1163 |
Some(("bce", Some(0))), |
| 1164 |
100, |
| 1165 |
Month::new(13), |
| 1166 |
1, |
| 1167 |
DateNewError::MonthNotInCalendar, |
| 1168 |
); |
| 1169 |
} |
| 1170 |
|
| 1171 |
#[test] |
| 1172 |
fn indian() { |
| 1173 |
let indian = AnyCalendar::new(AnyCalendarKind::Indian); |
| 1174 |
let indian = Ref(&indian); |
| 1175 |
single_test_roundtrip(indian, Some(("shaka", Some(0))), 100, Month::new(3), 1); |
| 1176 |
single_test_roundtrip(indian, None, 2000, Month::new(12), 1); |
| 1177 |
single_test_roundtrip(indian, None, -100, Month::new(3), 1); |
| 1178 |
single_test_roundtrip(indian, Some(("shaka", Some(0))), 0, Month::new(3), 1); |
| 1179 |
single_test_error( |
| 1180 |
indian, |
| 1181 |
Some(("shaka", Some(0))), |
| 1182 |
100, |
| 1183 |
Month::new(13), |
| 1184 |
1, |
| 1185 |
DateNewError::MonthNotInCalendar, |
| 1186 |
); |
| 1187 |
} |
| 1188 |
|
| 1189 |
#[test] |
| 1190 |
fn chinese_traditional() { |
| 1191 |
let chinese_traditional = AnyCalendar::new(AnyCalendarKind::Chinese); |
| 1192 |
let chinese_traditional = Ref(&chinese_traditional); |
| 1193 |
single_test_roundtrip(chinese_traditional, None, 400, Month::new(2), 5); |
| 1194 |
single_test_roundtrip(chinese_traditional, None, 4660, Month::new(7), 29); |
| 1195 |
single_test_roundtrip(chinese_traditional, None, -100, Month::new(11), 12); |
| 1196 |
single_test_error( |
| 1197 |
chinese_traditional, |
| 1198 |
None, |
| 1199 |
4658, |
| 1200 |
Month::new(13), |
| 1201 |
1, |
| 1202 |
DateNewError::MonthNotInCalendar, |
| 1203 |
); |
| 1204 |
} |
| 1205 |
|
| 1206 |
#[test] |
| 1207 |
fn korean_traditional() { |
| 1208 |
let korean_traditional = AnyCalendar::new(AnyCalendarKind::Dangi); |
| 1209 |
let korean_traditional = Ref(&korean_traditional); |
| 1210 |
single_test_roundtrip(korean_traditional, None, 400, Month::new(2), 5); |
| 1211 |
single_test_roundtrip(korean_traditional, None, 4660, Month::new(8), 29); |
| 1212 |
single_test_roundtrip(korean_traditional, None, -1300, Month::new(11), 12); |
| 1213 |
single_test_error( |
| 1214 |
korean_traditional, |
| 1215 |
None, |
| 1216 |
9393, |
| 1217 |
Month::leap(0), |
| 1218 |
1, |
| 1219 |
DateNewError::MonthNotInCalendar, |
| 1220 |
); |
| 1221 |
} |
| 1222 |
|
| 1223 |
#[test] |
| 1224 |
fn japanese() { |
| 1225 |
let japanese = AnyCalendar::new(AnyCalendarKind::Japanese); |
| 1226 |
let japanese = Ref(&japanese); |
| 1227 |
single_test_roundtrip(japanese, Some(("reiwa", Some(6))), 3, Month::new(3), 1); |
| 1228 |
single_test_roundtrip(japanese, Some(("heisei", Some(5))), 6, Month::new(12), 1); |
| 1229 |
single_test_roundtrip(japanese, Some(("meiji", Some(2))), 10, Month::new(3), 1); |
| 1230 |
single_test_roundtrip(japanese, Some(("ce", Some(1))), 1000, Month::new(3), 1); |
| 1231 |
single_test_roundtrip(japanese, None, 1000, Month::new(3), 1); |
| 1232 |
single_test_roundtrip(japanese, None, -100, Month::new(3), 1); |
| 1233 |
single_test_roundtrip(japanese, None, 2024, Month::new(3), 1); |
| 1234 |
single_test_roundtrip(japanese, Some(("bce", Some(0))), 10, Month::new(3), 1); |
| 1235 |
// Since #6910, the era range is not enforced in try_new |
| 1236 |
/* |
| 1237 |
single_test_error( |
| 1238 |
japanese, |
| 1239 |
Some(("ce", None)), |
| 1240 |
0, |
| 1241 |
Month::new(3), |
| 1242 |
1, |
| 1243 |
DateError::Range { |
| 1244 |
field: "year", |
| 1245 |
value: 0, |
| 1246 |
min: 1, |
| 1247 |
max: i32::MAX, |
| 1248 |
}, |
| 1249 |
); |
| 1250 |
single_test_error( |
| 1251 |
japanese, |
| 1252 |
Some(("bce", Some(0))), |
| 1253 |
0, |
| 1254 |
Month::new(3), |
| 1255 |
1, |
| 1256 |
DateError::Range { |
| 1257 |
field: "year", |
| 1258 |
value: 0, |
| 1259 |
min: 1, |
| 1260 |
max: i32::MAX, |
| 1261 |
}, |
| 1262 |
); |
| 1263 |
*/ |
| 1264 |
single_test_error( |
| 1265 |
japanese, |
| 1266 |
Some(("reiwa", None)), |
| 1267 |
2, |
| 1268 |
Month::new(13), |
| 1269 |
1, |
| 1270 |
DateNewError::MonthNotInCalendar, |
| 1271 |
); |
| 1272 |
} |
| 1273 |
|
| 1274 |
#[test] |
| 1275 |
fn persian() { |
| 1276 |
let persian = AnyCalendar::new(AnyCalendarKind::Persian); |
| 1277 |
let persian = Ref(&persian); |
| 1278 |
single_test_roundtrip(persian, Some(("ap", Some(0))), 477, Month::new(3), 1); |
| 1279 |
single_test_roundtrip(persian, None, 2083, Month::new(7), 21); |
| 1280 |
single_test_roundtrip(persian, None, -100, Month::new(7), 21); |
| 1281 |
single_test_roundtrip(persian, Some(("ap", Some(0))), 1600, Month::new(12), 20); |
| 1282 |
single_test_error( |
| 1283 |
persian, |
| 1284 |
Some(("ap", Some(0))), |
| 1285 |
100, |
| 1286 |
Month::new(50), |
| 1287 |
1, |
| 1288 |
DateNewError::MonthNotInCalendar, |
| 1289 |
); |
| 1290 |
} |
| 1291 |
|
| 1292 |
#[test] |
| 1293 |
fn hebrew() { |
| 1294 |
let hebrew = AnyCalendar::new(AnyCalendarKind::Hebrew); |
| 1295 |
let hebrew = Ref(&hebrew); |
| 1296 |
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5773, Month::new(3), 1); |
| 1297 |
single_test_roundtrip(hebrew, None, 4993, Month::new(7), 21); |
| 1298 |
single_test_roundtrip(hebrew, None, -100, Month::new(7), 21); |
| 1299 |
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5012, Month::new(12), 20); |
| 1300 |
single_test_error( |
| 1301 |
hebrew, |
| 1302 |
Some(("am", Some(0))), |
| 1303 |
100, |
| 1304 |
Month::new(50), |
| 1305 |
1, |
| 1306 |
DateNewError::MonthNotInCalendar, |
| 1307 |
); |
| 1308 |
} |
| 1309 |
|
| 1310 |
#[test] |
| 1311 |
fn roc() { |
| 1312 |
let roc = AnyCalendar::new(AnyCalendarKind::Roc); |
| 1313 |
let roc = Ref(&roc); |
| 1314 |
single_test_roundtrip(roc, Some(("roc", Some(1))), 10, Month::new(5), 3); |
| 1315 |
single_test_roundtrip(roc, Some(("broc", Some(0))), 15, Month::new(1), 10); |
| 1316 |
single_test_roundtrip(roc, None, 100, Month::new(10), 30); |
| 1317 |
single_test_roundtrip(roc, None, -100, Month::new(10), 30); |
| 1318 |
} |
| 1319 |
|
| 1320 |
#[test] |
| 1321 |
fn hijri_tabular_friday() { |
| 1322 |
let hijri_tabular_friday: AnyCalendar = |
| 1323 |
AnyCalendar::new(AnyCalendarKind::HijriTabularTypeIIFriday); |
| 1324 |
let hijri_tabular_friday = Ref(&hijri_tabular_friday); |
| 1325 |
single_test_roundtrip( |
| 1326 |
hijri_tabular_friday, |
| 1327 |
Some(("ah", Some(0))), |
| 1328 |
477, |
| 1329 |
Month::new(3), |
| 1330 |
1, |
| 1331 |
); |
| 1332 |
single_test_roundtrip(hijri_tabular_friday, None, 2083, Month::new(7), 21); |
| 1333 |
single_test_roundtrip(hijri_tabular_friday, None, -100, Month::new(7), 21); |
| 1334 |
single_test_roundtrip( |
| 1335 |
hijri_tabular_friday, |
| 1336 |
Some(("ah", Some(0))), |
| 1337 |
1600, |
| 1338 |
Month::new(12), |
| 1339 |
20, |
| 1340 |
); |
| 1341 |
single_test_error( |
| 1342 |
hijri_tabular_friday, |
| 1343 |
Some(("ah", Some(0))), |
| 1344 |
100, |
| 1345 |
Month::new(50), |
| 1346 |
1, |
| 1347 |
DateNewError::MonthNotInCalendar, |
| 1348 |
); |
| 1349 |
} |
| 1350 |
|
| 1351 |
#[test] |
| 1352 |
fn hijri_umm_al_qura() { |
| 1353 |
let hijri_umm_al_qura: AnyCalendar = AnyCalendar::new(AnyCalendarKind::HijriUmmAlQura); |
| 1354 |
let hijri_umm_al_qura = Ref(&hijri_umm_al_qura); |
| 1355 |
single_test_roundtrip( |
| 1356 |
hijri_umm_al_qura, |
| 1357 |
Some(("ah", Some(0))), |
| 1358 |
477, |
| 1359 |
Month::new(3), |
| 1360 |
1, |
| 1361 |
); |
| 1362 |
single_test_roundtrip(hijri_umm_al_qura, None, 2083, Month::new(7), 21); |
| 1363 |
single_test_roundtrip(hijri_umm_al_qura, None, -100, Month::new(7), 21); |
| 1364 |
single_test_roundtrip( |
| 1365 |
hijri_umm_al_qura, |
| 1366 |
Some(("ah", Some(0))), |
| 1367 |
1600, |
| 1368 |
Month::new(12), |
| 1369 |
20, |
| 1370 |
); |
| 1371 |
single_test_error( |
| 1372 |
hijri_umm_al_qura, |
| 1373 |
Some(("ah", Some(0))), |
| 1374 |
100, |
| 1375 |
Month::new(50), |
| 1376 |
1, |
| 1377 |
DateNewError::MonthNotInCalendar, |
| 1378 |
); |
| 1379 |
} |
| 1380 |
|
| 1381 |
#[test] |
| 1382 |
fn hijri_tabular_thursday() { |
| 1383 |
let hijri_tabular_thursday: AnyCalendar = |
| 1384 |
AnyCalendar::new(AnyCalendarKind::HijriTabularTypeIIThursday); |
| 1385 |
let hijri_tabular_thursday = Ref(&hijri_tabular_thursday); |
| 1386 |
single_test_roundtrip( |
| 1387 |
hijri_tabular_thursday, |
| 1388 |
Some(("ah", Some(0))), |
| 1389 |
477, |
| 1390 |
Month::new(3), |
| 1391 |
1, |
| 1392 |
); |
| 1393 |
single_test_roundtrip(hijri_tabular_thursday, None, 2083, Month::new(7), 21); |
| 1394 |
single_test_roundtrip(hijri_tabular_thursday, None, -100, Month::new(7), 21); |
| 1395 |
single_test_roundtrip( |
| 1396 |
hijri_tabular_thursday, |
| 1397 |
Some(("ah", Some(0))), |
| 1398 |
1600, |
| 1399 |
Month::new(12), |
| 1400 |
20, |
| 1401 |
); |
| 1402 |
single_test_error( |
| 1403 |
hijri_tabular_thursday, |
| 1404 |
Some(("ah", Some(0))), |
| 1405 |
100, |
| 1406 |
Month::new(50), |
| 1407 |
1, |
| 1408 |
DateNewError::MonthNotInCalendar, |
| 1409 |
); |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn iso() { |
| 1414 |
let iso = AnyCalendar::new(AnyCalendarKind::Iso); |
| 1415 |
let iso = Ref(&iso); |
| 1416 |
single_test_roundtrip(iso, Some(("default", Some(0))), 100, Month::new(3), 1); |
| 1417 |
single_test_roundtrip(iso, None, 2000, Month::new(3), 1); |
| 1418 |
single_test_roundtrip(iso, None, -100, Month::new(3), 1); |
| 1419 |
single_test_roundtrip(iso, Some(("default", Some(0))), -100, Month::new(3), 1); |
| 1420 |
single_test_error( |
| 1421 |
iso, |
| 1422 |
Some(("default", Some(0))), |
| 1423 |
100, |
| 1424 |
Month::new(13), |
| 1425 |
1, |
| 1426 |
DateNewError::MonthNotInCalendar, |
| 1427 |
); |
| 1428 |
} |
| 1429 |
} |