Skip to content

Commit

Permalink
Editorial: Parameterize UTCOffset grammar
Browse files Browse the repository at this point in the history
Instead of two separate productions UTCOffsetSubMinutePrecision and
UTCOffsetMinutePrecision, we can have one UTCOffset production with a
SubMinutePrecision parameter.
  • Loading branch information
ptomato committed Apr 30, 2024
1 parent 2099d24 commit 67fb2fd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
30 changes: 17 additions & 13 deletions polyfill/test/validStrings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,28 @@ const timeFraction = withCode(temporalDecimalFraction, (data, result) => {
data.microsecond = +fraction.slice(3, 6);
data.nanosecond = +fraction.slice(6, 9);
});

function saveOffset(data, result) {
data.offset = ES.ParseDateTimeUTCOffset(result);
}

const utcOffsetWithSubMinuteComponents = (extended) =>
seq(temporalSign, hour, timeSeparator(extended), minuteSecond, timeSeparator(extended), minuteSecond, [
temporalDecimalFraction
const utcOffset = (subMinutePrecision) =>
seq(temporalSign, hour, [
choice(
seq(
timeSeparator(true),
minuteSecond,
subMinutePrecision ? [timeSeparator(true), minuteSecond, [temporalDecimalFraction]] : empty
),
seq(
timeSeparator(false),
minuteSecond,
subMinutePrecision ? [timeSeparator(false), minuteSecond, [temporalDecimalFraction]] : empty
)
)
]);
const utcOffsetMinutePrecision = seq(temporalSign, hour, [
choice(seq(timeSeparator(true), minuteSecond), seq(timeSeparator(false), minuteSecond))
]);
const utcOffsetSubMinutePrecision = withCode(
choice(utcOffsetMinutePrecision, utcOffsetWithSubMinuteComponents(true), utcOffsetWithSubMinuteComponents(false)),
saveOffset
);
const dateTimeUTCOffset = (plain) =>
plain ? utcOffsetSubMinutePrecision : choice(utcDesignator, utcOffsetSubMinutePrecision);
const timeZoneUTCOffsetName = utcOffsetMinutePrecision;
plain ? withCode(utcOffset(true), saveOffset) : choice(utcDesignator, withCode(utcOffset(true), saveOffset));
const timeZoneUTCOffsetName = utcOffset(false);
const timeZoneIANAName = choice(...timezoneNames);
const timeZoneIdentifier = withCode(
choice(timeZoneUTCOffsetName, timeZoneIANAName),
Expand Down
20 changes: 7 additions & 13 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -1283,28 +1283,22 @@ <h1>ISO 8601 grammar</h1>
TimeFraction :::
TemporalDecimalFraction

UTCOffsetWithSubMinuteComponents[Extended] :::
TemporalSign Hour TimeSeparator[?Extended] MinuteSecond TimeSeparator[?Extended] MinuteSecond TemporalDecimalFraction?

NormalizedUTCOffset :::
ASCIISign Hour TimeSeparator[+Extended] MinuteSecond

UTCOffsetMinutePrecision :::
UTCOffset[SubMinutePrecision] :::
TemporalSign Hour
TemporalSign Hour TimeSeparator[+Extended] MinuteSecond
TemporalSign Hour TimeSeparator[~Extended] MinuteSecond

UTCOffsetSubMinutePrecision :::
UTCOffsetMinutePrecision
UTCOffsetWithSubMinuteComponents[+Extended]
UTCOffsetWithSubMinuteComponents[~Extended]
[+SubMinutePrecision] TemporalSign Hour TimeSeparator[+Extended] MinuteSecond TimeSeparator[+Extended] MinuteSecond TemporalDecimalFraction?
[+SubMinutePrecision] TemporalSign Hour TimeSeparator[~Extended] MinuteSecond TimeSeparator[~Extended] MinuteSecond TemporalDecimalFraction?

DateTimeUTCOffset[Plain] :::
[~Plain] UTCDesignator
UTCOffsetSubMinutePrecision
UTCOffset[+SubMinutePrecision]

TimeZoneUTCOffsetName :::
UTCOffsetMinutePrecision
UTCOffset[~SubMinutePrecision]

TZLeadingChar :::
Alpha
Expand Down Expand Up @@ -1635,8 +1629,8 @@ <h1>
1. Set _timeZoneResult_.[[TimeZoneAnnotation]] to CodePointsToString(_identifier_).
1. If _parseResult_ contains a |UTCDesignator| Parse Node, then
1. Set _timeZoneResult_.[[Z]] to *true*.
1. Else if _parseResult_ contains a |UTCOffsetSubMinutePrecision| Parse Node, then
1. Let _offset_ be the source text matched by the |UTCOffsetSubMinutePrecision| Parse Node contained within _parseResult_.
1. Else if _parseResult_ contains a |UTCOffset[+SubMinutePrecision]| Parse Node, then
1. Let _offset_ be the source text matched by the |UTCOffset[+SubMinutePrecision]| Parse Node contained within _parseResult_.
1. Set _timeZoneResult_.[[OffsetString]] to CodePointsToString(_offset_).
1. Let _calendar_ be *undefined*.
1. Let _calendarWasCritical_ be *false*.
Expand Down
2 changes: 1 addition & 1 deletion spec/intl.html
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ <h1>
1. <ins>If _toLocaleStringTimeZone_ is present, throw a *TypeError* exception.</ins>
1. Set _timeZone_ to ? ToString(_timeZone_).
1. If IsTimeZoneOffsetString(_timeZone_) is *true*, then
1. Let _parseResult_ be ParseText(StringToCodePoints(_timeZone_), <del>|UTCOffset|</del><ins>|UTCOffsetMinutePrecision|</ins>).
1. Let _parseResult_ be ParseText(StringToCodePoints(_timeZone_), <del>|UTCOffset|</del><ins>|UTCOffset[~SubMinutePrecision]|</ins>).
1. Assert: _parseResult_ is a Parse Node.
1. <del>If _parseResult_ contains more than one |MinuteSecond| Parse Node, throw a *RangeError* exception.</del>
1. Let _offsetNanoseconds_ be <del>ParseTimeZoneOffsetString</del><ins>? ParseDateTimeUTCOffset</ins>(_timeZone_).
Expand Down
6 changes: 3 additions & 3 deletions spec/mainadditions.html
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ <h1>Time Zone Offset String <del>Format</del><ins>Formats</ins></h1>
<ins class="block">
<p>
ECMAScript defines string interchange formats for UTC offsets, derived from ISO 8601.
UTC offsets that represent offset time zone identifiers, or that are intended for interoperability with ISO 8601, use only hours and minutes and are specified by |UTCOffsetMinutePrecision|.
UTC offsets that represent the offset of a named or custom time zone can be more precise, and are specified by |UTCOffsetSubMinutePrecision|.
UTC offsets that represent offset time zone identifiers, or that are intended for interoperability with ISO 8601, use only hours and minutes and are specified by |UTCOffset[~SubMinutePrecision]|.
UTC offsets that represent the offset of a named or custom time zone can be more precise, and are specified by |UTCOffset[+SubMinutePrecision]|.
</p>
<p>
These formats are described by the ISO String grammar in <emu-xref href="#sec-temporal-iso8601grammar"></emu-xref>.
Expand Down Expand Up @@ -519,7 +519,7 @@ <h1>
</dd>
</dl>
<emu-alg>
1. Let _parseResult_ be ParseText(StringToCodePoints(_offsetString_), <del>|UTCOffset|</del><ins>|UTCOffsetSubMinutePrecision|</ins>).
1. Let _parseResult_ be ParseText(StringToCodePoints(_offsetString_), <del>|UTCOffset|</del><ins>|UTCOffset[+SubMinutePrecision]|</ins>).
1. <del>Assert: _parseResult_ is not a List of errors.</del>
1. <ins>If _parseResult_ is a List of errors, throw a *RangeError* exception.</ins>
1. Assert: _parseResult_ contains a |TemporalSign| Parse Node.
Expand Down

0 comments on commit 67fb2fd

Please sign in to comment.