Skip to content

Commit

Permalink
Fix panics when no relative_to is supplied to round
Browse files Browse the repository at this point in the history
  • Loading branch information
robot-head committed Mar 10, 2024
1 parent a48e53e commit 96c8184
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/components/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,21 +568,25 @@ impl Duration {
};
// 35. Let calendarRec be ? CreateCalendarMethodsRecordFromRelativeTo(plainRelativeTo, zonedRelativeTo, « DATE-ADD, DATE-UNTIL »).

// TODO: Validate below expect -> Potentially return error.
let relative_to_date = relative_to.date.expect("Date must exist at this point.");

// 36. Let unbalanceResult be ? UnbalanceDateDurationRelative(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], largestUnit, plainRelativeTo, calendarRec).
let unbalanced =
self.date()
.unbalance_relative(largest_unit, Some(relative_to_date), context)?;

// NOTE: Step 37 handled in round duration
// 37. Let norm be NormalizeTimeDuration(duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]],
// duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
// 38. Let roundRecord be ? RoundDuration(unbalanceResult.[[Years]], unbalanceResult.[[Months]],
// unbalanceResult.[[Weeks]], unbalanceResult.[[Days]], norm, roundingIncrement, smallestUnit,
// roundingMode, plainRelativeTo, calendarRec, zonedRelativeTo, timeZoneRec, precalculatedPlainDateTime).
let (round_result, _) = Self::new_unchecked(unbalanced, *self.time()).round_internal(
// let relative_to_date = relative_to.date;

let (round_result, _) = if let Some(relative_to_date) = relative_to.date {
// 36. Let unbalanceResult be ? UnbalanceDateDurationRelative(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], largestUnit, plainRelativeTo, calendarRec).
let unbalanced =
self.date()
.unbalance_relative(largest_unit, Some(relative_to_date), context)?;

// NOTE: Step 37 handled in round duration
// 37. Let norm be NormalizeTimeDuration(duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]],
// duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
// 38. Let roundRecord be ? RoundDuration(unbalanceResult.[[Years]], unbalanceResult.[[Months]],
// unbalanceResult.[[Weeks]], unbalanceResult.[[Days]], norm, roundingIncrement, smallestUnit,
// roundingMode, plainRelativeTo, calendarRec, zonedRelativeTo, timeZoneRec, precalculatedPlainDateTime).
Self::new_unchecked(unbalanced, *self.time())
} else {
*self
}
.round_internal(
increment,
smallest_unit,
mode,
Expand Down Expand Up @@ -618,12 +622,16 @@ impl Duration {
round_result.0 .0.weeks,
balance_result.0,
);
let result = intermediate.balance_relative(
largest_unit,
smallest_unit,
Some(relative_to_date),
context,
)?;
let result = if let Some(relative_to_date) = relative_to.date {
intermediate.balance_relative(
largest_unit,
smallest_unit,
Some(relative_to_date),
context,
)?
} else {
intermediate
};

// 43. Return ! CreateTemporalDuration(result.[[Years]], result.[[Months]],
// result.[[Weeks]], result.[[Days]], balanceResult.[[Hours]], balanceResult.[[Minutes]],
Expand Down

0 comments on commit 96c8184

Please sign in to comment.