Skip to content
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

Implement add & subtract methods for DateTime component #45

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/components/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl<C: CalendarProtocol> CalendarSlot<C> {

// 10. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]],
// duration.[[Months]], duration.[[Weeks]], duration.[[Days]] + balanceResult.[[Days]], overflow).
let result = date.iso.add_iso_date(
let result = date.iso.add_date_duration(
&DateDuration::new_unchecked(
duration.years(),
duration.months(),
Expand Down
19 changes: 6 additions & 13 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<C: CalendarProtocol> Date<C> {
duration: &Duration,
context: &mut C::Context,
) -> TemporalResult<(Self, f64)> {
let new_date = self.add_date(duration, ArithmeticOverflow::Constrain, context)?;
let new_date = self.add_date(duration, None, context)?;
let days = f64::from(self.days_until(&new_date));
Ok((new_date, days))
}
Expand All @@ -56,10 +56,11 @@ impl<C: CalendarProtocol> Date<C> {
pub(crate) fn add_date(
&self,
duration: &Duration,
overflow: ArithmeticOverflow,
overflow: Option<ArithmeticOverflow>,
context: &mut C::Context,
) -> TemporalResult<Self> {
// 2. If options is not present, set options to undefined.
let overflow = overflow.unwrap_or(ArithmeticOverflow::Constrain);
// 3. If duration.[[Years]] ≠ 0, or duration.[[Months]] ≠ 0, or duration.[[Weeks]] ≠ 0, then
if duration.date().years != 0.0
|| duration.date().months != 0.0
Expand All @@ -81,7 +82,7 @@ impl<C: CalendarProtocol> Date<C> {
// 7. Let result be ? AddISODate(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], 0, 0, 0, days, overflow).
let result = self
.iso
.add_iso_date(&DateDuration::new(0f64, 0f64, 0f64, days)?, overflow)?;
.add_date_duration(&DateDuration::new(0f64, 0f64, 0f64, days)?, overflow)?;

Ok(Self::new_unchecked(result, self.calendar().clone()))
}
Expand Down Expand Up @@ -301,11 +302,7 @@ impl<C: CalendarProtocol> Date<C> {
overflow: Option<ArithmeticOverflow>,
context: &mut C::Context,
) -> TemporalResult<Self> {
self.add_date(
duration,
overflow.unwrap_or(ArithmeticOverflow::Constrain),
context,
)
self.add_date(duration, overflow, context)
}

pub fn contextual_subtract(
Expand All @@ -314,11 +311,7 @@ impl<C: CalendarProtocol> Date<C> {
overflow: Option<ArithmeticOverflow>,
context: &mut C::Context,
) -> TemporalResult<Self> {
self.add_date(
&duration.negated(),
overflow.unwrap_or(ArithmeticOverflow::Constrain),
context,
)
self.add_date(&duration.negated(), overflow, context)
}

pub fn contextual_until(
Expand Down
Loading