Skip to content

Commit

Permalink
Editorial: Use Time Record in TemporalTimeToString, and rename
Browse files Browse the repository at this point in the history
Instead of passing each component individually, pass a Time Record. The
name is no longer really appropriate, since we are not passing a Temporal
object, so rename it to TimeRecordToString.

See: #2949
  • Loading branch information
ptomato committed Oct 7, 2024
1 parent a607493 commit a8c0664
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
5 changes: 5 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,11 @@ export function TemporalDateToString(date, showCalendar = 'auto') {
return `${year}-${month}-${day}${calendar}`;
}

export function TimeRecordToString({ hour, minute, second, millisecond, microsecond, nanosecond }, precision) {
const subSecondNanoseconds = millisecond * 1e6 + microsecond * 1e3 + nanosecond;
return FormatTimeString(hour, minute, second, subSecondNanoseconds, precision);
}

export function TemporalDateTimeToString(isoDateTime, calendar, precision, showCalendar = 'auto') {
let { year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = isoDateTime;
const yearString = ISOYearString(year);
Expand Down
60 changes: 30 additions & 30 deletions polyfill/lib/plaintime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,6 @@ import {
SetSlot
} from './slots.mjs';

function TemporalTimeToString(time, precision, options = undefined) {
let hour = GetSlot(time, ISO_HOUR);
let minute = GetSlot(time, ISO_MINUTE);
let second = GetSlot(time, ISO_SECOND);
let millisecond = GetSlot(time, ISO_MILLISECOND);
let microsecond = GetSlot(time, ISO_MICROSECOND);
let nanosecond = GetSlot(time, ISO_NANOSECOND);

if (options) {
const { unit, increment, roundingMode } = options;
({ hour, minute, second, millisecond, microsecond, nanosecond } = ES.RoundTime(
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
increment,
unit,
roundingMode
));
}

const subSecondNanoseconds = millisecond * 1e6 + microsecond * 1e3 + nanosecond;
return ES.FormatTimeString(hour, minute, second, subSecondNanoseconds, precision);
}

export class PlainTime {
constructor(isoHour = 0, isoMinute = 0, isoSecond = 0, isoMillisecond = 0, isoMicrosecond = 0, isoNanosecond = 0) {
isoHour = isoHour === undefined ? 0 : ES.ToIntegerWithTruncation(isoHour);
Expand All @@ -75,8 +48,16 @@ export class PlainTime {
SetSlot(this, ISO_NANOSECOND, isoNanosecond);

if (typeof __debug__ !== 'undefined' && __debug__) {
const time = {
hour: isoHour,
minute: isoMinute,
second: isoSecond,
millisecond: isoMillisecond,
microsecond: isoMicrosecond,
nanosecond: isoNanosecond
};
ObjectDefineProperty(this, '_repr_', {
value: `${this[SymbolToStringTag]} <${TemporalTimeToString(this, 'auto')}>`,
value: `${this[SymbolToStringTag]} <${ES.TimeRecordToString(time, 'auto')}>`,
writable: false,
enumerable: false,
configurable: false
Expand Down Expand Up @@ -209,11 +190,30 @@ export class PlainTime {
const smallestUnit = ES.GetTemporalUnitValuedOption(resolvedOptions, 'smallestUnit', 'time', undefined);
if (smallestUnit === 'hour') throw new RangeErrorCtor('smallestUnit must be a time unit other than "hour"');
const { precision, unit, increment } = ES.ToSecondsStringPrecisionRecord(smallestUnit, digits);
return TemporalTimeToString(this, precision, { unit, increment, roundingMode });
const time = ES.RoundTime(
GetSlot(this, ISO_HOUR),
GetSlot(this, ISO_MINUTE),
GetSlot(this, ISO_SECOND),
GetSlot(this, ISO_MILLISECOND),
GetSlot(this, ISO_MICROSECOND),
GetSlot(this, ISO_NANOSECOND),
increment,
unit,
roundingMode
);
return ES.TimeRecordToString(time, precision, { unit, increment, roundingMode });
}
toJSON() {
if (!ES.IsTemporalTime(this)) throw new TypeErrorCtor('invalid receiver');
return TemporalTimeToString(this, 'auto');
const time = {
hour: GetSlot(this, ISO_HOUR),
minute: GetSlot(this, ISO_MINUTE),
second: GetSlot(this, ISO_SECOND),
millisecond: GetSlot(this, ISO_MILLISECOND),
microsecond: GetSlot(this, ISO_MICROSECOND),
nanosecond: GetSlot(this, ISO_NANOSECOND)
};
return ES.TimeRecordToString(time, 'auto');
}
toLocaleString(locales = undefined, options = undefined) {
if (!ES.IsTemporalTime(this)) throw new TypeErrorCtor('invalid receiver');
Expand Down
23 changes: 10 additions & 13 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ <h1>Temporal.PlainTime.prototype.toString ( [ _options_ ] )</h1>
1. If _smallestUnit_ is ~hour~, throw a *RangeError* exception.
1. Let _precision_ be ToSecondsStringPrecisionRecord(_smallestUnit_, _digits_).
1. Let _roundResult_ be RoundTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _precision_.[[Increment]], _precision_.[[Unit]], _roundingMode_).
1. Return TemporalTimeToString(_roundResult_.[[Hour]], _roundResult_.[[Minute]], _roundResult_.[[Second]], _roundResult_.[[Millisecond]], _roundResult_.[[Microsecond]], _roundResult_.[[Nanosecond]], _precision_.[[Precision]]).
1. Return TimeRecordToString(_roundResult_, _precision_.[[Precision]]).
</emu-alg>
</emu-clause>

Expand All @@ -326,7 +326,8 @@ <h1>Temporal.PlainTime.prototype.toLocaleString ( [ _locales_ [ , _options_ ] ]
<emu-alg>
1. Let _temporalTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_temporalTime_, [[InitializedTemporalTime]]).
1. Return TemporalTimeToString(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], ~auto~).
1. Let _time_ be Time Record { [[Hour]]: _temporalTime_.[[ISOHour]], [[Minute]]: _temporalTime_.[[ISOMinute]], [[Second]]: _temporalTime_.[[ISOSecond]], [[Millisecond]]: _temporalTime_.[[ISOMillisecond]], [[Microsecond]]: _temporalTime_.[[ISOMicrosecond]], [[Nanosecond]]: _temporalTime_.[[ISONanosecond]] }.
1. Return TimeRecordToString(_time_, ~auto~).
</emu-alg>
</emu-clause>

Expand All @@ -336,7 +337,8 @@ <h1>Temporal.PlainTime.prototype.toJSON ( )</h1>
<emu-alg>
1. Let _temporalTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_temporalTime_, [[InitializedTemporalTime]]).
1. Return TemporalTimeToString(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], ~auto~).
1. Let _time_ be Time Record { [[Hour]]: _temporalTime_.[[ISOHour]], [[Minute]]: _temporalTime_.[[ISOMinute]], [[Second]]: _temporalTime_.[[ISOSecond]], [[Millisecond]]: _temporalTime_.[[ISOMillisecond]], [[Microsecond]]: _temporalTime_.[[ISOMicrosecond]], [[Nanosecond]]: _temporalTime_.[[ISONanosecond]] }.
1. Return TimeRecordToString(_time_, ~auto~).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -830,15 +832,10 @@ <h1>
</emu-table>
</emu-clause>

<emu-clause id="sec-temporal-temporaltimetostring" type="abstract operation">
<emu-clause id="sec-temporal-timerecordtostring" type="abstract operation">
<h1>
TemporalTimeToString (
_hour_: an integer in the inclusive interval from 0 to 23,
_minute_: an integer in the inclusive interval from 0 to 59,
_second_: an integer in the inclusive interval from 0 to 59,
_millisecond_: an integer in the inclusive interval from 0 to 999,
_microsecond_: an integer in the inclusive interval from 0 to 999,
_nanosecond_: an integer in the inclusive interval from 0 to 999,
TimeRecordToString (
_time_: a Time Record
_precision_: an integer in the inclusive interval from 0 to 9, ~minute~, or ~auto~,
): a String
</h1>
Expand All @@ -847,8 +844,8 @@ <h1>
<dd>It formats the given time as an ISO 8601 string, to the precision specified by _precision_.</dd>
</dl>
<emu-alg>
1. Let _subSecondNanoseconds_ be _millisecond_ × 10<sup>6</sup> + _microsecond_ × 10<sup>3</sup> + _nanosecond_.
1. Return FormatTimeString(_hour_, _minute_, _second_, _subSecondNanoseconds_, _precision_).
1. Let _subSecondNanoseconds_ be _time_.[[Millisecond]] × 10<sup>6</sup> + _time_.[[Microsecond]] × 10<sup>3</sup> + _time_.[[Nanosecond]].
1. Return FormatTimeString(_time_.[[Hour]], _time_.[[Minute]], _time_.[[Second]], _subSecondNanoseconds_, _precision_).
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit a8c0664

Please sign in to comment.