Skip to content

Commit

Permalink
Editorial: Split total() code path out of RoundRelativeDuration
Browse files Browse the repository at this point in the history
In #2758 we kept the code paths for Duration.p.round() and
Duration.p.total() the same. However, it is actually simpler to separate
them in most places, except for NudgeToCalendarUnit.

- Splits TotalRelativeDuration out of RoundRelativeDuration
- Splits TotalTimeDuration out of RoundTimeDuration
- Splits DifferenceZonedDateTimeWithTotal out of
  DifferenceZonedDateTimeWithRounding
- Splits DifferencePlainDateTimeWithTotal out of
  DifferencePlainDateTimeWithRounding
- Removes [[Total]] field from Nudge Result Record
- Changes NudgeToCalendarUnit to return a record of { [[NudgeResult]],
  [[Total]] }

Closes: #2947
  • Loading branch information
ptomato authored and Ms2ger committed Oct 1, 2024
1 parent 8c70250 commit ae6edf0
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 146 deletions.
38 changes: 10 additions & 28 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {

// class static functions and methods
MathAbs,
NumberIsNaN,
ObjectCreate,
ObjectDefineProperty,

Expand Down Expand Up @@ -236,7 +235,7 @@ export class Duration {
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const relativeEpochNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
const targetEpochNs = ES.AddZonedDateTime(relativeEpochNs, timeZone, calendar, duration);
({ duration } = ES.DifferenceZonedDateTimeWithRounding(
duration = ES.DifferenceZonedDateTimeWithRounding(
relativeEpochNs,
targetEpochNs,
timeZone,
Expand All @@ -245,7 +244,7 @@ export class Duration {
roundingIncrement,
smallestUnit,
roundingMode
));
);
if (ES.TemporalUnitCategory(largestUnit) === 'date') largestUnit = 'hour';
return ES.UnnormalizeDuration(duration, largestUnit);
}
Expand All @@ -260,7 +259,7 @@ export class Duration {
const dateDuration = ES.AdjustDateDurationRecord(duration.date, targetTime.deltaDays);
const targetDate = ES.CalendarDateAdd(calendar, isoRelativeToDate, dateDuration, 'constrain');

({ duration } = ES.DifferencePlainDateTimeWithRounding(
duration = ES.DifferencePlainDateTimeWithRounding(
isoRelativeToDate.year,
isoRelativeToDate.month,
isoRelativeToDate.day,
Expand All @@ -284,7 +283,7 @@ export class Duration {
roundingIncrement,
smallestUnit,
roundingMode
));
);
return ES.UnnormalizeDuration(duration, largestUnit);
}

Expand All @@ -297,7 +296,7 @@ export class Duration {
}
assert(!ES.IsCalendarUnit(smallestUnit), 'smallestUnit was larger than largestUnit');
let duration = ES.NormalizeDurationWith24HourDays(this);
({ duration } = ES.RoundTimeDuration(duration, roundingIncrement, smallestUnit, roundingMode));
duration = ES.RoundTimeDuration(duration, roundingIncrement, smallestUnit, roundingMode);
return ES.UnnormalizeDuration(duration, largestUnit);
}
total(totalOf) {
Expand All @@ -320,18 +319,7 @@ export class Duration {
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const relativeEpochNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
const targetEpochNs = ES.AddZonedDateTime(relativeEpochNs, timeZone, calendar, duration);
const { total } = ES.DifferenceZonedDateTimeWithRounding(
relativeEpochNs,
targetEpochNs,
timeZone,
calendar,
unit,
1,
unit,
'trunc'
);
assert(!NumberIsNaN(total), 'total went through NudgeToZonedTime code path');
return total;
return ES.DifferenceZonedDateTimeWithTotal(relativeEpochNs, targetEpochNs, timeZone, calendar, unit);
}

if (plainRelativeTo) {
Expand All @@ -344,7 +332,7 @@ export class Duration {
const dateDuration = ES.AdjustDateDurationRecord(duration.date, targetTime.deltaDays);
const targetDate = ES.CalendarDateAdd(calendar, isoRelativeToDate, dateDuration, 'constrain');

const { total } = ES.DifferencePlainDateTimeWithRounding(
return ES.DifferencePlainDateTimeWithTotal(
isoRelativeToDate.year,
isoRelativeToDate.month,
isoRelativeToDate.day,
Expand All @@ -364,13 +352,8 @@ export class Duration {
targetTime.microsecond,
targetTime.nanosecond,
calendar,
unit,
1,
unit,
'trunc'
unit
);
assert(!NumberIsNaN(total), 'total went through NudgeToZonedTime code path');
return total;
}

// No reference date to calculate difference relative to
Expand All @@ -382,8 +365,7 @@ export class Duration {
throw new RangeErrorCtor(`a starting point is required for ${unit}s total`);
}
const duration = ES.NormalizeDurationWith24HourDays(this);
const { total } = ES.RoundTimeDuration(duration, 1, unit, 'trunc');
return total;
return ES.TotalTimeDuration(duration.norm, unit);
}
toString(options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeErrorCtor('invalid receiver');
Expand All @@ -400,7 +382,7 @@ export class Duration {

const largestUnit = ES.DefaultTemporalLargestUnit(this);
let duration = ES.NormalizeDuration(this);
({ duration } = ES.RoundTimeDuration(duration, increment, unit, roundingMode));
duration = ES.RoundTimeDuration(duration, increment, unit, roundingMode);
const roundedDuration = ES.UnnormalizeDuration(duration, ES.LargerOfTwoTemporalUnits(largestUnit, 'second'));
return ES.TemporalDurationToString(roundedDuration, precision);
}
Expand Down
Loading

0 comments on commit ae6edf0

Please sign in to comment.