Skip to content

Commit

Permalink
Actual fix -> div_euclid along with another adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Mar 4, 2024
1 parent c6a1662 commit e625714
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 9 deletions.
145 changes: 145 additions & 0 deletions src/components/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,151 @@ mod tests {
(2019, 6, 0, 21, 0, 0, 0, 0, 0, 0),
),
((2019, 12, 30), (2021, 3, 5), (1, 2, 0, 5, 0, 0, 0, 0, 0, 0)),
(
(2021, 7, 17),
(2021, 07, 16),
(0, 0, 0, -1, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 23),
(2021, 07, 16),
(0, 0, 0, -7, 0, 0, 0, 0, 0, 0),
),
(
(2021, 08, 16),
(2021, 07, 16),
(0, -1, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 1, 16),
(2020, 12, 16),
(0, -1, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 02, 05),
(2021, 01, 05),
(0, -1, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 03, 07),
(2021, 01, 07),
(0, -2, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 08, 17),
(2021, 07, 16),
(0, -1, 0, -1, 0, 0, 0, 0, 0, 0),
),
(
(2021, 08, 13),
(2021, 07, 16),
(0, 0, 0, -28, 0, 0, 0, 0, 0, 0),
),
(
(2021, 09, 16),
(2021, 07, 16),
(0, -2, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2022, 07, 16),
(2021, 07, 16),
(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2031, 07, 16),
(2021, 07, 16),
(-10, 0, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2022, 07, 19),
(2021, 07, 16),
(-1, 0, 0, -3, 0, 0, 0, 0, 0, 0),
),
(
(2022, 09, 19),
(2021, 07, 16),
(-1, -2, 0, -3, 0, 0, 0, 0, 0, 0),
),
(
(2031, 12, 16),
(2021, 07, 16),
(-10, -5, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(1997, 12, 16),
(-23, -7, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(1997, 07, 16),
(-24, 0, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 15),
(1997, 07, 16),
(-23, -11, 0, -30, 0, 0, 0, 0, 0, 0),
),
(
(2021, 06, 15),
(1997, 06, 16),
(-23, -11, 0, -29, 0, 0, 0, 0, 0, 0),
),
(
(2020, 03, 16),
(1960, 02, 16),
(-60, -1, 0, 0, 0, 0, 0, 0, 0, 0),
),
(
(2021, 03, 15),
(1960, 02, 16),
(-61, 0, 0, -28, 0, 0, 0, 0, 0, 0),
),
(
(2020, 03, 15),
(1960, 02, 16),
(-60, 0, 0, -28, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(2021, 03, 30),
(0, -3, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(2020, 03, 30),
(-1, -3, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(1960, 03, 30),
(-61, -3, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(2019, 12, 30),
(-1, -6, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(2020, 12, 30),
(0, -6, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(1997, 12, 30),
(-23, -6, 0, -17, 0, 0, 0, 0, 0, 0),
),
(
(2021, 07, 16),
(0001, 12, 25),
(-2019, -6, 0, -22, 0, 0, 0, 0, 0, 0),
),
(
(2021, 03, 05),
(2019, 12, 30),
(-1, -2, 0, -6, 0, 0, 0, 0, 0, 0),
),
];

let calendar = CalendarSlot::<()>::from_str("iso8601").unwrap();
Expand Down
25 changes: 16 additions & 9 deletions src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,12 @@ impl IsoDate {
let mut years = 0;
// 6. If largestUnit is "year", then
if largest_unit == TemporalUnit::Year {
// others.year - self.year is adopted from temporal-proposal/polyfill as it saves iterations.
// a. Let candidateYears be sign.
let mut candidate_years: i32 = sign.into();
let mut candidate_years: i32 = other.year - self.year;
if candidate_years != 0 {
candidate_years -= i32::from(sign);
}
// b. Repeat, while ISODateSurpasses(sign, y1 + candidateYears, m1, d1, y2, m2, d2) is false,
while !iso_date_surpasses(
&IsoDate::new_unchecked(self.year + candidate_years, self.month, self.day),
Expand All @@ -278,12 +282,12 @@ impl IsoDate {
// a. Let candidateMonths be sign.
let mut candidate_months: i32 = sign.into();
// b. Let intermediate be BalanceISOYearMonth(y1 + years, m1 + candidateMonths).
let (mut intermediate_year, mut intermediate_month) =
let mut intermediate =
balance_iso_year_month(self.year + years, i32::from(self.month) + candidate_months);
// c. Repeat, while ISODateSurpasses(sign, intermediate.[[Year]], intermediate.[[Month]], d1, y2, m2, d2) is false,
// Safety: balance_iso_year_month should always return a month value from 1..=12
while !iso_date_surpasses(
&IsoDate::new_unchecked(intermediate_year, intermediate_month as u8, self.day),
&IsoDate::new_unchecked(intermediate.0, intermediate.1 as u8, self.day),
other,
sign,
) {
Expand All @@ -292,8 +296,8 @@ impl IsoDate {
// ii. Set candidateMonths to candidateMonths + sign.
candidate_months += i32::from(sign);
// iii. Set intermediate to BalanceISOYearMonth(intermediate.[[Year]], intermediate.[[Month]] + sign).
(intermediate_year, intermediate_month) =
balance_iso_year_month(intermediate_year, intermediate_month + i32::from(sign));
intermediate =
balance_iso_year_month(intermediate.0, intermediate.1 + i32::from(sign));
}
}

Expand Down Expand Up @@ -709,7 +713,7 @@ fn iso_date_to_epoch_days(year: i32, month: i32, day: i32) -> i32 {
let month_t = utils::epoch_time_for_month_given_year(resolved_month, resolved_year);

// 4. Return EpochTimeToDayNumber(t) + date - 1.
utils::epoch_time_to_day_number((year_t.abs() + month_t).copysign(year_t)) + day - 1
utils::epoch_time_to_day_number((year_t + month_t).copysign(year_t)) + day - 1
}

#[inline]
Expand All @@ -731,9 +735,12 @@ fn iso_date_surpasses(this: &IsoDate, other: &IsoDate, sign: i8) -> bool {

#[inline]
fn balance_iso_year_month(year: i32, month: i32) -> (i32, i32) {
let y = year + (month - 1) / 12;
// NOTE(nekevss): MUST be `Rem``, not `rem_euclid`.
let m = (month - 1) % 12 + 1;
// 1. Assert: year and month are integers.
// 2. Set year to year + floor((month - 1) / 12).
let y = year + (month - 1).div_euclid(12);
// 3. Set month to ((month - 1) modulo 12) + 1.
let m = (month - 1).rem_euclid(12) + 1;
// 4. Return the Record { [[Year]]: year, [[Month]]: month }.
(y, m)
}

Expand Down

0 comments on commit e625714

Please sign in to comment.