| 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::abstract_gregorian::{ |
| 6 |
AbstractGregorian, GregorianYears, impl_with_abstract_gregorian, |
| 7 |
}; |
| 8 |
use crate::calendar_arithmetic::ArithmeticDate; |
| 9 |
use crate::error::UnknownEraError; |
| 10 |
use crate::preferences::CalendarAlgorithm; |
| 11 |
use crate::{Date, RangeError, types}; |
| 12 |
use tinystr::tinystr; |
| 13 |
|
| 14 |
impl_with_abstract_gregorian!(Gregorian, GregorianDateInner, CeBce, _x, CeBce); |
| 15 |
|
| 16 |
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17 |
pub(crate) struct CeBce; |
| 18 |
|
| 19 |
impl GregorianYears for CeBce { |
| 20 |
fn extended_from_era_year( |
| 21 |
&self, |
| 22 |
era: Option<&[u8]>, |
| 23 |
year: i32, |
| 24 |
) -> Result<i32, UnknownEraError> { |
| 25 |
match era { |
| 26 |
None => Ok(year), |
| 27 |
Some(b"ad" | b"ce") => Ok(year), |
| 28 |
Some(b"bce" | b"bc") => Ok(1 - year), |
| 29 |
Some(_) => Err(UnknownEraError), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
fn era_year_from_extended(&self, extended_year: i32, _month: u8, _day: u8) -> types::EraYear { |
| 34 |
if extended_year > 0 { |
| 35 |
types::EraYear { |
| 36 |
era: tinystr!(16, "ce"), |
| 37 |
era_index: Some(1), |
| 38 |
year: extended_year, |
| 39 |
extended_year, |
| 40 |
ambiguity: match extended_year { |
| 41 |
..=999 => types::YearAmbiguity::EraAndCenturyRequired, |
| 42 |
1000..=1949 => types::YearAmbiguity::CenturyRequired, |
| 43 |
1950..=2049 => types::YearAmbiguity::Unambiguous, |
| 44 |
2050.. => types::YearAmbiguity::CenturyRequired, |
| 45 |
}, |
| 46 |
} |
| 47 |
} else { |
| 48 |
types::EraYear { |
| 49 |
era: tinystr!(16, "bce"), |
| 50 |
era_index: Some(0), |
| 51 |
year: 1 - extended_year, |
| 52 |
extended_year, |
| 53 |
ambiguity: types::YearAmbiguity::EraAndCenturyRequired, |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
fn debug_name(&self) -> &'static str { |
| 59 |
"Gregorian" |
| 60 |
} |
| 61 |
|
| 62 |
fn calendar_algorithm(&self) -> Option<CalendarAlgorithm> { |
| 63 |
Some(CalendarAlgorithm::Gregory) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// The [Gregorian Calendar](https://en.wikipedia.org/wiki/Gregorian_calendar) |
| 68 |
/// |
| 69 |
/// The Gregorian calendar is an improvement over the [`Julian`](super::Julian) calendar. |
| 70 |
/// It was adopted under Pope Gregory XIII in 1582 CE by much of Roman Catholic Europe, |
| 71 |
/// and over the following centuries by all other countries that had been using |
| 72 |
/// the Julian calendar. Eventually even countries that had been using other calendars |
| 73 |
/// adopted this calendar, and today it is used as the international civil calendar. |
| 74 |
/// |
| 75 |
/// This implementation extends proleptically for dates before the calendar's creation. |
| 76 |
/// |
| 77 |
/// This corresponds to the `"gregory"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 78 |
/// |
| 79 |
/// # Era codes |
| 80 |
/// |
| 81 |
/// This calendar uses two era codes: `bce` (alias `bc`), and `ce` (alias `ad`), corresponding to the BCE and CE eras. |
| 82 |
/// |
| 83 |
/// # Months and days |
| 84 |
/// |
| 85 |
/// The 12 months are called January (`M01`, 31 days), February (`M02`, 28 days), |
| 86 |
/// March (`M03`, 31 days), April (`M04`, 30 days), May (`M05`, 31 days), June (`M06`, 30 days), |
| 87 |
/// July (`M07`, 31 days), August (`M08`, 31 days), September (`M09`, 30 days), |
| 88 |
/// October (`M10`, 31 days), November (`M11`, 30 days), December (`M12`, 31 days). |
| 89 |
/// |
| 90 |
/// In leap years (years divisible by 4 but not 100 except when divisble by 400), February gains a 29th day. |
| 91 |
/// |
| 92 |
/// Standard years thus have 365 days, and leap years 366. |
| 93 |
/// |
| 94 |
/// # Calendar drift |
| 95 |
/// |
| 96 |
/// The Gregorian calendar has an average year length of 365.2425, slightly longer than |
| 97 |
/// the mean solar year, so this calendar drifts 1 day in ~7700 years with respect |
| 98 |
/// to the seasons. |
| 99 |
/// |
| 100 |
/// # Historical accuracy |
| 101 |
/// |
| 102 |
/// This type implements the [*proleptic* Gregorian calendar]( |
| 103 |
/// https://en.wikipedia.org/wiki/Gregorian_calendar#Proleptic_Gregorian_calendar), |
| 104 |
/// with dates before 1582 CE using the rules projected backwards. Care needs to be taken |
| 105 |
/// when intepreting historical dates before or during the transition from the Julian to |
| 106 |
/// the Gregorian calendar. [Some regions](https://en.wikipedia.org/wiki/Adoption_of_the_Gregorian_calendar) |
| 107 |
/// continued using the Julian calendar as late as the 20th century. Sources often |
| 108 |
/// mark dates as "New Style" (Gregorian) or "Old Style" (Julian) if there is ambiguity. |
| 109 |
/// |
| 110 |
/// Historically, the Julian/Gregorian calendars were used with a variety of year reckoning |
| 111 |
/// schemes (see [`Julian`](super::Julian) for more detail). The Gregorian calendar has generally |
| 112 |
/// been used with the [Anno Domini](https://en.wikipedia.org/wiki/Anno_Domini)/[Common era]( |
| 113 |
/// https://en.wikipedia.org/wiki/Common_Era) since its inception. However, some countries |
| 114 |
/// that have adopted the Gregorian calendar more recently are still using their traditional |
| 115 |
/// year-reckoning schemes. This crate implements some of these as different types, i.e the Thai |
| 116 |
/// [`Buddhist`](super::Buddhist) calendar, the [`Japanese`](super::Japanese) calendar, and the |
| 117 |
/// Chinese Republican Calendar ([`Roc`](super::Roc)). |
| 118 |
#[derive(Copy, Clone, Debug, Default)] |
| 119 |
#[allow(clippy::exhaustive_structs)] // this type is stable |
| 120 |
pub struct Gregorian; |
| 121 |
|
| 122 |
impl Date<Gregorian> { |
| 123 |
/// Construct a new Gregorian [`Date`]. |
| 124 |
/// |
| 125 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 126 |
/// valid range of `-9999..=9999`. |
| 127 |
/// |
| 128 |
/// ```rust |
| 129 |
/// use icu::calendar::Date; |
| 130 |
/// |
| 131 |
/// // Conversion from ISO to Gregorian |
| 132 |
/// let date_gregorian = Date::try_new_gregorian(1970, 1, 2) |
| 133 |
/// .expect("Failed to initialize Gregorian Date instance."); |
| 134 |
/// |
| 135 |
/// assert_eq!(date_gregorian.era_year().year, 1970); |
| 136 |
/// assert_eq!(date_gregorian.month().ordinal, 1); |
| 137 |
/// assert_eq!(date_gregorian.day_of_month().0, 2); |
| 138 |
/// ``` |
| 139 |
pub fn try_new_gregorian(year: i32, month: u8, day: u8) -> Result<Date<Gregorian>, RangeError> { |
| 140 |
ArithmeticDate::from_year_month_day(year, month, day, &AbstractGregorian(CeBce)) |
| 141 |
.map(ArithmeticDate::cast) |
| 142 |
.map(GregorianDateInner) |
| 143 |
.map(|i| Date::from_raw(i, Gregorian)) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl Gregorian { |
| 148 |
/// Returns the date of Easter in the given year. |
| 149 |
pub fn easter(year: i32) -> Date<Self> { |
| 150 |
Date::from_rata_die(calendrical_calculations::gregorian::easter(year), Self) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
#[cfg(test)] |
| 155 |
mod test { |
| 156 |
use calendrical_calculations::rata_die::RataDie; |
| 157 |
|
| 158 |
use super::*; |
| 159 |
|
| 160 |
#[derive(Debug)] |
| 161 |
struct TestCase { |
| 162 |
rd: RataDie, |
| 163 |
extended_year: i32, |
| 164 |
month: u8, |
| 165 |
day: u8, |
| 166 |
era_year: i32, |
| 167 |
era: &'static str, |
| 168 |
} |
| 169 |
|
| 170 |
fn check_test_case(case: TestCase) { |
| 171 |
let date = Date::from_rata_die(case.rd, Gregorian); |
| 172 |
|
| 173 |
assert_eq!(date.to_rata_die(), case.rd, "{case:?}"); |
| 174 |
|
| 175 |
assert_eq!(date.era_year().year, case.era_year, "{case:?}"); |
| 176 |
assert_eq!( |
| 177 |
date.era_year().extended_year, |
| 178 |
case.extended_year, |
| 179 |
"{case:?}" |
| 180 |
); |
| 181 |
assert_eq!(date.era_year().era, case.era, "{case:?}"); |
| 182 |
assert_eq!(date.month().ordinal, case.month, "{case:?}"); |
| 183 |
assert_eq!(date.day_of_month().0, case.day, "{case:?}"); |
| 184 |
|
| 185 |
assert_eq!( |
| 186 |
Date::try_new_gregorian( |
| 187 |
date.era_year().extended_year, |
| 188 |
date.month().ordinal, |
| 189 |
date.day_of_month().0 |
| 190 |
), |
| 191 |
Ok(date), |
| 192 |
"{case:?}" |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn test_gregorian_ce() { |
| 198 |
// Tests that the Gregorian calendar gives the correct expected |
| 199 |
// day, month, and year for positive years (AD/CE/gregory era) |
| 200 |
|
| 201 |
let cases = [ |
| 202 |
TestCase { |
| 203 |
rd: RataDie::new(1), |
| 204 |
extended_year: 1, |
| 205 |
month: 1, |
| 206 |
day: 1, |
| 207 |
era_year: 1, |
| 208 |
era: "ce", |
| 209 |
}, |
| 210 |
TestCase { |
| 211 |
rd: RataDie::new(181), |
| 212 |
extended_year: 1, |
| 213 |
month: 6, |
| 214 |
day: 30, |
| 215 |
era_year: 1, |
| 216 |
era: "ce", |
| 217 |
}, |
| 218 |
TestCase { |
| 219 |
rd: RataDie::new(1155), |
| 220 |
extended_year: 4, |
| 221 |
month: 2, |
| 222 |
day: 29, |
| 223 |
era_year: 4, |
| 224 |
era: "ce", |
| 225 |
}, |
| 226 |
TestCase { |
| 227 |
rd: RataDie::new(1344), |
| 228 |
extended_year: 4, |
| 229 |
month: 9, |
| 230 |
day: 5, |
| 231 |
era_year: 4, |
| 232 |
era: "ce", |
| 233 |
}, |
| 234 |
TestCase { |
| 235 |
rd: RataDie::new(36219), |
| 236 |
extended_year: 100, |
| 237 |
month: 3, |
| 238 |
day: 1, |
| 239 |
era_year: 100, |
| 240 |
era: "ce", |
| 241 |
}, |
| 242 |
]; |
| 243 |
|
| 244 |
for case in cases { |
| 245 |
check_test_case(case); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn test_gregorian_bce() { |
| 251 |
// Tests that the Gregorian calendar gives the correct expected |
| 252 |
// day, month, and year for negative years (BC/BCE era) |
| 253 |
|
| 254 |
let cases = [ |
| 255 |
TestCase { |
| 256 |
rd: RataDie::new(0), |
| 257 |
extended_year: 0, |
| 258 |
month: 12, |
| 259 |
day: 31, |
| 260 |
era_year: 1, |
| 261 |
era: "bce", |
| 262 |
}, |
| 263 |
TestCase { |
| 264 |
rd: RataDie::new(-365), // This is a leap year |
| 265 |
extended_year: 0, |
| 266 |
month: 1, |
| 267 |
day: 1, |
| 268 |
era_year: 1, |
| 269 |
era: "bce", |
| 270 |
}, |
| 271 |
TestCase { |
| 272 |
rd: RataDie::new(-366), |
| 273 |
extended_year: -1, |
| 274 |
month: 12, |
| 275 |
day: 31, |
| 276 |
era_year: 2, |
| 277 |
era: "bce", |
| 278 |
}, |
| 279 |
TestCase { |
| 280 |
rd: RataDie::new(-1461), |
| 281 |
extended_year: -4, |
| 282 |
month: 12, |
| 283 |
day: 31, |
| 284 |
era_year: 5, |
| 285 |
era: "bce", |
| 286 |
}, |
| 287 |
TestCase { |
| 288 |
rd: RataDie::new(-1826), |
| 289 |
extended_year: -4, |
| 290 |
month: 1, |
| 291 |
day: 1, |
| 292 |
era_year: 5, |
| 293 |
era: "bce", |
| 294 |
}, |
| 295 |
]; |
| 296 |
|
| 297 |
for case in cases { |
| 298 |
check_test_case(case); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
#[test] |
| 303 |
fn check_gregorian_directionality() { |
| 304 |
// Tests that for a large range of RDs, if a RD |
| 305 |
// is less than another, the corresponding YMD should also be less |
| 306 |
// than the other, without exception. |
| 307 |
for i in -100..100 { |
| 308 |
for j in -100..100 { |
| 309 |
let greg_i = Date::from_rata_die(RataDie::new(i), Gregorian); |
| 310 |
let greg_j = Date::from_rata_die(RataDie::new(j), Gregorian); |
| 311 |
|
| 312 |
assert_eq!( |
| 313 |
i.cmp(&j), |
| 314 |
greg_i.cmp(&greg_j), |
| 315 |
"Gregorian directionality inconsistent with directionality for i: {i}, j: {j}" |
| 316 |
); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn test_constructor_roundtrip() { |
| 323 |
let rds = crate::tests::get_interesting_rds(); |
| 324 |
for rd in rds { |
| 325 |
let date = Date::from_rata_die(rd, Gregorian); |
| 326 |
let reconstructed = Date::try_new_gregorian( |
| 327 |
date.year().extended_year(), |
| 328 |
date.month().ordinal, |
| 329 |
date.day_of_month().0, |
| 330 |
) |
| 331 |
.unwrap(); |
| 332 |
assert_eq!( |
| 333 |
reconstructed.to_rata_die(), |
| 334 |
rd, |
| 335 |
"Gregorian failed for RD {:?}", |
| 336 |
rd |
| 337 |
); |
| 338 |
} |
| 339 |
} |
| 340 |
} |