Skip to content

Commit

Permalink
Avoid formatting irrelevant parts of dateStyle/timeStyle for Temporal…
Browse files Browse the repository at this point in the history
… types

timeStyle: 'long' and timeStyle: 'full' include the time zone name for
legacy Date formatting. However, PlainTime and PlainDateTime don't include
a time zone in their data model, so they shouldn't format a time zone name
when using timeStyle. Similarly, PlainYearMonth and PlainMonthDay
shouldn't format a day or year respectively when using dateStyle. This was
already the consensus but somewhere along the line the spec text got out
of sync, probably in a rebase on latest ECMA-402.

In the polyfill, we already handled dateStyle for PlainYearMonth and
PlainMonthDay. Try to hack around this the same way for timeStyle.

Closes: #2795
  • Loading branch information
ptomato committed Oct 4, 2024
1 parent 7f3344e commit ae39bca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ function timeAmend(originalOptions) {
timeZoneName: false,
dateStyle: false
});
if (options.timeStyle === 'long' || options.timeStyle === 'full') {
// Try to fake what timeStyle should do if not printing the time zone name
delete options.timeStyle;
ObjectAssign(options, { hour: 'numeric', minute: '2-digit', second: '2-digit' });
}
if (!hasTimeOptions(options)) {
if (hasAnyDateTimeOptions(originalOptions)) {
throw new TypeError(`cannot format Temporal.PlainTime with options [${ObjectKeys(originalOptions)}]`);
Expand Down Expand Up @@ -431,6 +436,11 @@ function dateAmend(originalOptions) {

function datetimeAmend(originalOptions) {
const options = amend(originalOptions, { timeZoneName: false });
if (options.timeStyle === 'long' || options.timeStyle === 'full') {
// Try to fake what timeStyle should do if not printing the time zone name
delete options.timeStyle;
ObjectAssign(options, { hour: 'numeric', minute: '2-digit', second: '2-digit' });
}
if (!hasTimeOptions(options) && !hasDateOptions(options)) {
if (hasAnyDateTimeOptions(originalOptions)) {
throw new TypeError(`cannot format PlainDateTime with options [${ObjectKeys(originalOptions)}]`);
Expand Down
8 changes: 4 additions & 4 deletions spec/intl.html
Original file line number Diff line number Diff line change
Expand Up @@ -701,17 +701,17 @@ <h1>
1. Let _bestFormat_ be DateTimeStyleFormat(_dateStyle_, _timeStyle_, _styles_).
1. <ins>If _dateStyle_ is not *undefined*, then</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainDateFormat]] to _bestFormat_.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainYearMonthFormat]] to _bestFormat_.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainMonthDayFormat]] to _bestFormat_.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainYearMonthFormat]] to GetDateTimeFormat(_styles_, _formatMatcher_, _bestFormat_, ~year-month~, ~year-month~, ~relevant~).</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainMonthDayFormat]] to GetDateTimeFormat(_styles_, _formatMatcher_, _bestFormat_, ~month-day~, ~month-day~, ~relevant~).</ins>
1. <ins>Else,</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainDateFormat]] to *null*.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainYearMonthFormat]] to *null*.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainMonthDayFormat]] to *null*.</ins>
1. <ins>If _timeStyle_ is not *undefined*, then</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainTimeFormat]] to _bestFormat_.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainTimeFormat]] to GetDateTimeFormat(_styles_, _formatMatcher_, _bestFormat_, ~time~, ~time~, ~relevant~).</ins>
1. <ins>Else,</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainTimeFormat]] to *null*.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainDateTimeFormat]] to _bestFormat_.</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalPlainDateTimeFormat]] to GetDateTimeFormat(_styles_, _formatMatcher_, _bestFormat_, ~any~, ~all~, ~relevant~).</ins>
1. <ins>Set _dateTimeFormat_.[[TemporalInstantFormat]] to _bestFormat_.</ins>
1. Else,
1. <del>Let _needDefaults_ be *true*.</del>
Expand Down

0 comments on commit ae39bca

Please sign in to comment.