Skip to content

Commit a2d6518

Browse files
authored
Implement add & subtract methods for DateTime component (#45)
* Add isodatetime methods * Build out add and subtract datetime functionality
1 parent d8ddcfd commit a2d6518

File tree

8 files changed

+295
-75
lines changed

8 files changed

+295
-75
lines changed

src/components/calendar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<C: CalendarProtocol> CalendarSlot<C> {
503503

504504
// 10. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]],
505505
// duration.[[Months]], duration.[[Weeks]], duration.[[Days]] + balanceResult.[[Days]], overflow).
506-
let result = date.iso.add_iso_date(
506+
let result = date.iso.add_date_duration(
507507
&DateDuration::new_unchecked(
508508
duration.years(),
509509
duration.months(),

src/components/date.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<C: CalendarProtocol> Date<C> {
4545
duration: &Duration,
4646
context: &mut C::Context,
4747
) -> TemporalResult<(Self, f64)> {
48-
let new_date = self.add_date(duration, ArithmeticOverflow::Constrain, context)?;
48+
let new_date = self.add_date(duration, None, context)?;
4949
let days = f64::from(self.days_until(&new_date));
5050
Ok((new_date, days))
5151
}
@@ -56,10 +56,11 @@ impl<C: CalendarProtocol> Date<C> {
5656
pub(crate) fn add_date(
5757
&self,
5858
duration: &Duration,
59-
overflow: ArithmeticOverflow,
59+
overflow: Option<ArithmeticOverflow>,
6060
context: &mut C::Context,
6161
) -> TemporalResult<Self> {
6262
// 2. If options is not present, set options to undefined.
63+
let overflow = overflow.unwrap_or(ArithmeticOverflow::Constrain);
6364
// 3. If duration.[[Years]] ≠ 0, or duration.[[Months]] ≠ 0, or duration.[[Weeks]] ≠ 0, then
6465
if duration.date().years != 0.0
6566
|| duration.date().months != 0.0
@@ -81,7 +82,7 @@ impl<C: CalendarProtocol> Date<C> {
8182
// 7. Let result be ? AddISODate(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], 0, 0, 0, days, overflow).
8283
let result = self
8384
.iso
84-
.add_iso_date(&DateDuration::new(0f64, 0f64, 0f64, days)?, overflow)?;
85+
.add_date_duration(&DateDuration::new(0f64, 0f64, 0f64, days)?, overflow)?;
8586

8687
Ok(Self::new_unchecked(result, self.calendar().clone()))
8788
}
@@ -301,11 +302,7 @@ impl<C: CalendarProtocol> Date<C> {
301302
overflow: Option<ArithmeticOverflow>,
302303
context: &mut C::Context,
303304
) -> TemporalResult<Self> {
304-
self.add_date(
305-
duration,
306-
overflow.unwrap_or(ArithmeticOverflow::Constrain),
307-
context,
308-
)
305+
self.add_date(duration, overflow, context)
309306
}
310307

311308
pub fn contextual_subtract(
@@ -314,11 +311,7 @@ impl<C: CalendarProtocol> Date<C> {
314311
overflow: Option<ArithmeticOverflow>,
315312
context: &mut C::Context,
316313
) -> TemporalResult<Self> {
317-
self.add_date(
318-
&duration.negated(),
319-
overflow.unwrap_or(ArithmeticOverflow::Constrain),
320-
context,
321-
)
314+
self.add_date(&duration.negated(), overflow, context)
322315
}
323316

324317
pub fn contextual_until(

0 commit comments

Comments
 (0)