Skip to content

Commit

Permalink
Implement add & subtract methods for DateTime component (#45)
Browse files Browse the repository at this point in the history
* Add isodatetime methods

* Build out add and subtract datetime functionality
  • Loading branch information
nekevss authored May 20, 2024
1 parent d8ddcfd commit a2d6518
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 75 deletions.
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

0 comments on commit a2d6518

Please sign in to comment.