| 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::Calendar; |
| 6 |
use crate::Date; |
| 7 |
use crate::preferences::CalendarAlgorithm; |
| 8 |
use crate::types::Month; |
| 9 |
|
| 10 |
/// Reference: <https://tc39.es/proposal-intl-era-monthcode/#sec-temporal-calendardatearithmeticyear> |
| 11 |
fn extended_epoch<C: Calendar>(c: &C) -> Option<i32> { |
| 12 |
Some(match c.calendar_algorithm()?.as_str() { |
| 13 |
"buddhist" => -543, |
| 14 |
"chinese" => 0, |
| 15 |
"coptic" => 283, |
| 16 |
"dangi" => 0, |
| 17 |
"ethioaa" => -5493, |
| 18 |
"ethiopic" => 7, |
| 19 |
"gregory" => 0, |
| 20 |
"hebrew" => -3761, |
| 21 |
"hijri-civil" => 621, |
| 22 |
"hijri-tbla" => 621, |
| 23 |
"hijri-umalqura" => 621, |
| 24 |
"indian" => 78, |
| 25 |
"japanese" => 0, |
| 26 |
"persian" => 621, |
| 27 |
"roc" => 1911, |
| 28 |
_ => return None, |
| 29 |
}) |
| 30 |
} |
| 31 |
|
| 32 |
super::test_all_cals!( |
| 33 |
fn extended_year<C: Calendar + Copy>(cal: C) { |
| 34 |
let Some(extended_epoch) = extended_epoch(&cal) else { |
| 35 |
return; |
| 36 |
}; |
| 37 |
|
| 38 |
// Create the first date in the epoch year (extended_year = 0) |
| 39 |
let date_in_epoch_year = Date::try_new(0.into(), Month::new(1), 1, cal).unwrap(); |
| 40 |
let iso_date_in_epoch_year = date_in_epoch_year.to_calendar(crate::cal::Iso); |
| 41 |
assert_eq!( |
| 42 |
iso_date_in_epoch_year.year().extended_year(), |
| 43 |
extended_epoch, |
| 44 |
"Extended year for {date_in_epoch_year:?} should be {extended_epoch}" |
| 45 |
); |
| 46 |
|
| 47 |
// Test that for all calendars except Japanese, the *current* era is |
| 48 |
// the same as that used for the extended year |
| 49 |
// This date can be anything as long as it is vaguely modern. |
| 50 |
// |
| 51 |
// Note that this property is not *required* by the specification, new |
| 52 |
// calendars may have epochs that do not follow this property. This |
| 53 |
// property is strongly suggested by the specification as a rule of thumb |
| 54 |
// to follow where possible. |
| 55 |
let iso_date_in_2025 = Date::try_new_iso(2025, 1, 1).unwrap(); |
| 56 |
let date_in_2025 = iso_date_in_2025.to_calendar(cal); |
| 57 |
|
| 58 |
// The extended year should align with the year in the modern era or related ISO. |
| 59 |
// There is a special case for Japanese since it has a modern era but uses ISO for the extended year. |
| 60 |
if matches!(cal.calendar_algorithm(), Some(CalendarAlgorithm::Japanese)) { |
| 61 |
assert_eq!( |
| 62 |
date_in_2025.year().extended_year(), |
| 63 |
2025, |
| 64 |
"Extended year for {date_in_2025:?} should be 2025" |
| 65 |
); |
| 66 |
} else { |
| 67 |
// Note: This code only works because 2025 is in the modern era for all calendars. |
| 68 |
// These two function calls are not equivalent in general. |
| 69 |
let expected = date_in_2025.year().era_year_or_related_iso(); |
| 70 |
assert_eq!( |
| 71 |
date_in_2025.year().extended_year(), |
| 72 |
expected, |
| 73 |
"Extended year for {date_in_2025:?} should be {expected}" |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
); |