Skip to content

Commit

Permalink
Normative: Reject an ambiguous time string even with a calendar
Browse files Browse the repository at this point in the history
Fixes #2285
  • Loading branch information
gibson042 authored and ptomato committed Aug 16, 2022
1 parent 5a84124 commit 5e2afb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 10 additions & 5 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ObjectDefineProperty = Object.defineProperty;
const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const ObjectIs = Object.is;
const ObjectEntries = Object.entries;
const StringPrototypeSlice = String.prototype.slice;

import bigInt from 'big-integer';
import Call from 'es-abstract/2020/Call.js';
Expand Down Expand Up @@ -394,15 +395,19 @@ export const ES = ObjectAssign({}, ES2020, {
if (/[tT ][0-9][0-9]/.test(isoString)) {
return { hour, minute, second, millisecond, microsecond, nanosecond, calendar };
}
// slow but non-grammar-dependent way to ensure that time-only strings that
// are also valid PlainMonthDay and PlainYearMonth throw. corresponds to
// assertion in spec text
// Reject strings that are ambiguous with PlainMonthDay or PlainYearMonth.
// The calendar suffix is `[u-ca=${calendar}]`, i.e. calendar plus 7 characters,
// and must be stripped so presence of a calendar doesn't result in interpretation
// of otherwise ambiguous input as a time.
const isoStringWithoutCalendar = calendar
? StringPrototypeSlice.call(isoString, 0, isoString.length - calendar.length - 7)
: isoString;
try {
const { month, day } = ES.ParseTemporalMonthDayString(isoString);
const { month, day } = ES.ParseTemporalMonthDayString(isoStringWithoutCalendar);
ES.RejectISODate(1972, month, day);
} catch {
try {
const { year, month } = ES.ParseTemporalYearMonthString(isoString);
const { year, month } = ES.ParseTemporalYearMonthString(isoStringWithoutCalendar);
ES.RejectISODate(year, month, 1);
} catch {
return { hour, minute, second, millisecond, microsecond, nanosecond, calendar };
Expand Down
3 changes: 1 addition & 2 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,7 @@ <h1>ISO 8601 grammar</h1>

CalendarTime :
TimeDesignator TimeSpec TimeZone? Calendar?
TimeSpec TimeZone? Calendar
TimeSpecWithOptionalTimeZoneNotAmbiguous
TimeSpecWithOptionalTimeZoneNotAmbiguous Calendar?

CalendarDateTime:
DateTime Calendar?
Expand Down

0 comments on commit 5e2afb9

Please sign in to comment.