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 Jun 13, 2024
1 parent 3d53cf6 commit c3eb4cf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
31 changes: 18 additions & 13 deletions polyfill/test/validStrings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +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 = (z) => (z ? choice(utcDesignator, utcOffsetSubMinutePrecision) : utcOffsetSubMinutePrecision);
const timeZoneUTCOffsetName = utcOffsetMinutePrecision;
const dateTimeUTCOffset = (z) =>
z ? choice(utcDesignator, withCode(utcOffset(true), saveOffset)) : 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 @@ -1278,28 +1278,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[Z] :::
[+Z] UTCDesignator
UTCOffsetSubMinutePrecision
UTCOffset[+SubMinutePrecision]

TimeZoneUTCOffsetName :::
UTCOffsetMinutePrecision
UTCOffset[~SubMinutePrecision]

TZLeadingChar :::
Alpha
Expand Down Expand Up @@ -1631,8 +1625,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. Return the Record {
[[Year]]: _yearMV_,
Expand Down
2 changes: 1 addition & 1 deletion spec/intl.html
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,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 @@ -252,8 +252,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 @@ -461,7 +461,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 c3eb4cf

Please sign in to comment.