| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
use crate::helpers::i64_to_i32; |
| 85 |
use crate::rata_die::RataDie; |
| 86 |
use core::cmp::Ordering; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
brew |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
brew |
| 102 |
). |
| 103 |
|
| 104 |
is |
| 105 |
to |
| 106 |
he |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#[inline] |
| 121 |
fn months_preceding_molad(h_year: i32) -> i64 { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
(235 * (i64::from(h_year) - 1) + 1).div_euclid(19) |
| 128 |
} |
| 129 |
|
| 130 |
nt |
| 131 |
ek |
| 132 |
macro_rules! ḥal { |
| 133 |
($d:literal-$h:literal-$p:literal) => {{ |
| 134 |
const CONSTANT: i32 = (($d - 1) * 24 + $h) * 1080 + $p; |
| 135 |
CONSTANT |
| 136 |
}}; |
| 137 |
(0-indexed $d:literal-$h:literal-$p:literal) => {{ |
| 138 |
const CONSTANT: i32 = ($d * 24 + $h) * 1080 + $p; |
| 139 |
CONSTANT |
| 140 |
}}; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
r) |
| 145 |
|
| 146 |
4. |
| 147 |
umerals) |
| 148 |
|
| 149 |
M) |
| 150 |
|
| 151 |
|
| 152 |
const MOLAD_BEHERAD_OFFSET: i32 = ḥal!(2 - 5 - 204); |
| 153 |
|
| 154 |
me |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
l. |
| 162 |
|
| 163 |
const HEBREW_LUNATION_TIME: i32 = ḥal!(0-indexed 29-12-793); |
| 164 |
|
| 165 |
|
| 166 |
const HEBREW_APPROX_YEAR_LENGTH: f64 = 35975351.0 / 98496.0; |
| 167 |
|
| 168 |
ek |
| 169 |
|
| 170 |
|
| 171 |
const ḤALAKIM_IN_WEEK: i64 = 1080 * 24 * 7; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
const HEBREW_CALENDAR_EPOCH: RataDie = crate::julian::fixed_from_julian_book_version(-3761, 10, 7); |
| 178 |
|
| 179 |
|
| 180 |
pub const HEBREW_MIN_YEAR: i32 = i32::MIN; |
| 181 |
|
| 182 |
|
| 183 |
pub const HEBREW_MIN_RD: RataDie = RataDie::new(-784362951979); |
| 184 |
|
| 185 |
|
| 186 |
pub const HEBREW_MAX_YEAR: i32 = i32::MAX; |
| 187 |
|
| 188 |
|
| 189 |
pub const HEBREW_MAX_RD: RataDie = RataDie::new(784360204356); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
0) |
| 195 |
#[inline] |
| 196 |
fn molad_details(h_year: i32) -> (i64, i32) { |
| 197 |
let months_preceding = months_preceding_molad(h_year); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
let molad = MOLAD_BEHERAD_OFFSET as i64 + months_preceding * HEBREW_LUNATION_TIME as i64; |
| 202 |
|
| 203 |
|
| 204 |
let weeks_since_beharad = molad.div_euclid(ḤALAKIM_IN_WEEK); |
| 205 |
let in_week = molad.rem_euclid(ḤALAKIM_IN_WEEK); |
| 206 |
|
| 207 |
let in_week = i32::try_from(in_week); |
| 208 |
debug_assert!(in_week.is_ok(), "ḤALAKIM_IN_WEEK should fit in an i32"); |
| 209 |
|
| 210 |
(weeks_since_beharad, in_week.unwrap_or(0)) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 215 |
#[allow(clippy::exhaustive_structs)] |
| 216 |
pub struct YearInfo { |
| 217 |
|
| 218 |
pub keviyah: Keviyah, |
| 219 |
|
| 220 |
pub weeks_since_beharad: i64, |
| 221 |
} |
| 222 |
|
| 223 |
impl YearInfo { |
| 224 |
|
| 225 |
#[inline] |
| 226 |
pub fn compute_for(h_year: i32) -> Self { |
| 227 |
let (mut weeks_since_beharad, ḥalakim) = molad_details(h_year); |
| 228 |
|
| 229 |
let cycle_type = MetonicCycleType::for_h_year(h_year); |
| 230 |
|
| 231 |
let keviyah = keviyah_for(cycle_type, ḥalakim); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if ḥalakim > ḥal!(7 - 18 - 0) { |
| 237 |
weeks_since_beharad += 1; |
| 238 |
} |
| 239 |
|
| 240 |
Self { |
| 241 |
keviyah, |
| 242 |
weeks_since_beharad, |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
#[inline] |
| 250 |
pub fn year_containing_rd(date: RataDie) -> (Self, i32) { |
| 251 |
#[allow(unused_imports)] |
| 252 |
use core_maths::*; |
| 253 |
|
| 254 |
let date = date.clamp(HEBREW_MIN_RD, HEBREW_MAX_RD); |
| 255 |
|
| 256 |
let days_since_epoch = (date - HEBREW_CALENDAR_EPOCH) as f64; |
| 257 |
let maybe_approx = |
| 258 |
i64_to_i32(1 + days_since_epoch.div_euclid(HEBREW_APPROX_YEAR_LENGTH) as i64); |
| 259 |
let approx = maybe_approx.unwrap_or_else(|e| e.saturate()); |
| 260 |
|
| 261 |
let yi = Self::compute_for(approx); |
| 262 |
|
| 263 |
rd |
| 264 |
let cmp = yi.compare(date); |
| 265 |
|
| 266 |
let (yi, h_year) = match cmp { |
| 267 |
|
| 268 |
Ordering::Greater => { |
| 269 |
let prev = approx.saturating_sub(1); |
| 270 |
(Self::compute_for(prev), prev) |
| 271 |
} |
| 272 |
|
| 273 |
Ordering::Equal => (yi, approx), |
| 274 |
|
| 275 |
Ordering::Less => { |
| 276 |
let next = approx.saturating_add(1); |
| 277 |
(Self::compute_for(next), next) |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
debug_assert!(yi.compare(date).is_eq(), |
| 282 |
"Date {date:?} calculated approximately to Hebrew Year {approx} (comparison: {cmp:?}), \ |
| 283 |
should be contained in adjacent year {h_year} but that year is still {:?} it", yi.compare(date)); |
| 284 |
|
| 285 |
(yi, h_year) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
rd |
| 292 |
fn compare(self, rd: RataDie) -> Ordering { |
| 293 |
let ny = self.new_year(); |
| 294 |
let len = self.keviyah.year_length(); |
| 295 |
|
| 296 |
if rd < ny { |
| 297 |
Ordering::Greater |
| 298 |
} else if rd >= ny + len.into() { |
| 299 |
Ordering::Less |
| 300 |
} else { |
| 301 |
Ordering::Equal |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
#[inline] |
| 307 |
pub fn new_year(self) -> RataDie { |
| 308 |
|
| 309 |
const BEHARAD_START_OF_YEAR: StartOfYear = StartOfYear::Monday; |
| 310 |
let days_since_beharad = (self.weeks_since_beharad * 7) |
| 311 |
+ self.keviyah.start_of_year() as i64 |
| 312 |
- BEHARAD_START_OF_YEAR as i64; |
| 313 |
HEBREW_CALENDAR_EPOCH + days_since_beharad |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
ay of |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
ap. |
| 329 |
|
| 330 |
|
| 331 |
(ג). |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] |
| 337 |
#[allow(clippy::exhaustive_enums)] |
| 338 |
pub enum Keviyah { |
| 339 |
|
| 340 |
|
| 341 |
בחג = 0, |
| 342 |
|
| 343 |
בשה = 1, |
| 344 |
|
| 345 |
גכה = 2, |
| 346 |
|
| 347 |
הכז = 3, |
| 348 |
|
| 349 |
השא = 4, |
| 350 |
|
| 351 |
זחא = 5, |
| 352 |
|
| 353 |
זשג = 6, |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
בחה = 7, |
| 358 |
|
| 359 |
בשז = 8, |
| 360 |
|
| 361 |
גכז = 9, |
| 362 |
|
| 363 |
החא = 10, |
| 364 |
|
| 365 |
השג = 11, |
| 366 |
|
| 367 |
זחג = 12, |
| 368 |
|
| 369 |
זשה = 13, |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] |
| 374 |
#[allow(clippy::exhaustive_enums)] |
| 375 |
pub enum YearType { |
| 376 |
9 days |
| 377 |
Deficient = -1, |
| 378 |
has 30 |
| 379 |
Regular = 0, |
| 380 |
0 days |
| 381 |
Complete = 1, |
| 382 |
} |
| 383 |
|
| 384 |
impl YearType { |
| 385 |
|
| 386 |
fn length_correction(self) -> i8 { |
| 387 |
self as i8 |
| 388 |
} |
| 389 |
|
| 390 |
an |
| 391 |
fn ḥeshvan_length(self) -> u8 { |
| 392 |
if self == Self::Complete { |
| 393 |
ḤESHVAN_DEFAULT_LEN + 1 |
| 394 |
} else { |
| 395 |
ḤESHVAN_DEFAULT_LEN |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
fn kislev_length(self) -> u8 { |
| 401 |
if self == Self::Deficient { |
| 402 |
KISLEV_DEFAULT_LEN - 1 |
| 403 |
} else { |
| 404 |
KISLEV_DEFAULT_LEN |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] |
| 410 |
#[allow(clippy::exhaustive_enums)] |
| 411 |
pub enum StartOfYear { |
| 412 |
|
| 413 |
|
| 414 |
Monday = 2, |
| 415 |
|
| 416 |
Tuesday = 3, |
| 417 |
|
| 418 |
Thursday = 5, |
| 419 |
|
| 420 |
Saturday = 7, |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
macro_rules! u16_cvt( |
| 426 |
|
| 427 |
|
| 428 |
($first:ident $(+ $rest:ident)*) => { |
| 429 |
{ |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
const COMPUTED: u16 = $first as u16 $(+ $rest as u16)*; |
| 434 |
COMPUTED |
| 435 |
} |
| 436 |
}; |
| 437 |
); |
| 438 |
|
| 439 |
|
| 440 |
const TISHREI_LEN: u8 = 30; |
| 441 |
|
| 442 |
const ḤESHVAN_DEFAULT_LEN: u8 = 29; |
| 443 |
|
| 444 |
const KISLEV_DEFAULT_LEN: u8 = 30; |
| 445 |
const TEVET_LEN: u8 = 29; |
| 446 |
const SHEVAT_LEN: u8 = 30; |
| 447 |
const ADARI_LEN: u8 = 30; |
| 448 |
const ADAR_LEN: u8 = 29; |
| 449 |
const NISAN_LEN: u8 = 30; |
| 450 |
const IYYAR_LEN: u8 = 29; |
| 451 |
const SIVAN_LEN: u8 = 30; |
| 452 |
const TAMMUZ_LEN: u8 = 29; |
| 453 |
const AV_LEN: u8 = 30; |
| 454 |
const ELUL_LEN: u8 = 29; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
pub const TISHREI: u8 = 1; |
| 461 |
|
| 462 |
pub const ḤESHVAN: u8 = 2; |
| 463 |
|
| 464 |
pub const KISLEV: u8 = 3; |
| 465 |
|
| 466 |
pub const TEVET: u8 = 4; |
| 467 |
|
| 468 |
pub const SHEVAT: u8 = 5; |
| 469 |
|
| 470 |
pub const ADAR: u8 = 6; |
| 471 |
|
| 472 |
pub const NISAN: u8 = 7; |
| 473 |
|
| 474 |
pub const IYYAR: u8 = 8; |
| 475 |
|
| 476 |
pub const SIVAN: u8 = 9; |
| 477 |
|
| 478 |
pub const TAMMUZ: u8 = 10; |
| 479 |
|
| 480 |
pub const AV: u8 = 11; |
| 481 |
|
| 482 |
pub const ELUL: u8 = 12; |
| 483 |
|
| 484 |
impl Keviyah { |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
= C |
| 489 |
#[inline] |
| 490 |
pub fn year_type(self) -> YearType { |
| 491 |
match self { |
| 492 |
Self::בחג => YearType::Deficient, |
| 493 |
Self::בשה => YearType::Complete, |
| 494 |
Self::גכה => YearType::Regular, |
| 495 |
Self::הכז => YearType::Regular, |
| 496 |
Self::השא => YearType::Complete, |
| 497 |
Self::זחא => YearType::Deficient, |
| 498 |
Self::זשג => YearType::Complete, |
| 499 |
Self::בחה => YearType::Deficient, |
| 500 |
Self::בשז => YearType::Complete, |
| 501 |
Self::גכז => YearType::Regular, |
| 502 |
Self::החא => YearType::Deficient, |
| 503 |
Self::השג => YearType::Complete, |
| 504 |
Self::זחג => YearType::Deficient, |
| 505 |
Self::זשה => YearType::Complete, |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
rday |
| 512 |
#[inline] |
| 513 |
pub fn start_of_year(self) -> StartOfYear { |
| 514 |
match self { |
| 515 |
Self::בחג => StartOfYear::Monday, |
| 516 |
Self::בשה => StartOfYear::Monday, |
| 517 |
Self::גכה => StartOfYear::Tuesday, |
| 518 |
Self::הכז => StartOfYear::Thursday, |
| 519 |
Self::השא => StartOfYear::Thursday, |
| 520 |
Self::זחא => StartOfYear::Saturday, |
| 521 |
Self::זשג => StartOfYear::Saturday, |
| 522 |
Self::בחה => StartOfYear::Monday, |
| 523 |
Self::בשז => StartOfYear::Monday, |
| 524 |
Self::גכז => StartOfYear::Tuesday, |
| 525 |
Self::החא => StartOfYear::Thursday, |
| 526 |
Self::השג => StartOfYear::Thursday, |
| 527 |
Self::זחג => StartOfYear::Saturday, |
| 528 |
Self::זשה => StartOfYear::Saturday, |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
#[inline] |
| 537 |
fn normalized_ordinal_month(self, ordinal_month: u8) -> Option<u8> { |
| 538 |
if self.is_leap() { |
| 539 |
match ordinal_month.cmp(&6) { |
| 540 |
|
| 541 |
Ordering::Equal => None, |
| 542 |
Ordering::Less => Some(ordinal_month), |
| 543 |
Ordering::Greater => Some(ordinal_month - 1), |
| 544 |
} |
| 545 |
} else { |
| 546 |
Some(ordinal_month) |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
#[inline] |
| 553 |
pub fn month_len(self, ordinal_month: u8) -> u8 { |
| 554 |
|
| 555 |
let Some(normalized_ordinal_month) = self.normalized_ordinal_month(ordinal_month) else { |
| 556 |
return ADARI_LEN; |
| 557 |
}; |
| 558 |
debug_assert!(normalized_ordinal_month <= 12 && normalized_ordinal_month > 0); |
| 559 |
match normalized_ordinal_month { |
| 560 |
TISHREI => TISHREI_LEN, |
| 561 |
ḤESHVAN => self.year_type().ḥeshvan_length(), |
| 562 |
KISLEV => self.year_type().kislev_length(), |
| 563 |
TEVET => TEVET_LEN, |
| 564 |
SHEVAT => SHEVAT_LEN, |
| 565 |
ADAR => ADAR_LEN, |
| 566 |
NISAN => NISAN_LEN, |
| 567 |
IYYAR => IYYAR_LEN, |
| 568 |
SIVAN => SIVAN_LEN, |
| 569 |
TAMMUZ => TAMMUZ_LEN, |
| 570 |
AV => AV_LEN, |
| 571 |
ELUL => ELUL_LEN, |
| 572 |
_ => { |
| 573 |
debug_assert!(false, "Got unknown month index {ordinal_month}"); |
| 574 |
30 |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
#[inline] |
| 581 |
pub fn days_preceding(self, ordinal_month: u8) -> u16 { |
| 582 |
|
| 583 |
|
| 584 |
const BEFORE_ADAR_DEFAULT_LEN: u16 = u16_cvt!( |
| 585 |
TISHREI_LEN + ḤESHVAN_DEFAULT_LEN + KISLEV_DEFAULT_LEN + TEVET_LEN + SHEVAT_LEN |
| 586 |
); |
| 587 |
|
| 588 |
let Some(normalized_ordinal_month) = self.normalized_ordinal_month(ordinal_month) else { |
| 589 |
|
| 590 |
let corrected = |
| 591 |
BEFORE_ADAR_DEFAULT_LEN as i16 + i16::from(self.year_type().length_correction()); |
| 592 |
return corrected as u16; |
| 593 |
}; |
| 594 |
debug_assert!(normalized_ordinal_month <= ELUL && normalized_ordinal_month > 0); |
| 595 |
|
| 596 |
let year_type = self.year_type(); |
| 597 |
|
| 598 |
let mut days = match normalized_ordinal_month { |
| 599 |
TISHREI => 0, |
| 600 |
ḤESHVAN => u16_cvt!(TISHREI_LEN), |
| 601 |
KISLEV => u16_cvt!(TISHREI_LEN) + u16::from(year_type.ḥeshvan_length()), |
| 602 |
|
| 603 |
|
| 604 |
TEVET => u16_cvt!(TISHREI_LEN + ḤESHVAN_DEFAULT_LEN + KISLEV_DEFAULT_LEN), |
| 605 |
SHEVAT => u16_cvt!(TISHREI_LEN + ḤESHVAN_DEFAULT_LEN + KISLEV_DEFAULT_LEN + TEVET_LEN), |
| 606 |
ADAR => BEFORE_ADAR_DEFAULT_LEN, |
| 607 |
NISAN => u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN), |
| 608 |
IYYAR => u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN + NISAN_LEN), |
| 609 |
SIVAN => u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN + NISAN_LEN + IYYAR_LEN), |
| 610 |
TAMMUZ => { |
| 611 |
u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN + NISAN_LEN + IYYAR_LEN + SIVAN_LEN) |
| 612 |
} |
| 613 |
#[rustfmt::skip] |
| 614 |
AV => u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN + NISAN_LEN + IYYAR_LEN + SIVAN_LEN + TAMMUZ_LEN), |
| 615 |
#[rustfmt::skip] |
| 616 |
_ => u16_cvt!(BEFORE_ADAR_DEFAULT_LEN + ADAR_LEN + NISAN_LEN + IYYAR_LEN + SIVAN_LEN + TAMMUZ_LEN + AV_LEN), |
| 617 |
}; |
| 618 |
|
| 619 |
on |
| 620 |
if normalized_ordinal_month > KISLEV { |
| 621 |
|
| 622 |
debug_assert!(days > 1 && year_type.length_correction().abs() <= 1); |
| 623 |
days = (days as i16 + year_type.length_correction() as i16) as u16; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if normalized_ordinal_month >= ADAR && self.is_leap() { |
| 629 |
days += u16::from(ADARI_LEN); |
| 630 |
} |
| 631 |
|
| 632 |
days |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
pub fn month_day_for(self, mut day: u16) -> (u8, u8) { |
| 637 |
for month in 1..14 { |
| 638 |
let month_len = self.month_len(month); |
| 639 |
if let Ok(day) = u8::try_from(day) { |
| 640 |
if day <= month_len { |
| 641 |
return (month, day); |
| 642 |
} |
| 643 |
} |
| 644 |
day -= u16::from(month_len); |
| 645 |
} |
| 646 |
debug_assert!(false, "Attempted to get Hebrew date for {day:?}, in keviyah {self:?}, didn't have enough days in the year"); |
| 647 |
self.last_month_day_in_year() |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
#[inline] |
| 652 |
pub fn last_month_day_in_year(self) -> (u8, u8) { |
| 653 |
|
| 654 |
if self.is_leap() { |
| 655 |
(13, ELUL_LEN) |
| 656 |
} else { |
| 657 |
(12, ELUL_LEN) |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
#[inline] |
| 663 |
pub fn is_leap(self) -> bool { |
| 664 |
debug_assert_eq!(Self::בחה as u8, 7, "Representation of keviyot changed!"); |
| 665 |
|
| 666 |
|
| 667 |
self >= Self::בחה |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
#[inline] |
| 672 |
pub fn year_info(self, h_year: i32) -> YearInfo { |
| 673 |
let (mut weeks_since_beharad, ḥalakim) = molad_details(h_year); |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
if ḥalakim > ḥal!(7 - 18 - 0) { |
| 679 |
weeks_since_beharad += 1; |
| 680 |
} |
| 681 |
|
| 682 |
YearInfo { |
| 683 |
keviyah: self, |
| 684 |
weeks_since_beharad, |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
#[inline] |
| 690 |
pub fn year_length(self) -> u16 { |
| 691 |
let base_year_length = if self.is_leap() { 384 } else { 354 }; |
| 692 |
|
| 693 |
(base_year_length + i16::from(self.year_type().length_correction())) as u16 |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
#[inline] |
| 699 |
pub fn from_integer(integer: u8) -> Self { |
| 700 |
debug_assert!( |
| 701 |
integer < 14, |
| 702 |
"Keviyah::from_integer() takes in a number between 0 and 13 inclusive" |
| 703 |
); |
| 704 |
match integer { |
| 705 |
0 => Self::בחג, |
| 706 |
1 => Self::בשה, |
| 707 |
2 => Self::גכה, |
| 708 |
3 => Self::הכז, |
| 709 |
4 => Self::השא, |
| 710 |
5 => Self::זחא, |
| 711 |
6 => Self::זשג, |
| 712 |
7 => Self::בחה, |
| 713 |
8 => Self::בשז, |
| 714 |
9 => Self::גכז, |
| 715 |
10 => Self::החא, |
| 716 |
11 => Self::השג, |
| 717 |
12 => Self::זחג, |
| 718 |
_ => Self::זשה, |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 736 |
enum MetonicCycleType { |
| 737 |
|
| 738 |
LMinusOne, |
| 739 |
|
| 740 |
LPlusOne, |
| 741 |
|
| 742 |
LPlusMinusOne, |
| 743 |
|
| 744 |
Leap, |
| 745 |
} |
| 746 |
|
| 747 |
impl MetonicCycleType { |
| 748 |
fn for_h_year(h_year: i32) -> Self { |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
let remainder = h_year.rem_euclid(19); |
| 756 |
match remainder { |
| 757 |
|
| 758 |
2 | 5 | 10 | 13 | 16 => Self::LMinusOne, |
| 759 |
1 | 4 | 9 | 12 | 15 => Self::LPlusOne, |
| 760 |
7 | 18 => Self::LPlusMinusOne, |
| 761 |
_ => { |
| 762 |
debug_assert!(matches!(remainder, 3 | 6 | 8 | 11 | 14 | 17 | 0 | 19)); |
| 763 |
Self::Leap |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
ek |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
const FOUR_GATES_LMINUSONE: [i32; 7] = [ |
| 782 |
ḥal!(7 - 18 - 0), |
| 783 |
ḥal!(1 - 9 - 204), |
| 784 |
ḥal!(2 - 18 - 0), |
| 785 |
ḥal!(3 - 9 - 204), |
| 786 |
ḥal!(5 - 9 - 204), |
| 787 |
ḥal!(5 - 18 - 0), |
| 788 |
ḥal!(6 - 9 - 204), |
| 789 |
]; |
| 790 |
const FOUR_GATES_LPLUSONE: [i32; 7] = [ |
| 791 |
ḥal!(7 - 18 - 0), |
| 792 |
ḥal!(1 - 9 - 204), |
| 793 |
ḥal!(2 - 15 - 589), |
| 794 |
ḥal!(3 - 9 - 204), |
| 795 |
ḥal!(5 - 9 - 204), |
| 796 |
ḥal!(5 - 18 - 0), |
| 797 |
ḥal!(6 - 0 - 408), |
| 798 |
]; |
| 799 |
|
| 800 |
const FOUR_GATES_LPLUSMINUSONE: [i32; 7] = [ |
| 801 |
ḥal!(7 - 18 - 0), |
| 802 |
ḥal!(1 - 9 - 204), |
| 803 |
ḥal!(2 - 15 - 589), |
| 804 |
ḥal!(3 - 9 - 204), |
| 805 |
ḥal!(5 - 9 - 204), |
| 806 |
ḥal!(5 - 18 - 0), |
| 807 |
ḥal!(6 - 9 - 204), |
| 808 |
]; |
| 809 |
|
| 810 |
const FOUR_GATES_LEAP: [i32; 7] = [ |
| 811 |
ḥal!(7 - 18 - 0), |
| 812 |
ḥal!(1 - 20 - 491), |
| 813 |
ḥal!(2 - 18 - 0), |
| 814 |
ḥal!(3 - 18 - 0), |
| 815 |
ḥal!(4 - 11 - 695), |
| 816 |
ḥal!(5 - 18 - 0), |
| 817 |
ḥal!(6 - 20 - 491), |
| 818 |
]; |
| 819 |
|
| 820 |
|
| 821 |
ri |
| 822 |
#[inline] |
| 823 |
fn keviyah_for(year_type: MetonicCycleType, ḥalakim: i32) -> Keviyah { |
| 824 |
let gate = match year_type { |
| 825 |
MetonicCycleType::LMinusOne => FOUR_GATES_LMINUSONE, |
| 826 |
MetonicCycleType::LPlusOne => FOUR_GATES_LPLUSONE, |
| 827 |
MetonicCycleType::LPlusMinusOne => FOUR_GATES_LPLUSMINUSONE, |
| 828 |
MetonicCycleType::Leap => FOUR_GATES_LEAP, |
| 829 |
}; |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
let keviyot = if ḥalakim >= gate[0] || ḥalakim < gate[1] { |
| 835 |
(Keviyah::בחג, Keviyah::בחה) |
| 836 |
} else if ḥalakim < gate[2] { |
| 837 |
(Keviyah::בשה, Keviyah::בשז) |
| 838 |
} else if ḥalakim < gate[3] { |
| 839 |
(Keviyah::גכה, Keviyah::גכז) |
| 840 |
} else if ḥalakim < gate[4] { |
| 841 |
(Keviyah::הכז, Keviyah::החא) |
| 842 |
} else if ḥalakim < gate[5] { |
| 843 |
(Keviyah::השא, Keviyah::השג) |
| 844 |
} else if ḥalakim < gate[6] { |
| 845 |
(Keviyah::זחא, Keviyah::זחג) |
| 846 |
} else { |
| 847 |
(Keviyah::זשג, Keviyah::זשה) |
| 848 |
}; |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
debug_assert!( |
| 853 |
keviyot.0 as u8 + 7 == keviyot.1 as u8, |
| 854 |
"The table above should produce matching-indexed keviyot for the leap/non-leap year" |
| 855 |
); |
| 856 |
#[cfg(debug_assertions)] |
| 857 |
#[allow(clippy::indexing_slicing)] |
| 858 |
if keviyot.0 as u8 == 0 { |
| 859 |
|
| 860 |
debug_assert!( |
| 861 |
ḥalakim >= gate[keviyot.0 as usize] || ḥalakim < gate[(keviyot.0 as usize + 1) % 7], |
| 862 |
"The table above should produce the right indexed keviyah, instead found {keviyot:?} for time {ḥalakim} (year type {year_type:?})" |
| 863 |
); |
| 864 |
} else { |
| 865 |
im |
| 866 |
debug_assert!( |
| 867 |
ḥalakim >= gate[keviyot.0 as usize] && ḥalakim < gate[(keviyot.0 as usize + 1) % 7], |
| 868 |
"The table above should produce the right indexed keviyah, instead found {keviyot:?} for time {ḥalakim} (year type {year_type:?})" |
| 869 |
); |
| 870 |
} |
| 871 |
|
| 872 |
if year_type == MetonicCycleType::Leap { |
| 873 |
keviyot.1 |
| 874 |
} else { |
| 875 |
keviyot.0 |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
#[cfg(test)] |
| 880 |
mod test { |
| 881 |
use super::*; |
| 882 |
use crate::hebrew::{self, BookHebrew}; |
| 883 |
|
| 884 |
#[test] |
| 885 |
fn test_consts() { |
| 886 |
assert_eq!(MOLAD_BEHERAD_OFFSET, 31524); |
| 887 |
assert_eq!(ḤALAKIM_IN_WEEK, 181440); |
| 888 |
|
| 889 |
). |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
assert_eq!(HEBREW_LUNATION_TIME, 765433); |
| 894 |
|
| 895 |
|
| 896 |
assert_eq!(HEBREW_CALENDAR_EPOCH, hebrew::FIXED_HEBREW_EPOCH); |
| 897 |
} |
| 898 |
|
| 899 |
#[test] |
| 900 |
fn test_roundtrip_days() { |
| 901 |
for h_year in (1..10).chain(5775..5795).chain(10000..10010) { |
| 902 |
let year_info = YearInfo::compute_for(h_year); |
| 903 |
let ny = year_info.new_year(); |
| 904 |
for day in 1..=year_info.keviyah.year_length() { |
| 905 |
let offset_date = ny + i64::from(day) - 1; |
| 906 |
let (offset_yearinfo, offset_h_year) = YearInfo::year_containing_rd(offset_date); |
| 907 |
|
| 908 |
assert_eq!( |
| 909 |
offset_h_year, h_year, |
| 910 |
"Backcomputed h_year should be same for day {day} in Hebrew Year {h_year}" |
| 911 |
); |
| 912 |
assert_eq!( |
| 913 |
offset_yearinfo, year_info, |
| 914 |
"Backcomputed YearInfo should be same for day {day} in Hebrew Year {h_year}" |
| 915 |
); |
| 916 |
|
| 917 |
let (month, day2) = year_info.keviyah.month_day_for(day); |
| 918 |
|
| 919 |
let days_preceding = year_info.keviyah.days_preceding(month); |
| 920 |
|
| 921 |
assert_eq!( |
| 922 |
days_preceding + u16::from(day2), |
| 923 |
day, |
| 924 |
"{h_year}-{month}-{day2} should round trip for day-of-year {day}" |
| 925 |
) |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
#[test] |
| 930 |
fn test_book_parity() { |
| 931 |
let mut last_year = None; |
| 932 |
for h_year in (1..100).chain(5600..5900).chain(10000..10100) { |
| 933 |
let book_date = BookHebrew::from_civil_date(h_year, 1, 1); |
| 934 |
let book_ny = BookHebrew::fixed_from_book_hebrew(book_date); |
| 935 |
let kv_yearinfo = YearInfo::compute_for(h_year); |
| 936 |
let kv_ny = kv_yearinfo.new_year(); |
| 937 |
assert_eq!( |
| 938 |
book_ny, |
| 939 |
kv_ny, |
| 940 |
"Book and Keviyah-based years should match for Hebrew Year {h_year}. Got YearInfo {kv_yearinfo:?}" |
| 941 |
); |
| 942 |
let book_is_leap = BookHebrew::is_hebrew_leap_year(h_year); |
| 943 |
assert_eq!( |
| 944 |
book_is_leap, |
| 945 |
kv_yearinfo.keviyah.is_leap(), |
| 946 |
"Book and Keviyah-based years should match for Hebrew Year {h_year}. Got YearInfo {kv_yearinfo:?}" |
| 947 |
); |
| 948 |
|
| 949 |
let book_year_len = BookHebrew::days_in_book_hebrew_year(h_year); |
| 950 |
let book_year_type = match book_year_len { |
| 951 |
355 | 385 => YearType::Complete, |
| 952 |
354 | 384 => YearType::Regular, |
| 953 |
353 | 383 => YearType::Deficient, |
| 954 |
_ => unreachable!("Found unexpected book year len {book_year_len}"), |
| 955 |
}; |
| 956 |
assert_eq!( |
| 957 |
book_year_type, |
| 958 |
kv_yearinfo.keviyah.year_type(), |
| 959 |
"Book and Keviyah-based years should match for Hebrew Year {h_year}. Got YearInfo {kv_yearinfo:?}" |
| 960 |
); |
| 961 |
|
| 962 |
let kv_recomputed_yearinfo = kv_yearinfo.keviyah.year_info(h_year); |
| 963 |
assert_eq!( |
| 964 |
kv_recomputed_yearinfo, |
| 965 |
kv_yearinfo, |
| 966 |
"Recomputed YearInfo should match for Hebrew Year {h_year}. Got YearInfo {kv_yearinfo:?}" |
| 967 |
); |
| 968 |
|
| 969 |
let year_len = kv_yearinfo.keviyah.year_length(); |
| 970 |
|
| 971 |
let month_range = if kv_yearinfo.keviyah.is_leap() { |
| 972 |
1..14 |
| 973 |
} else { |
| 974 |
1..13 |
| 975 |
}; |
| 976 |
|
| 977 |
let mut days_preceding = 0; |
| 978 |
|
| 979 |
for month in month_range { |
| 980 |
let kv_month_len = kv_yearinfo.keviyah.month_len(month); |
| 981 |
let book_date = BookHebrew::from_civil_date(h_year, month, 1); |
| 982 |
let book_month_len = |
| 983 |
BookHebrew::last_day_of_book_hebrew_month(book_date.year, book_date.month); |
| 984 |
assert_eq!(kv_month_len, book_month_len, "Month lengths should be same for ordinal hebrew month {month} in year {h_year}. Got YearInfo {kv_yearinfo:?}"); |
| 985 |
|
| 986 |
assert_eq!(days_preceding, kv_yearinfo.keviyah.days_preceding(month), "Days preceding should be the sum of preceding days for ordinal hebrew month {month} in year {h_year}. Got YearInfo {kv_yearinfo:?}"); |
| 987 |
days_preceding += u16::from(kv_month_len); |
| 988 |
} |
| 989 |
|
| 990 |
for offset in [0, 1, 100, year_len - 100, year_len - 2, year_len - 1] { |
| 991 |
let offset_date = kv_ny + offset.into(); |
| 992 |
let (offset_yearinfo, offset_h_year) = YearInfo::year_containing_rd(offset_date); |
| 993 |
|
| 994 |
assert_eq!(offset_h_year, h_year, "Backcomputed h_year should be same for date {offset_date:?} in Hebrew Year {h_year} (offset from ny {offset})"); |
| 995 |
assert_eq!(offset_yearinfo, kv_yearinfo, "Backcomputed YearInfo should be same for date {offset_date:?} in Hebrew Year {h_year} (offset from ny {offset})"); |
| 996 |
} |
| 997 |
|
| 998 |
if let Some((last_h_year, predicted_ny)) = last_year { |
| 999 |
if last_h_year + 1 == h_year { |
| 1000 |
assert_eq!(predicted_ny, kv_ny, "{last_h_year}'s YearInfo predicts New Year {predicted_ny:?}, which does not match current new year. Got YearInfo {kv_yearinfo:?}"); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
last_year = Some((h_year, kv_ny + year_len.into())) |
| 1005 |
} |
| 1006 |
} |
| 1007 |
#[test] |
| 1008 |
fn test_minmax() { |
| 1009 |
let min = YearInfo::compute_for(HEBREW_MIN_YEAR); |
| 1010 |
let min_ny = min.new_year(); |
| 1011 |
assert_eq!(min_ny, HEBREW_MIN_RD); |
| 1012 |
|
| 1013 |
let (recomputed_yi, recomputed_y) = YearInfo::year_containing_rd(min_ny); |
| 1014 |
assert_eq!(recomputed_y, HEBREW_MIN_YEAR); |
| 1015 |
assert_eq!(recomputed_yi, min); |
| 1016 |
|
| 1017 |
let max = YearInfo::compute_for(HEBREW_MAX_YEAR); |
| 1018 |
let max_ny = max.new_year(); |
| 1019 |
|
| 1020 |
let max_last = max_ny + i64::from(max.keviyah.year_length()) - 1; |
| 1021 |
assert_eq!(max_last, HEBREW_MAX_RD); |
| 1022 |
|
| 1023 |
let (recomputed_yi, recomputed_y) = YearInfo::year_containing_rd(max_last); |
| 1024 |
assert_eq!(recomputed_y, HEBREW_MAX_YEAR); |
| 1025 |
assert_eq!(recomputed_yi, max); |
| 1026 |
} |
| 1027 |
|
| 1028 |
#[test] |
| 1029 |
fn test_leap_agreement() { |
| 1030 |
for year0 in -1000..1000 { |
| 1031 |
let year1 = year0 + 1; |
| 1032 |
let info0 = YearInfo::compute_for(year0); |
| 1033 |
let info1 = YearInfo::compute_for(year1); |
| 1034 |
let num_months = (info1.new_year() - info0.new_year()) / 29; |
| 1035 |
if info0.keviyah.is_leap() { |
| 1036 |
assert_eq!(num_months, 13, "{year0}"); |
| 1037 |
} else { |
| 1038 |
assert_eq!(num_months, 12, "{year0}"); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |