Skip to content

Followup #6440 #6461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/calendar/src/provider/hijri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl PackedHijriYearInfo {
month_lengths: [bool; 12],
start_day: RataDie,
) -> Self {
let start_offset = start_day.const_diff(Self::mean_synodic_start_day(extended_year));
let start_offset = start_day.diff(Self::mean_synodic_start_day(extended_year));

debug_assert!(
-8 < start_offset && start_offset < 8,
Expand Down Expand Up @@ -117,7 +117,7 @@ impl PackedHijriYearInfo {
const fn mean_synodic_start_day(extended_year: i32) -> RataDie {
// -1 because the epoch is new year of year 1
// truncating instead of flooring does not matter, as this is used for positive years only
calendrical_calculations::islamic::ISLAMIC_EPOCH_FRIDAY.const_add(
calendrical_calculations::islamic::ISLAMIC_EPOCH_FRIDAY.add(
((extended_year - 1) as f64 * calendrical_calculations::islamic::MEAN_YEAR_LENGTH)
as i64,
)
Expand Down
2 changes: 1 addition & 1 deletion utils/calendrical_calculations/src/ethiopian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::helpers::I32CastError;
use crate::rata_die::RataDie;

const ETHIOPIC_TO_COPTIC_OFFSET: i64 =
super::coptic::COPTIC_EPOCH.const_diff(crate::julian::fixed_from_julian(8, 8, 29));
super::coptic::COPTIC_EPOCH.diff(crate::julian::fixed_from_julian(8, 8, 29));

/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L2017>
pub fn fixed_from_ethiopian(year: i32, month: u8, day: u8) -> RataDie {
Expand Down
2 changes: 1 addition & 1 deletion utils/calendrical_calculations/src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn fixed_from_iso(year: i32, month: u8, day: u8) -> RataDie {
/// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1191-L1217>
pub(crate) const fn iso_year_from_fixed(date: RataDie) -> i64 {
// Shouldn't overflow because it's not possbile to construct extreme values of RataDie
let date = date.const_diff(EPOCH);
let date = date.diff(EPOCH);

// 400 year cycles have 146097 days
let (n_400, date) = (date.div_euclid(146097), date.rem_euclid(146097));
Expand Down
35 changes: 18 additions & 17 deletions utils/calendrical_calculations/src/rata_die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl RataDie {
}

/// A valid `RataDie` that is intended to be below all dates representable in calendars
#[doc(hidden)]
#[doc(hidden)] // for testing only
pub const fn big_negative() -> Self {
Self::new(i64::MIN / 256 / 256)
}
Expand All @@ -67,14 +67,25 @@ impl RataDie {
self.0 as f64
}

/// Subtracts a number of days from this `RataDie` in a const-friendly way
pub const fn sub(self, rhs: i64) -> RataDie {
let result = Self(self.0 - rhs);
#[cfg(debug_assertions)]
result.check();
result
}

/// Calculate the number of days between two `RataDie` in a const-friendly way
pub const fn const_diff(self, rhs: Self) -> i64 {
pub const fn diff(self, rhs: Self) -> i64 {
self.0 - rhs.0
}

/// Adds a number of days to this `RataDie` in a const-friendly way
pub const fn const_add(self, rhs: i64) -> Self {
Self(self.0 + rhs)
pub const fn add(self, rhs: i64) -> Self {
let result = Self(self.0 + rhs);
#[cfg(debug_assertions)]
result.check();
result
}

/// Convert this to a [`Moment`]
Expand All @@ -98,45 +109,35 @@ impl fmt::Debug for RataDie {
impl Add<i64> for RataDie {
type Output = Self;
fn add(self, rhs: i64) -> Self::Output {
let result = Self(self.0 + rhs);
#[cfg(debug_assertions)]
result.check();
result
self.add(rhs)
}
}

impl AddAssign<i64> for RataDie {
fn add_assign(&mut self, rhs: i64) {
self.0 += rhs;
#[cfg(debug_assertions)]
self.check();
}
}

/// Shift a RataDie N days into the past
impl Sub<i64> for RataDie {
type Output = Self;
fn sub(self, rhs: i64) -> Self::Output {
let result = Self(self.0 - rhs);
#[cfg(debug_assertions)]
result.check();
result
self.sub(rhs)
}
}

impl SubAssign<i64> for RataDie {
fn sub_assign(&mut self, rhs: i64) {
self.0 -= rhs;
#[cfg(debug_assertions)]
self.check();
}
}

/// Calculate the number of days between two RataDie
impl Sub for RataDie {
type Output = i64;
fn sub(self, rhs: Self) -> Self::Output {
self.0 - rhs.0
self.diff(rhs)
}
}

Expand Down