Skip to content

Commit f5eab0a

Browse files
committed
Editorial: Remove Duration Records
These are no longer necessary. We should either be using Normalized Duration Records (or their subcomponents, Date Duration Records and Normalized Time Duration Records) or Temporal.Duration instances. The records were still used in a few places: ToTemporalDurationRecord can be inlined into ToTemporalDuration since that is its only caller, and ParseTemporalDurationString can just return a Temporal.Duration instance directly.
1 parent 1d7e8c7 commit f5eab0a

File tree

3 files changed

+113
-164
lines changed

3 files changed

+113
-164
lines changed

polyfill/lib/ecmascript.mjs

+47-58
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ export function ParseTemporalTimeZoneString(stringIdent) {
641641
throw new ErrorCtor('this line should not be reached');
642642
}
643643

644-
export function ParseTemporalDurationString(isoString) {
644+
function ParseTemporalDurationString(isoString) {
645645
const match = Call(RegExpPrototypeExec, PARSE.duration, [isoString]);
646646
if (!match) throw new RangeErrorCtor(`invalid duration: ${isoString}`);
647647
if (Call(ArrayPrototypeEvery, match, [(part, i) => i < 2 || part === undefined])) {
@@ -690,8 +690,19 @@ export function ParseTemporalDurationString(isoString) {
690690
seconds += MathTrunc(excessNanoseconds / 1e9) % 60;
691691
minutes += MathTrunc(excessNanoseconds / 60e9);
692692

693-
RejectDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
694-
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds };
693+
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
694+
return new TemporalDuration(
695+
years,
696+
months,
697+
weeks,
698+
days,
699+
hours,
700+
minutes,
701+
seconds,
702+
milliseconds,
703+
microseconds,
704+
nanoseconds
705+
);
695706
}
696707

697708
export function RegulateISODate(year, month, day, overflow) {
@@ -738,49 +749,6 @@ export function RegulateISOYearMonth(year, month, overflow) {
738749
return { year, month };
739750
}
740751

741-
export function ToTemporalDurationRecord(item) {
742-
if (Type(item) !== 'Object') {
743-
return ParseTemporalDurationString(RequireString(item));
744-
}
745-
if (IsTemporalDuration(item)) {
746-
return {
747-
years: GetSlot(item, YEARS),
748-
months: GetSlot(item, MONTHS),
749-
weeks: GetSlot(item, WEEKS),
750-
days: GetSlot(item, DAYS),
751-
hours: GetSlot(item, HOURS),
752-
minutes: GetSlot(item, MINUTES),
753-
seconds: GetSlot(item, SECONDS),
754-
milliseconds: GetSlot(item, MILLISECONDS),
755-
microseconds: GetSlot(item, MICROSECONDS),
756-
nanoseconds: GetSlot(item, NANOSECONDS)
757-
};
758-
}
759-
const result = {
760-
years: 0,
761-
months: 0,
762-
weeks: 0,
763-
days: 0,
764-
hours: 0,
765-
minutes: 0,
766-
seconds: 0,
767-
milliseconds: 0,
768-
microseconds: 0,
769-
nanoseconds: 0
770-
};
771-
let partial = ToTemporalPartialDurationRecord(item);
772-
for (let index = 0; index < DURATION_FIELDS.length; index++) {
773-
const property = DURATION_FIELDS[index];
774-
const value = partial[property];
775-
if (value !== undefined) {
776-
result[property] = value;
777-
}
778-
}
779-
let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = result;
780-
RejectDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
781-
return result;
782-
}
783-
784752
export function ToTemporalPartialDurationRecord(temporalDurationLike) {
785753
if (Type(temporalDurationLike) !== 'Object') {
786754
throw new TypeErrorCtor('invalid duration-like');
@@ -1312,20 +1280,41 @@ export function ToTemporalDateTime(item, options = undefined) {
13121280

13131281
export function ToTemporalDuration(item) {
13141282
if (IsTemporalDuration(item)) return item;
1315-
let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
1316-
ToTemporalDurationRecord(item);
1283+
if (Type(item) !== 'Object') {
1284+
return ParseTemporalDurationString(RequireString(item));
1285+
}
1286+
const result = {
1287+
years: 0,
1288+
months: 0,
1289+
weeks: 0,
1290+
days: 0,
1291+
hours: 0,
1292+
minutes: 0,
1293+
seconds: 0,
1294+
milliseconds: 0,
1295+
microseconds: 0,
1296+
nanoseconds: 0
1297+
};
1298+
let partial = ToTemporalPartialDurationRecord(item);
1299+
for (let index = 0; index < DURATION_FIELDS.length; index++) {
1300+
const property = DURATION_FIELDS[index];
1301+
const value = partial[property];
1302+
if (value !== undefined) {
1303+
result[property] = value;
1304+
}
1305+
}
13171306
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
13181307
return new TemporalDuration(
1319-
years,
1320-
months,
1321-
weeks,
1322-
days,
1323-
hours,
1324-
minutes,
1325-
seconds,
1326-
milliseconds,
1327-
microseconds,
1328-
nanoseconds
1308+
result.years,
1309+
result.months,
1310+
result.weeks,
1311+
result.days,
1312+
result.hours,
1313+
result.minutes,
1314+
result.seconds,
1315+
result.milliseconds,
1316+
result.microseconds,
1317+
result.nanoseconds
13291318
);
13301319
}
13311320

spec/abstractops.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ <h1>
17471747
<h1>
17481748
ParseTemporalDurationString (
17491749
_isoString_: a String,
1750-
): either a normal completion containing a Duration Record or a throw completion
1750+
): either a normal completion containing a Temporal.Duration or a throw completion
17511751
</h1>
17521752
<dl class="header">
17531753
<dt>description</dt>
@@ -1843,8 +1843,7 @@ <h1>
18431843
1. Set _millisecondsMV_ to floor(_millisecondsMV_) × _factor_.
18441844
1. Set _microsecondsMV_ to floor(_microsecondsMV_) × _factor_.
18451845
1. Set _nanosecondsMV_ to floor(_nanosecondsMV_) × _factor_.
1846-
1. If IsValidDuration(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_) is *false*, throw a *RangeError* exception.
1847-
1. Return CreateDurationRecord(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_).
1846+
1. Return ? CreateTemporalDuration(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_).
18481847
</emu-alg>
18491848
</emu-clause>
18501849

0 commit comments

Comments
 (0)