Skip to content

Commit

Permalink
Editorial: Use Time Record in RoundTime
Browse files Browse the repository at this point in the history
Instead of passing each component individually.

See: #2949
  • Loading branch information
ptomato committed Oct 7, 2024
1 parent 46faf71 commit 8d093f0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
9 changes: 7 additions & 2 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4457,12 +4457,17 @@ export function RoundISODateTime(
unit,
roundingMode
) {
const time = RoundTime(hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode);
const time = RoundTime({ hour, minute, second, millisecond, microsecond, nanosecond }, increment, unit, roundingMode);
const isoDate = BalanceISODate(year, month, day + time.deltaDays);
return CombineISODateAndTimeRecord(isoDate, time);
}

export function RoundTime(hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode) {
export function RoundTime(
{ hour, minute, second, millisecond, microsecond, nanosecond },
increment,
unit,
roundingMode
) {
let quantity;
switch (unit) {
case 'day':
Expand Down
38 changes: 18 additions & 20 deletions polyfill/lib/plaintime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,19 @@ export class PlainTime {
};
ES.ValidateTemporalRoundingIncrement(roundingIncrement, MAX_INCREMENTS[smallestUnit], false);

let hour = GetSlot(this, ISO_HOUR);
let minute = GetSlot(this, ISO_MINUTE);
let second = GetSlot(this, ISO_SECOND);
let millisecond = GetSlot(this, ISO_MILLISECOND);
let microsecond = GetSlot(this, ISO_MICROSECOND);
let nanosecond = GetSlot(this, ISO_NANOSECOND);
({ hour, minute, second, millisecond, microsecond, nanosecond } = ES.RoundTime(
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
const { hour, minute, second, millisecond, microsecond, nanosecond } = ES.RoundTime(
{
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)
},
roundingIncrement,
smallestUnit,
roundingMode
));
);

return new PlainTime(hour, minute, second, millisecond, microsecond, nanosecond);
}
Expand All @@ -191,12 +187,14 @@ export class PlainTime {
if (smallestUnit === 'hour') throw new RangeErrorCtor('smallestUnit must be a time unit other than "hour"');
const { precision, unit, increment } = ES.ToSecondsStringPrecisionRecord(smallestUnit, digits);
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),
{
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)
},
increment,
unit,
roundingMode
Expand Down
3 changes: 2 additions & 1 deletion spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,8 @@ <h1>
</dl>
<emu-alg>
1. Assert: ISODateTimeWithinLimits(_isoDateTime_) is *true*.
1. Let _roundedTime_ be RoundTime(_isoDateTime_.[[Hour]], _isoDateTime_.[[Minute]], _isoDateTime_.[[Second]], _isoDateTime_.[[Millisecond]], _isoDateTime_.[[Microsecond]], _isoDateTime_.[[Nanosecond]], _increment_, _unit_, _roundingMode_).
1. Let _time_ be Time Record { [[Hour]]: _isoDateTime_.[[Hour]], [[Minute]]: _isoDateTime_.[[Minute]], [[Second]]: _isoDateTime_.[[Second]], [[Millisecond]]: _isoDateTime_.[[Millisecond]], [[Microsecond]]: _isoDateTime_.[[Microsecond]], [[Nanosecond]]: _isoDateTime_.[[Nanosecond]] }.
1. Let _roundedTime_ be RoundTime(_time_, _increment_, _unit_, _roundingMode_).
1. Let _balanceResult_ be BalanceISODate(_isoDateTime_.[[Year]], _isoDateTime_.[[Month]], _isoDateTime_.[[Day]] + _roundedTime_.[[Days]]).
1. Return CombineISODateAndTimeRecord(_balanceResult_, _roundedTime_).
</emu-alg>
Expand Down
35 changes: 16 additions & 19 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ <h1>Temporal.PlainTime.prototype.round ( _roundTo_ )</h1>
1. Let _maximum_ be MaximumTemporalDurationRoundingIncrement(_smallestUnit_).
1. Assert: _maximum_ is not ~unset~.
1. Perform ? ValidateTemporalRoundingIncrement(_roundingIncrement_, _maximum_, *false*).
1. Let _result_ be RoundTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _roundingIncrement_, _smallestUnit_, _roundingMode_).
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. Let _result_ be RoundTime(_time_, _roundingIncrement_, _smallestUnit_, _roundingMode_).
1. Return ! CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -312,7 +313,8 @@ <h1>Temporal.PlainTime.prototype.toString ( [ _options_ ] )</h1>
1. Let _smallestUnit_ be ? GetTemporalUnitValuedOption(_resolvedOptions_, *"smallestUnit"*, ~time~, ~unset~).
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. Let _time_ be Time Record { [[Hour]]: _temporalTime_.[[ISOHour]], [[Minute]]: _temporalTime_.[[ISOMinute]], [[Second]]: _temporalTime_.[[ISOSecond]], [[Millisecond]]: _temporalTime_.[[ISOMillisecond]], [[Microsecond]]: _temporalTime_.[[ISOMicrosecond]], [[Nanosecond]]: _temporalTime_.[[ISONanosecond]] }.
1. Let _roundResult_ be RoundTime(_time_, _precision_.[[Increment]], _precision_.[[Unit]], _roundingMode_).
1. Return TimeRecordToString(_roundResult_, _precision_.[[Precision]]).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -900,12 +902,7 @@ <h1>
<emu-clause id="sec-temporal-roundtime" type="abstract operation">
<h1>
RoundTime (
_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,
_time_: a Time Record,
_increment_: a positive integer,
_unit_: a time unit or ~day~,
_roundingMode_: a rounding mode,
Expand All @@ -917,18 +914,18 @@ <h1>
</dl>
<emu-alg>
1. If _unit_ is ~day~ or ~hour~, then
1. Let _quantity_ be ((((_hour_ × 60 + _minute_) × 60 + _second_) × 1000 + _millisecond_) × 1000 + _microsecond_) × 1000 + _nanosecond_.
1. Let _quantity_ be ((((_time_.[[Hour]] × 60 + _time_.[[Minute]]) × 60 + _time_.[[Second]]) × 1000 + _time_.[[Millisecond]]) × 1000 + _time_.[[Microsecond]]) × 1000 + _time_.[[Nanosecond]].
1. Else if _unit_ is ~minute~, then
1. Let _quantity_ be (((_minute_ × 60 + _second_) × 1000 + _millisecond_) × 1000 + _microsecond_) × 1000 + _nanosecond_.
1. Let _quantity_ be (((_time_.[[Minute]] × 60 + _time_.[[Second]]) × 1000 + _time_.[[Millisecond]]) × 1000 + _time_.[[Microsecond]]) × 1000 + _time_.[[Nanosecond]].
1. Else if _unit_ is ~second~, then
1. Let _quantity_ be ((_second_ × 1000 + _millisecond_) × 1000 + _microsecond_) × 1000 + _nanosecond_.
1. Let _quantity_ be ((_time_.[[Second]] × 1000 + _time_.[[Millisecond]]) × 1000 + _time_.[[Microsecond]]) × 1000 + _time_.[[Nanosecond]].
1. Else if _unit_ is ~millisecond~, then
1. Let _quantity_ be (_millisecond_ × 1000 + _microsecond_) × 1000 + _nanosecond_.
1. Let _quantity_ be (_time_.[[Millisecond]] × 1000 + _time_.[[Microsecond]]) × 1000 + _time_.[[Nanosecond]].
1. Else if _unit_ is ~microsecond~, then
1. Let _quantity_ be _microsecond_ × 1000 + _nanosecond_.
1. Let _quantity_ be _time_.[[Microsecond]] × 1000 + _time_.[[Nanosecond]].
1. Else,
1. Assert: _unit_ is ~nanosecond~.
1. Let _quantity_ be _nanosecond_.
1. Let _quantity_ be _time_.[[Nanosecond]].
1. Let _unitLength_ be the value in the "Length in Nanoseconds" column of the row of <emu-xref href="#table-temporal-units"></emu-xref> whose "Value" column contains _unit_.
1. Let _result_ be RoundNumberToIncrement(_quantity_, _increment_ × _unitLength_, _roundingMode_) / _unitLength_.
1. If _unit_ is ~day~, then
Expand All @@ -944,15 +941,15 @@ <h1>
1. If _unit_ is ~hour~, then
1. Return BalanceTime(_result_, 0, 0, 0, 0, 0).
1. If _unit_ is ~minute~, then
1. Return BalanceTime(_hour_, _result_, 0, 0, 0, 0).
1. Return BalanceTime(_time_.[[Hour]], _result_, 0, 0, 0, 0).
1. If _unit_ is ~second~, then
1. Return BalanceTime(_hour_, _minute_, _result_, 0, 0, 0).
1. Return BalanceTime(_time_.[[Hour]], _time_.[[Minute]], _result_, 0, 0, 0).
1. If _unit_ is ~millisecond~, then
1. Return BalanceTime(_hour_, _minute_, _second_, _result_, 0, 0).
1. Return BalanceTime(_time_.[[Hour]], _time_.[[Minute]], _time_.[[Second]], _result_, 0, 0).
1. If _unit_ is ~microsecond~, then
1. Return BalanceTime(_hour_, _minute_, _second_, _millisecond_, _result_, 0).
1. Return BalanceTime(_time_.[[Hour]], _time_.[[Minute]], _time_.[[Second]], _time_.[[Millisecond]], _result_, 0).
1. Assert: _unit_ is ~nanosecond~.
1. Return BalanceTime(_hour_, _minute_, _second_, _millisecond_, _microsecond_, _result_).
1. Return BalanceTime(_time_.[[Hour]], _time_.[[Minute]], _time_.[[Second]], _time_.[[Millisecond]], _time_.[[Microsecond]], _result_).
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit 8d093f0

Please sign in to comment.