| 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_arithmetic::ArithmeticDate; |
| 6 |
use crate::calendar_arithmetic::DateFieldsResolver; |
| 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::types::DateFields; |
| 13 |
use crate::{Calendar, Date, RangeError, types}; |
| 14 |
use calendrical_calculations::rata_die::RataDie; |
| 15 |
use tinystr::tinystr; |
| 16 |
|
| 17 |
/// The [Julian Calendar](https://en.wikipedia.org/wiki/Julian_calendar). |
| 18 |
/// |
| 19 |
/// The Julian calendar is a solar calendar that was introduced in the Roman Republic under |
| 20 |
/// Julius Caesar in 45 BCE, and used in Europe and much of the western world until it was |
| 21 |
/// eventually replaced by the more accurate [`Gregorian`](super::Gregorian) calendar. |
| 22 |
/// |
| 23 |
/// This implementation extends proleptically for dates before the calendar's creation. |
| 24 |
/// |
| 25 |
/// While no country uses the Julian calendar as its civil calendar today, it is still |
| 26 |
/// used by eastern Christian churches to determine lithurgical dates like Christmas and |
| 27 |
/// Easter. |
| 28 |
/// |
| 29 |
/// # Era codes |
| 30 |
/// |
| 31 |
/// This calendar uses two era codes: `bce` (alias `bc`), and `ce` (alias `ad`), corresponding to the BCE and CE eras. |
| 32 |
/// |
| 33 |
/// # Months and days |
| 34 |
/// |
| 35 |
/// The 12 months are called January (`M01`, 31 days), February (`M02`, 28 days), |
| 36 |
/// March (`M03`, 31 days), April (`M04`, 30 days), May (`M05`, 31 days), June (`M06`, 30 days), |
| 37 |
/// July (`M07`, 31 days), August (`M08`, 31 days), September (`M09`, 30 days), |
| 38 |
/// October (`M10`, 31 days), November (`M11`, 30 days), December (`M12`, 31 days). |
| 39 |
/// |
| 40 |
/// In leap years (years divisible by 4), February gains a 29th day. |
| 41 |
/// |
| 42 |
/// Standard years thus have 365 days, and leap years 366. |
| 43 |
/// |
| 44 |
/// # Calendar drift |
| 45 |
/// |
| 46 |
/// The Julian calendar has an average year length of 365.25, slightly longer than |
| 47 |
/// the mean solar year, so this calendar drifts 1 day in ~128 years with |
| 48 |
/// respect to the seasons. This significant drift was the reason for its replacement |
| 49 |
/// by the Gregorian calendar. The Julian calendar is currently 14 days ahead of the |
| 50 |
/// Gregorian calendar and the solar year. |
| 51 |
/// |
| 52 |
/// # Historical accuracy |
| 53 |
/// |
| 54 |
/// Historically, a variety of year reckoning schemes have been used with the Julian |
| 55 |
/// calendar, such as Roman consular years, regnal years, [indictions]( |
| 56 |
/// https://en.wikipedia.org/wiki/Indiction), [Anno Mundi]( |
| 57 |
/// https://en.wikipedia.org/wiki/Anno_Mundi#Byzantine_era), the [Diocletian era]( |
| 58 |
/// https://en.wikipedia.org/wiki/Era_of_the_Martyrs), [Anno Domini]( |
| 59 |
/// https://en.wikipedia.org/wiki/Anno_Domini), and the (equivalent) [Common era]( |
| 60 |
/// https://en.wikipedia.org/wiki/Common_Era). |
| 61 |
/// The latter, which is used today and by this implementation, has been used by |
| 62 |
/// western European authorities since the early middle ages, however some eastern |
| 63 |
/// European countries/churches have not adopted it until fairly recently, or, in |
| 64 |
/// some cases, are still using a different year reckoning scheme. |
| 65 |
/// |
| 66 |
/// Also during the middle ages, [some countries](https://en.wikipedia.org/wiki/New_Year#Historical_European_new_year_dates) |
| 67 |
/// used different dates for the first day of the year, ranging from late December to |
| 68 |
/// late March. Care has to be taken when interpreting year numbers with dates in this |
| 69 |
/// range. |
| 70 |
/// |
| 71 |
/// The calendar was used [incorrectly](https://en.wikipedia.org/wiki/Julian_calendar#Leap_year_error) |
| 72 |
/// for a while after adoption, so the first year where the months align with this proleptic |
| 73 |
/// implementation is probably 4 CE. |
| 74 |
/// |
| 75 |
/// # Examples |
| 76 |
/// |
| 77 |
/// The `Julian` variant of [`AnyCalendar`](crate::AnyCalendar) can be used to implement a date that |
| 78 |
/// switches between [`Julian`] and [`Gregorian`](crate::cal::Gregorian). |
| 79 |
/// |
| 80 |
/// ```rust |
| 81 |
/// use icu::calendar::AnyCalendar; |
| 82 |
/// use icu::calendar::AnyCalendarKind; |
| 83 |
/// use icu::calendar::Date; |
| 84 |
/// use icu::calendar::error::RangeError; |
| 85 |
/// use icu::calendar::Gregorian; |
| 86 |
/// |
| 87 |
/// fn historical_french_date(year: i32, month: u8, day: u8) -> Result<Date<AnyCalendar>, RangeError> { |
| 88 |
/// if (year, month, day) <= (1582, 10, 4) { |
| 89 |
/// // October 4, 1582 was the last day of the Julian calendar in France |
| 90 |
/// Ok(Date::try_new_julian(year, month, day)?.to_any()) |
| 91 |
/// } else if (year, month, day) >= (1582, 10, 15) { |
| 92 |
/// // October 15, 1582 was the first day of the Gregorian calendar in France |
| 93 |
/// Ok(Date::try_new_gregorian(year, month, day)?.to_any()) |
| 94 |
/// } else { |
| 95 |
/// // October 5-14 did not exist in France in 1582 |
| 96 |
/// Err(RangeError { |
| 97 |
/// field: "day", |
| 98 |
/// value: day.into(), |
| 99 |
/// min: 15, |
| 100 |
/// max: 4 |
| 101 |
/// }) |
| 102 |
/// } |
| 103 |
/// } |
| 104 |
/// |
| 105 |
/// let julian = historical_french_date(1500, 1, 1).unwrap(); |
| 106 |
/// let gregorian = historical_french_date(1600, 1, 1).unwrap(); |
| 107 |
/// |
| 108 |
/// assert_eq!(julian.calendar().kind(), AnyCalendarKind::Julian); |
| 109 |
/// assert_eq!(gregorian.calendar().kind(), AnyCalendarKind::Gregorian); |
| 110 |
/// |
| 111 |
/// // Check the resolved dates in the proleptic Gregorian calendar: |
| 112 |
/// assert_eq!(julian.to_calendar(Gregorian), Date::try_new_gregorian(1500, 1, 10).unwrap()); |
| 113 |
/// assert_eq!(gregorian.to_calendar(Gregorian), Date::try_new_gregorian(1600, 1, 1).unwrap()); |
| 114 |
/// ``` |
| 115 |
#[derive(Copy, Clone, Debug, Hash, Default, Eq, PartialEq, PartialOrd, Ord)] |
| 116 |
#[allow(clippy::exhaustive_structs)] // this type is stable |
| 117 |
pub struct Julian; |
| 118 |
|
| 119 |
/// The inner date type used for representing [`Date`]s of [`Julian`]. See [`Date`] and [`Julian`] for more details. |
| 120 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 121 |
// The inner date type used for representing Date<Julian> |
| 122 |
pub struct JulianDateInner(pub(crate) ArithmeticDate<Julian>); |
| 123 |
|
| 124 |
impl DateFieldsResolver for Julian { |
| 125 |
type YearInfo = i32; |
| 126 |
|
| 127 |
fn days_in_provided_month(year: i32, month: u8) -> u8 { |
| 128 |
if month == 2 { |
| 129 |
28 + calendrical_calculations::julian::is_leap_year(year) as u8 |
| 130 |
} else { |
| 131 |
// https://www.youtube.com/watch?v=J9KijLyP-yg&t=1394s |
| 132 |
30 | month ^ (month >> 3) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[inline] |
| 137 |
fn extended_year_from_era_year_unchecked( |
| 138 |
&self, |
| 139 |
era: &[u8], |
| 140 |
era_year: i32, |
| 141 |
) -> Result<i32, UnknownEraError> { |
| 142 |
match era { |
| 143 |
b"ad" | b"ce" => Ok(era_year), |
| 144 |
b"bc" | b"bce" => Ok(1 - era_year), |
| 145 |
_ => Err(UnknownEraError), |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
#[inline] |
| 150 |
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo { |
| 151 |
extended_year |
| 152 |
} |
| 153 |
|
| 154 |
#[inline] |
| 155 |
fn extended_from_year_info(&self, year_info: Self::YearInfo) -> i32 { |
| 156 |
year_info |
| 157 |
} |
| 158 |
|
| 159 |
#[inline] |
| 160 |
fn reference_year_from_month_day( |
| 161 |
&self, |
| 162 |
month: types::Month, |
| 163 |
day: u8, |
| 164 |
) -> Result<Self::YearInfo, EcmaReferenceYearError> { |
| 165 |
let (ordinal_month, false) = (month.number(), month.is_leap()) else { |
| 166 |
return Err(EcmaReferenceYearError::MonthNotInCalendar); |
| 167 |
}; |
| 168 |
// December 31, 1972 occurs on 12th month, 18th day, 1972 Old Style |
| 169 |
// Note: 1972 is a leap year |
| 170 |
let julian_year = if ordinal_month < 12 || (ordinal_month == 12 && day <= 18) { |
| 171 |
1972 |
| 172 |
} else { |
| 173 |
1971 |
| 174 |
}; |
| 175 |
Ok(julian_year) |
| 176 |
} |
| 177 |
|
| 178 |
fn to_rata_die_inner(year: Self::YearInfo, month: u8, day: u8) -> RataDie { |
| 179 |
calendrical_calculations::julian::fixed_from_julian(year, month, day) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl crate::cal::scaffold::UnstableSealed for Julian {} |
| 184 |
impl Calendar for Julian { |
| 185 |
type DateInner = JulianDateInner; |
| 186 |
type Year = types::EraYear; |
| 187 |
type DateCompatibilityError = core::convert::Infallible; |
| 188 |
|
| 189 |
fn new_date( |
| 190 |
&self, |
| 191 |
year: types::YearInput, |
| 192 |
month: types::Month, |
| 193 |
day: u8, |
| 194 |
) -> Result<Self::DateInner, DateNewError> { |
| 195 |
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(JulianDateInner) |
| 196 |
} |
| 197 |
|
| 198 |
fn from_fields( |
| 199 |
&self, |
| 200 |
fields: DateFields, |
| 201 |
options: DateFromFieldsOptions, |
| 202 |
) -> Result<Self::DateInner, DateFromFieldsError> { |
| 203 |
ArithmeticDate::from_fields(fields, options, self).map(JulianDateInner) |
| 204 |
} |
| 205 |
|
| 206 |
fn from_rata_die(&self, rd: RataDie) -> Self::DateInner { |
| 207 |
// by precondition the year cannot exceed i32, so the error case is unreachable |
| 208 |
let (year, month, day) = |
| 209 |
calendrical_calculations::julian::julian_from_fixed(rd).unwrap_or((1, 1, 1)); |
| 210 |
|
| 211 |
// date is in the valid RD range |
| 212 |
JulianDateInner(ArithmeticDate::new_unchecked(year, month, day)) |
| 213 |
} |
| 214 |
|
| 215 |
fn to_rata_die(&self, date: &Self::DateInner) -> RataDie { |
| 216 |
date.0.to_rata_die() |
| 217 |
} |
| 218 |
|
| 219 |
fn has_cheap_iso_conversion(&self) -> bool { |
| 220 |
false |
| 221 |
} |
| 222 |
|
| 223 |
fn months_in_year(&self, date: &Self::DateInner) -> u8 { |
| 224 |
Self::months_in_provided_year(date.0.year()) |
| 225 |
} |
| 226 |
|
| 227 |
fn days_in_year(&self, date: &Self::DateInner) -> u16 { |
| 228 |
365 + calendrical_calculations::julian::is_leap_year(date.0.year()) as u16 |
| 229 |
} |
| 230 |
|
| 231 |
fn days_in_month(&self, date: &Self::DateInner) -> u8 { |
| 232 |
Self::days_in_provided_month(date.0.year(), date.0.month()) |
| 233 |
} |
| 234 |
|
| 235 |
fn add( |
| 236 |
&self, |
| 237 |
date: &Self::DateInner, |
| 238 |
duration: types::DateDuration, |
| 239 |
options: DateAddOptions, |
| 240 |
) -> Result<Self::DateInner, DateAddError> { |
| 241 |
date.0.added(duration, self, options).map(JulianDateInner) |
| 242 |
} |
| 243 |
|
| 244 |
fn until( |
| 245 |
&self, |
| 246 |
date1: &Self::DateInner, |
| 247 |
date2: &Self::DateInner, |
| 248 |
options: DateDifferenceOptions, |
| 249 |
) -> types::DateDuration { |
| 250 |
date1.0.until(&date2.0, self, options) |
| 251 |
} |
| 252 |
|
| 253 |
fn check_date_compatibility(&self, &Self: &Self) -> Result<(), Self::DateCompatibilityError> { |
| 254 |
Ok(()) |
| 255 |
} |
| 256 |
|
| 257 |
/// The calendar-specific year represented by `date` |
| 258 |
/// Julian has the same era scheme as Gregorian |
| 259 |
fn year_info(&self, date: &Self::DateInner) -> Self::Year { |
| 260 |
let extended_year = date.0.year(); |
| 261 |
if extended_year > 0 { |
| 262 |
types::EraYear { |
| 263 |
era: tinystr!(16, "ce"), |
| 264 |
era_index: Some(1), |
| 265 |
year: extended_year, |
| 266 |
extended_year, |
| 267 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 268 |
} |
| 269 |
} else { |
| 270 |
types::EraYear { |
| 271 |
era: tinystr!(16, "bce"), |
| 272 |
era_index: Some(0), |
| 273 |
year: 1 - extended_year, |
| 274 |
extended_year, |
| 275 |
ambiguity: types::YearAmbiguity::EraAndCenturyRequired, |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
fn is_in_leap_year(&self, date: &Self::DateInner) -> bool { |
| 281 |
calendrical_calculations::julian::is_leap_year(date.0.year()) |
| 282 |
} |
| 283 |
|
| 284 |
fn month(&self, date: &Self::DateInner) -> types::MonthInfo { |
| 285 |
types::MonthInfo::new(self, date.0) |
| 286 |
} |
| 287 |
|
| 288 |
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth { |
| 289 |
types::DayOfMonth(date.0.day()) |
| 290 |
} |
| 291 |
|
| 292 |
fn day_of_year(&self, date: &Self::DateInner) -> types::DayOfYear { |
| 293 |
types::DayOfYear( |
| 294 |
calendrical_calculations::julian::days_before_month(date.0.year(), date.0.month()) |
| 295 |
+ date.0.day() as u16, |
| 296 |
) |
| 297 |
} |
| 298 |
|
| 299 |
fn debug_name(&self) -> &'static str { |
| 300 |
"Julian" |
| 301 |
} |
| 302 |
|
| 303 |
fn calendar_algorithm(&self) -> Option<crate::preferences::CalendarAlgorithm> { |
| 304 |
None |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
impl Julian { |
| 309 |
/// Construct a new Julian Calendar |
| 310 |
pub fn new() -> Self { |
| 311 |
Self |
| 312 |
} |
| 313 |
|
| 314 |
/// Returns the date of (Orthodox) Easter in the given year. |
| 315 |
pub fn easter(year: i32) -> Date<Self> { |
| 316 |
Date::from_rata_die(calendrical_calculations::julian::easter(year), Self) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
impl Date<Julian> { |
| 321 |
/// Construct new Julian [`Date`]. |
| 322 |
/// |
| 323 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 324 |
/// valid range of `-9999..=9999`. |
| 325 |
/// |
| 326 |
/// ```rust |
| 327 |
/// use icu::calendar::Date; |
| 328 |
/// |
| 329 |
/// let date_julian = Date::try_new_julian(1969, 12, 20) |
| 330 |
/// .expect("Failed to initialize Julian Date instance."); |
| 331 |
/// |
| 332 |
/// assert_eq!(date_julian.era_year().year, 1969); |
| 333 |
/// assert_eq!(date_julian.month().ordinal, 12); |
| 334 |
/// assert_eq!(date_julian.day_of_month().0, 20); |
| 335 |
/// ``` |
| 336 |
pub fn try_new_julian(year: i32, month: u8, day: u8) -> Result<Date<Julian>, RangeError> { |
| 337 |
ArithmeticDate::from_year_month_day(year, month, day, &Julian) |
| 338 |
.map(JulianDateInner) |
| 339 |
.map(|inner| Date::from_raw(inner, Julian)) |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
#[cfg(test)] |
| 344 |
mod test { |
| 345 |
use super::*; |
| 346 |
|
| 347 |
#[test] |
| 348 |
fn test_julian() { |
| 349 |
let cases = [ |
| 350 |
// March 1st 200 is same on both calendars |
| 351 |
TestCase { |
| 352 |
rd: Date::try_new_iso(200, 3, 1).unwrap().to_rata_die(), |
| 353 |
era: "ce", |
| 354 |
year: 200, |
| 355 |
month: 3, |
| 356 |
day: 1, |
| 357 |
}, |
| 358 |
// Feb 28th, 200 (iso) = Feb 29th, 200 (julian) |
| 359 |
TestCase { |
| 360 |
rd: Date::try_new_iso(200, 2, 28).unwrap().to_rata_die(), |
| 361 |
era: "ce", |
| 362 |
year: 200, |
| 363 |
month: 2, |
| 364 |
day: 29, |
| 365 |
}, |
| 366 |
// March 1st 400 (iso) = Feb 29th, 400 (julian) |
| 367 |
TestCase { |
| 368 |
rd: Date::try_new_iso(400, 3, 1).unwrap().to_rata_die(), |
| 369 |
era: "ce", |
| 370 |
year: 400, |
| 371 |
month: 2, |
| 372 |
day: 29, |
| 373 |
}, |
| 374 |
// Jan 1st, 2022 (iso) = Dec 19, 2021 (julian) |
| 375 |
TestCase { |
| 376 |
rd: Date::try_new_iso(2022, 1, 1).unwrap().to_rata_die(), |
| 377 |
era: "ce", |
| 378 |
year: 2021, |
| 379 |
month: 12, |
| 380 |
day: 19, |
| 381 |
}, |
| 382 |
// March 1st, 2022 (iso) = Feb 16, 2022 (julian) |
| 383 |
TestCase { |
| 384 |
era: "ce", |
| 385 |
year: 2022, |
| 386 |
month: 2, |
| 387 |
day: 16, |
| 388 |
rd: Date::try_new_iso(2022, 3, 1).unwrap().to_rata_die(), |
| 389 |
}, |
| 390 |
]; |
| 391 |
|
| 392 |
for case in cases { |
| 393 |
check_case(case) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn test_roundtrip_negative() { |
| 399 |
// https://github.com/unicode-org/icu4x/issues/2254 |
| 400 |
let rd = Date::try_new_iso(-1000, 3, 3).unwrap().to_rata_die(); |
| 401 |
let julian = Date::from_rata_die(rd, Julian::new()); |
| 402 |
let recovered_rd = julian.to_rata_die(); |
| 403 |
assert_eq!(rd, recovered_rd); |
| 404 |
} |
| 405 |
|
| 406 |
#[derive(Debug)] |
| 407 |
struct TestCase { |
| 408 |
rd: RataDie, |
| 409 |
year: i32, |
| 410 |
era: &'static str, |
| 411 |
month: u8, |
| 412 |
day: u8, |
| 413 |
} |
| 414 |
|
| 415 |
fn check_case(case: TestCase) { |
| 416 |
let date = Date::from_rata_die(case.rd, Julian); |
| 417 |
|
| 418 |
assert_eq!(date.to_rata_die(), case.rd, "{case:?}"); |
| 419 |
|
| 420 |
assert_eq!(date.era_year().year, case.year, "{case:?}"); |
| 421 |
assert_eq!(date.era_year().era, case.era, "{case:?}"); |
| 422 |
assert_eq!(date.month().ordinal, case.month, "{case:?}"); |
| 423 |
assert_eq!(date.day_of_month().0, case.day, "{case:?}"); |
| 424 |
|
| 425 |
assert_eq!( |
| 426 |
Date::try_new_julian( |
| 427 |
date.era_year().extended_year, |
| 428 |
date.month().ordinal, |
| 429 |
date.day_of_month().0 |
| 430 |
), |
| 431 |
Ok(date), |
| 432 |
"{case:?}" |
| 433 |
); |
| 434 |
} |
| 435 |
|
| 436 |
#[test] |
| 437 |
fn test_julian_near_era_change() { |
| 438 |
// Tests that the Julian calendar gives the correct expected |
| 439 |
// day, month, and year for positive years (CE) |
| 440 |
|
| 441 |
let cases = [ |
| 442 |
TestCase { |
| 443 |
rd: RataDie::new(1), |
| 444 |
year: 1, |
| 445 |
era: "ce", |
| 446 |
month: 1, |
| 447 |
day: 3, |
| 448 |
}, |
| 449 |
TestCase { |
| 450 |
rd: RataDie::new(0), |
| 451 |
year: 1, |
| 452 |
era: "ce", |
| 453 |
month: 1, |
| 454 |
day: 2, |
| 455 |
}, |
| 456 |
TestCase { |
| 457 |
rd: RataDie::new(-1), |
| 458 |
year: 1, |
| 459 |
era: "ce", |
| 460 |
month: 1, |
| 461 |
day: 1, |
| 462 |
}, |
| 463 |
TestCase { |
| 464 |
rd: RataDie::new(-2), |
| 465 |
year: 1, |
| 466 |
era: "bce", |
| 467 |
month: 12, |
| 468 |
day: 31, |
| 469 |
}, |
| 470 |
TestCase { |
| 471 |
rd: RataDie::new(-3), |
| 472 |
year: 1, |
| 473 |
era: "bce", |
| 474 |
month: 12, |
| 475 |
day: 30, |
| 476 |
}, |
| 477 |
TestCase { |
| 478 |
rd: RataDie::new(-367), |
| 479 |
year: 1, |
| 480 |
era: "bce", |
| 481 |
month: 1, |
| 482 |
day: 1, |
| 483 |
}, |
| 484 |
TestCase { |
| 485 |
rd: RataDie::new(-368), |
| 486 |
year: 2, |
| 487 |
era: "bce", |
| 488 |
month: 12, |
| 489 |
day: 31, |
| 490 |
}, |
| 491 |
TestCase { |
| 492 |
rd: RataDie::new(-1462), |
| 493 |
year: 4, |
| 494 |
era: "bce", |
| 495 |
month: 1, |
| 496 |
day: 1, |
| 497 |
}, |
| 498 |
TestCase { |
| 499 |
rd: RataDie::new(-1463), |
| 500 |
year: 5, |
| 501 |
era: "bce", |
| 502 |
month: 12, |
| 503 |
day: 31, |
| 504 |
}, |
| 505 |
]; |
| 506 |
|
| 507 |
for case in cases { |
| 508 |
check_case(case) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn test_julian_rd_date_conversion() { |
| 514 |
// Tests that converting from RD to Julian then |
| 515 |
// back to RD yields the same RD |
| 516 |
for i in -10000..=10000 { |
| 517 |
let rd = RataDie::new(i); |
| 518 |
let julian = Date::from_rata_die(rd, Julian); |
| 519 |
let new_rd = julian.to_rata_die(); |
| 520 |
assert_eq!(rd, new_rd); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
#[test] |
| 525 |
fn test_julian_directionality() { |
| 526 |
// Tests that for a large range of RDs, if a RD |
| 527 |
// is less than another, the corresponding YMD should also be less |
| 528 |
// than the other, without exception. |
| 529 |
for i in -100..=100 { |
| 530 |
for j in -100..=100 { |
| 531 |
let julian_i = Date::from_rata_die(RataDie::new(i), Julian); |
| 532 |
let julian_j = Date::from_rata_die(RataDie::new(j), Julian); |
| 533 |
|
| 534 |
assert_eq!( |
| 535 |
i.cmp(&j), |
| 536 |
julian_i.inner().0.cmp(&julian_j.inner().0), |
| 537 |
"Julian directionality inconsistent with directionality for i: {i}, j: {j}" |
| 538 |
); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn test_julian_leap_years() { |
| 545 |
Date::try_new_julian(4, 2, 29).unwrap(); |
| 546 |
Date::try_new_julian(0, 2, 29).unwrap(); |
| 547 |
Date::try_new_julian(-4, 2, 29).unwrap(); |
| 548 |
Date::try_new_julian(2020, 2, 29).unwrap(); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn test_constructor_roundtrip() { |
| 553 |
let rds = crate::tests::get_interesting_rds(); |
| 554 |
for rd in rds { |
| 555 |
let date = Date::from_rata_die(rd, Julian); |
| 556 |
let reconstructed = Date::try_new_julian( |
| 557 |
date.year().extended_year(), |
| 558 |
date.month().ordinal, |
| 559 |
date.day_of_month().0, |
| 560 |
) |
| 561 |
.unwrap(); |
| 562 |
assert_eq!( |
| 563 |
reconstructed.to_rata_die(), |
| 564 |
rd, |
| 565 |
"Julian failed for RD {:?}", |
| 566 |
rd |
| 567 |
); |
| 568 |
} |
| 569 |
} |
| 570 |
} |