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

Editorial: Split total() code path out of RoundRelativeDuration #2987

Merged
merged 1 commit into from
Oct 1, 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
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
Loading