Skip to content

Commit af87527

Browse files
gibson04212wrigja
authored andcommitted
Reject an ambiguous time string even with a calendar
UPSTREAM_COMMIT=5e2afb9ef1427e775a7ccd4ec2e3371adfb0e1ee
1 parent 72bce78 commit af87527

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/ecmascript.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const NumberIsNaN = Number.isNaN;
1111
const NumberIsFinite = Number.isFinite;
1212
const NumberCtor = Number;
1313
const StringCtor = String;
14+
const StringPrototypeSlice = String.prototype.slice;
1415
const NumberMaxSafeInteger = Number.MAX_SAFE_INTEGER;
1516
const ObjectAssign = Object.assign;
1617
const ObjectCreate = Object.create;
@@ -477,15 +478,19 @@ export function ParseTemporalTimeString(isoString: string) {
477478
if (/[tT ][0-9][0-9]/.test(isoString)) {
478479
return { hour, minute, second, millisecond, microsecond, nanosecond, calendar };
479480
}
480-
// slow but non-grammar-dependent way to ensure that time-only strings that
481-
// are also valid PlainMonthDay and PlainYearMonth throw. corresponds to
482-
// assertion in spec text
481+
// Reject strings that are ambiguous with PlainMonthDay or PlainYearMonth.
482+
// The calendar suffix is `[u-ca=${calendar}]`, i.e. calendar plus 7 characters,
483+
// and must be stripped so presence of a calendar doesn't result in interpretation
484+
// of otherwise ambiguous input as a time.
485+
const isoStringWithoutCalendar = calendar
486+
? StringPrototypeSlice.call(isoString, 0, isoString.length - calendar.length - 7)
487+
: isoString;
483488
try {
484-
const { month, day } = ParseTemporalMonthDayString(isoString);
489+
const { month, day } = ParseTemporalMonthDayString(isoStringWithoutCalendar);
485490
RejectISODate(1972, month, day);
486491
} catch {
487492
try {
488-
const { year, month } = ParseTemporalYearMonthString(isoString);
493+
const { year, month } = ParseTemporalYearMonthString(isoStringWithoutCalendar);
489494
RejectISODate(year, month, 1);
490495
} catch {
491496
return { hour, minute, second, millisecond, microsecond, nanosecond, calendar };

0 commit comments

Comments
 (0)