Skip to content

Commit

Permalink
Normative: Read date-time options only once when creating DateTimeFor…
Browse files Browse the repository at this point in the history
…mat objects

This commits inlines the default values processing and conflicting
date-time styles checks from `ToDateTimeOptions` into
`CreateDateTimeFormat`.

Fixes #237
Fixes #706
  • Loading branch information
anba authored and ryzokuken committed Jul 21, 2023
1 parent 6e3b70d commit 02bd03a
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions spec/datetimeformat.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1>ChainDateTimeFormat ( _dateTimeFormat_, _newTarget_, _this_ )</h1>
</emu-normative-optional>
</emu-clause>

<emu-clause id="sec-createdatetimeformat" type="abstract operation" oldids="sec-initializedatetimeformat">
<emu-clause id="sec-createdatetimeformat" type="abstract operation" oldids="sec-initializedatetimeformat,sec-todatetimeoptions">
<h1>
CreateDateTimeFormat (
_newTarget_: a constructor,
Expand All @@ -54,9 +54,7 @@ <h1>
<emu-alg>
1. Let _dateTimeFormat_ be ? OrdinaryCreateFromConstructor(_newTarget_, *"%DateTimeFormat.prototype%"*, &laquo; [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[FractionalSecondDigits]], [[TimeZoneName]], [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[Pattern]], [[RangePatterns]], [[BoundFormat]] &raquo;).
1. Let _requestedLocales_ be ? CanonicalizeLocaleList(_locales_).
1. If _required_ is not *"any"* or _defaults_ is not *"date"*, then
1. Set _options_ to ? ToDateTimeOptions(_options_, _required_, _defaults_).
1. Set _options_ to ? ToDateTimeOptions(_options_, *"any"*, *"date"*).
1. Set _options_ to ? CoerceOptionsToObject(_options_).
1. Let _opt_ be a new Record.
1. Let _matcher_ be ? GetOption(_options_, *"localeMatcher"*, ~string~, &laquo; *"lookup"*, *"best fit"* &raquo;, *"best fit"*).
1. Set _opt_.[[localeMatcher]] to _matcher_.
Expand Down Expand Up @@ -121,9 +119,28 @@ <h1>
1. If _dateStyle_ is not *undefined* or _timeStyle_ is not *undefined*, then
1. If _hasExplicitFormatComponents_ is *true*, then
1. Throw a *TypeError* exception.
1. If _required_ is *"date"* and _timeStyle_ is not *undefined*, then
1. Throw a *TypeError* exception.
1. If _required_ is *"time"* and _dateStyle_ is not *undefined*, then
1. Throw a *TypeError* exception.
1. Let _styles_ be _dataLocaleData_.[[styles]].[[&lt;_resolvedCalendar_&gt;]].
1. Let _bestFormat_ be DateTimeStyleFormat(_dateStyle_, _timeStyle_, _styles_).
1. Else,
1. Let _needDefaults_ be *true*.
1. If _required_ is *"date"* or *"any"*, then
1. For each property name _prop_ of &laquo; *"weekday"*, *"year"*, *"month"*, *"day"* &raquo;, do
1. Let _value_ be _formatOptions_.[[&lt;_prop_&gt;]].
1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
1. If _required_ is *"time"* or *"any"*, then
1. For each property name _prop_ of &laquo; *"dayPeriod"*, *"hour"*, *"minute"*, *"second"*, *"fractionalSecondDigits"* &raquo;, do
1. Let _value_ be _formatOptions_.[[&lt;_prop_&gt;]].
1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
1. If _needDefaults_ is *true* and _defaults_ is either *"date"* or *"all"*, then
1. For each property name _prop_ of &laquo; *"year"*, *"month"*, *"day"* &raquo;, do
1. Set _formatOptions_.[[&lt;_prop_&gt;]] to *"numeric"*.
1. If _needDefaults_ is *true* and _defaults_ is either *"time"* or *"all"*, then
1. For each property name _prop_ of &laquo; *"hour"*, *"minute"*, *"second"* &raquo;, do
1. Set _formatOptions_.[[&lt;_prop_&gt;]] to *"numeric"*.
1. Let _formats_ be _dataLocaleData_.[[formats]].[[&lt;_resolvedCalendar_&gt;]].
1. If _matcher_ is *"basic"*, then
1. Let _bestFormat_ be BasicFormatMatcher(_formatOptions_, _formats_).
Expand Down Expand Up @@ -751,42 +768,6 @@ <h1>Abstract Operations for DateTimeFormat Objects</h1>
</table>
</emu-table>

<emu-clause id="sec-todatetimeoptions" aoid="ToDateTimeOptions">
<h1>ToDateTimeOptions ( _options_, _required_, _defaults_ )</h1>

<p>
When the ToDateTimeOptions abstract operation is called with arguments _options_, _required_, and _defaults_, the following steps are taken:
</p>

<emu-alg>
1. If _options_ is *undefined*, let _options_ be *null*; otherwise let _options_ be ? ToObject(_options_).
1. Let _options_ be OrdinaryObjectCreate(_options_).
1. Let _needDefaults_ be *true*.
1. If _required_ is *"date"* or *"any"*, then
1. For each property name _prop_ of &laquo; *"weekday"*, *"year"*, *"month"*, *"day"* &raquo;, do
1. Let _value_ be ? Get(_options_, _prop_).
1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
1. If _required_ is *"time"* or *"any"*, then
1. For each property name _prop_ of &laquo; *"dayPeriod"*, *"hour"*, *"minute"*, *"second"*, *"fractionalSecondDigits"* &raquo;, do
1. Let _value_ be ? Get(_options_, _prop_).
1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
1. Let _dateStyle_ be ? Get(_options_, *"dateStyle"*).
1. Let _timeStyle_ be ? Get(_options_, *"timeStyle"*).
1. If _dateStyle_ is not *undefined* or _timeStyle_ is not *undefined*, let _needDefaults_ be *false*.
1. If _required_ is *"date"* and _timeStyle_ is not *undefined*, then
1. Throw a *TypeError* exception.
1. If _required_ is *"time"* and _dateStyle_ is not *undefined*, then
1. Throw a *TypeError* exception.
1. If _needDefaults_ is *true* and _defaults_ is either *"date"* or *"all"*, then
1. For each property name _prop_ of &laquo; *"year"*, *"month"*, *"day"* &raquo;, do
1. Perform ? CreateDataPropertyOrThrow(_options_, _prop_, *"numeric"*).
1. If _needDefaults_ is *true* and _defaults_ is either *"time"* or *"all"*, then
1. For each property name _prop_ of &laquo; *"hour"*, *"minute"*, *"second"* &raquo;, do
1. Perform ? CreateDataPropertyOrThrow(_options_, _prop_, *"numeric"*).
1. Return _options_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-date-time-style-format" aoid="DateTimeStyleFormat">
<h1>DateTimeStyleFormat ( _dateStyle_, _timeStyle_, _styles_ )</h1>
<p>The DateTimeStyleFormat abstract operation accepts arguments _dateStyle_ and _timeStyle_, which are each either *undefined*, *"full"*, *"long"*, *"medium"*, or *"short"*, at least one of which is not *undefined*, and _styles_, which is a record from %DateTimeFormat%.[[LocaleData]].[[&lt;_locale_&gt;]].[[styles]].[[&lt;_calendar_&gt;]] for some locale _locale_ and calendar _calendar_. It returns the appropriate format record for date time formatting based on the parameters.</p>
Expand Down

0 comments on commit 02bd03a

Please sign in to comment.