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: Remove duplicate steps in UnbalanceDateDurationRelative #2690

Merged
merged 5 commits into from
Jan 31, 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
18 changes: 9 additions & 9 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3445,14 +3445,18 @@ export function BalanceTimeDurationRelative(
export function UnbalanceDateDurationRelative(years, months, weeks, days, largestUnit, plainRelativeTo, calendarRec) {
// calendarRec must have looked up dateAdd and dateUntil
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
const defaultLargestUnit = DefaultTemporalLargestUnit(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
const effectiveLargestUnit = LargerOfTwoTemporalUnits(largestUnit, 'day');
if (LargerOfTwoTemporalUnits(defaultLargestUnit, effectiveLargestUnit) === effectiveLargestUnit) {
// no-op
return { years, months, weeks, days };
}
if (!calendarRec) throw new RangeError(`a starting point is required for ${largestUnit}s balancing`);

switch (largestUnit) {
switch (effectiveLargestUnit) {
case 'year':
// no-op
return { years, months, weeks, days };
throw new Error('assert not reached');
case 'month': {
if (years === 0) return { years: 0, months, weeks, days };
if (!calendarRec) throw new RangeError('a starting point is required for months balancing');
// balance years down to months
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years));
const untilOptions = ObjectCreate(null);
Expand All @@ -3462,17 +3466,13 @@ export function UnbalanceDateDurationRelative(years, months, weeks, days, larges
return { years: 0, months: months + yearsInMonths, weeks, days };
}
case 'week': {
if (years === 0 && months === 0) return { years: 0, months: 0, weeks, days };
if (!calendarRec) throw new RangeError('a starting point is required for weeks balancing');
// balance years and months down to days
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years, months));
const yearsMonthsInDays = DaysUntil(plainRelativeTo, later);
return { years: 0, months: 0, weeks, days: days + yearsMonthsInDays };
}
default: {
// largestUnit is "day", or any time unit
if (years === 0 && months === 0 && weeks === 0) return { years: 0, months: 0, weeks: 0, days };
if (!calendarRec) throw new RangeError('a starting point is required for balancing calendar units');
// balance years, months, and weeks down to days
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years, months, weeks));
const yearsMonthsWeeksInDays = DaysUntil(plainRelativeTo, later);
Expand Down
28 changes: 11 additions & 17 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -1739,13 +1739,15 @@ <h1>
</dl>
<emu-alg>
1. Assert: If _plainRelativeTo_ is not *undefined*, _calendarRec_ is not *undefined*.
1. If _largestUnit_ is *"year"*, then
1. Let _defaultLargestUnit_ be DefaultTemporalLargestUnit(_years_, _months_, _weeks_, _days_, 0, 0, 0, 0, 0).
1. Let _effectiveLargestUnit_ be LargerOfTwoTemporalUnits(_largestUnit_, *"day"*).
1. If _effectiveLargestUnit_ is LargerOfTwoTemporalUnits(_defaultLargestUnit_, _effectiveLargestUnit_), then
1. Return ! CreateDateDurationRecord(_years_, _months_, _weeks_, _days_).
1. If _largestUnit_ is *"month"*, then
1. If _years_ = 0, return ! CreateDateDurationRecord(0, _months_, _weeks_, _days_).
1. If _calendarRec_ is *undefined*, then
1. Throw a *RangeError* exception.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-add~) is *true*.
1. Assert: _effectiveLargestUnit_ is not *"year"*.
1. If _calendarRec_ is *undefined*, then
1. Throw a *RangeError* exception.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-add~) is *true*.
1. If _effectiveLargestUnit_ is *"month"*, then
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-until~) is *true*.
1. Let _yearsDuration_ be ! CreateTemporalDuration(_years_, 0, 0, 0, 0, 0, 0, 0, 0, 0).
1. Let _later_ be ? CalendarDateAdd(_calendarRec_, _plainRelativeTo_, _yearsDuration_).
Expand All @@ -1754,24 +1756,16 @@ <h1>
1. Let _untilResult_ be ? CalendarDateUntil(_calendarRec_, _plainRelativeTo_, _later_, _untilOptions_).
1. Let _yearsInMonths_ be _untilResult_.[[Months]].
1. Return ? CreateDateDurationRecord(0, _months_ + _yearsInMonths_, _weeks_, _days_).
1. If _largestUnit_ is *"week"*, then
1. If _years_ = 0 and _months_ = 0, return ! CreateDateDurationRecord(0, 0, _weeks_, _days_).
1. If _calendarRec_ is *undefined*, then
1. Throw a *RangeError* exception.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-add~) is *true*.
1. If _effectiveLargestUnit_ is *"week"*, then
1. Let _yearsMonthsDuration_ be ! CreateTemporalDuration(_years_, _months_, 0, 0, 0, 0, 0, 0, 0, 0).
1. Let _later_ be ? CalendarDateAdd(_calendarRec_, _plainRelativeTo_, _yearsMonthsDuration_).
1. Let _yearsMonthsInDays_ be DaysUntil(_plainRelativeTo_, _later_).
1. Return ? CreateDateDurationRecord(0, 0, _weeks_, _days_ + _yearsMonthsInDays_).
1. Return ! CreateDateDurationRecord(0, 0, _weeks_, _days_ + _yearsMonthsInDays_).
1. NOTE: _largestUnit_ can be any time unit as well as *"day"*.
1. If _years_ = 0, and _months_ = 0, and _weeks_ = 0, return ! CreateDateDurationRecord(0, 0, 0, _days_).
1. If _calendarRec_ is *undefined*, then
1. Throw a *RangeError* exception.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-add~) is *true*.
1. Let _yearsMonthsWeeksDuration_ be ! CreateTemporalDuration(_years_, _months_, _weeks_, 0, 0, 0, 0, 0, 0, 0).
1. Let _later_ be ? CalendarDateAdd(_calendarRec_, _plainRelativeTo_, _yearsMonthsWeeksDuration_).
1. Let _yearsMonthsWeeksInDays_ be DaysUntil(_plainRelativeTo_, _later_).
1. Return ? CreateDateDurationRecord(0, 0, 0, _days_ + _yearsMonthsWeeksInDays_).
1. Return ! CreateDateDurationRecord(0, 0, 0, _days_ + _yearsMonthsWeeksInDays_).
</emu-alg>
</emu-clause>

Expand Down
Loading