From 4f3a9e6361f3f686dd8b8cbc96da519afe05790c Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 16 Jun 2023 15:35:38 +0200 Subject: [PATCH] Editorial: Change Duration.prototype.total to work more like the polyfill While investigating integer math, this discrepancy has been unhelpful. If I had to choose one of the two ways of implementing it, I'd choose this one anyway, because it's more concise and deals with the quantity that is actually returned to the user. --- spec/duration.html | 55 +++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/spec/duration.html b/spec/duration.html index 0332b5cfb0..caa1abab0d 100644 --- a/spec/duration.html +++ b/spec/duration.html @@ -543,29 +543,7 @@

Temporal.Duration.prototype.total ( _totalOf_ )

1. If _balanceResult_ is ~negative overflow~, return *-∞*𝔽. 1. Assert: _balanceResult_ is a Time Duration Record. 1. Let _roundRecord_ be ? RoundDuration(_unbalanceResult_.[[Years]], _unbalanceResult_.[[Months]], _unbalanceResult_.[[Weeks]], _balanceResult_.[[Days]], _balanceResult_.[[Hours]], _balanceResult_.[[Minutes]], _balanceResult_.[[Seconds]], _balanceResult_.[[Milliseconds]], _balanceResult_.[[Microseconds]], _balanceResult_.[[Nanoseconds]], 1, _unit_, *"trunc"*, _plainRelativeTo_, _calendarRec_, _zonedRelativeTo_, _timeZoneRec_). - 1. Let _roundResult_ be _roundRecord_.[[DurationRecord]]. - 1. If _unit_ is *"year"*, then - 1. Let _whole_ be _roundResult_.[[Years]]. - 1. Else if _unit_ is *"month"*, then - 1. Let _whole_ be _roundResult_.[[Months]]. - 1. Else if _unit_ is *"week"*, then - 1. Let _whole_ be _roundResult_.[[Weeks]]. - 1. Else if _unit_ is *"day"*, then - 1. Let _whole_ be _roundResult_.[[Days]]. - 1. Else if _unit_ is *"hour"*, then - 1. Let _whole_ be _roundResult_.[[Hours]]. - 1. Else if _unit_ is *"minute"*, then - 1. Let _whole_ be _roundResult_.[[Minutes]]. - 1. Else if _unit_ is *"second"*, then - 1. Let _whole_ be _roundResult_.[[Seconds]]. - 1. Else if _unit_ is *"millisecond"*, then - 1. Let _whole_ be _roundResult_.[[Milliseconds]]. - 1. Else if _unit_ is *"microsecond"*, then - 1. Let _whole_ be _roundResult_.[[Microseconds]]. - 1. Else, - 1. Assert: _unit_ is *"nanosecond"*. - 1. Let _whole_ be _roundResult_.[[Nanoseconds]]. - 1. Return 𝔽(_whole_ + _roundRecord_.[[Remainder]]). + 1. Return 𝔽(_roundRecord_.[[Total]]). @@ -1667,7 +1645,11 @@

description
-
It rounds a duration (denoted by _years_ through _nanoseconds_) according to the rounding parameters _unit_, _increment_, and _roundingMode_, and returns a Record with the Duration Record result in its [[DurationRecord]] field, as well as the remainder from the rounding operation in its [[Remainder]] field. For rounding involving calendar units, the _plainRelativeTo_ parameter is required.
+
+ It rounds a duration (denoted by _years_ through _nanoseconds_) according to the rounding parameters _unit_, _increment_, and _roundingMode_, and returns a Record with the Duration Record result in its [[DurationRecord]] field. + It also returns the total of the smallest unit before the rounding operation in its [[Total]] field, for use in `Temporal.Duration.prototype.total`. + For rounding involving calendar units, the _plainRelativeTo_ parameter is required. +
1. Assert: If either of _plainRelativeTo_ or _zonedRelativeTo_ are present and not *undefined*, _calendarRec_ is not *undefined*. @@ -1693,7 +1675,7 @@

1. Else, 1. Let _fractionalSeconds_ be _nanoseconds_ × 10-9 + _microseconds_ × 10-6 + _milliseconds_ × 10-3 + _seconds_. 1. Assert: _fractionalDays_ is not used below. - 1. Let _remainder_ be *undefined*. + 1. Let _total_ be ~unset~. 1. If _unit_ is *"year"*, then 1. Let _yearsDuration_ be ! CreateTemporalDuration(_years_, 0, 0, 0, 0, 0, 0, 0, 0, 0). 1. Let _yearsLater_ be ? AddDate(_calendarRec_, _plainRelativeTo_, _yearsDuration_). @@ -1720,7 +1702,7 @@

1. Let _oneYearDays_ be _moveResult_.[[Days]]. 1. Let _fractionalYears_ be _years_ + _fractionalDays_ / abs(_oneYearDays_). 1. Set _years_ to RoundNumberToIncrement(_fractionalYears_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalYears_ - _years_. + 1. Set _total_ to _fractionalYears_. 1. Set _months_ and _weeks_ to 0. 1. Else if _unit_ is *"month"*, then 1. Let _yearsMonths_ be ! CreateTemporalDuration(_years_, _months_, 0, 0, 0, 0, 0, 0, 0, 0). @@ -1748,7 +1730,7 @@

1. Let _oneMonthDays_ be _moveResult_.[[Days]]. 1. Let _fractionalMonths_ be _months_ + _fractionalDays_ / abs(_oneMonthDays_). 1. Set _months_ to RoundNumberToIncrement(_fractionalMonths_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalMonths_ - _months_. + 1. Set _total_ to _fractionalMonths_. 1. Set _weeks_ to 0. 1. Else if _unit_ is *"week"*, then 1. Let _isoResult_ be ! AddISODate(_plainRelativeTo_.[[ISOYear]], _plainRelativeTo_.[[ISOMonth]], _plainRelativeTo_.[[ISODay]], 0, 0, 0, truncate(_fractionalDays_), *"constrain"*). @@ -1769,43 +1751,42 @@

1. Let _oneWeekDays_ be _moveResult_.[[Days]]. 1. Let _fractionalWeeks_ be _weeks_ + _fractionalDays_ / abs(_oneWeekDays_). 1. Set _weeks_ to RoundNumberToIncrement(_fractionalWeeks_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalWeeks_ - _weeks_. + 1. Set _total_ to _fractionalWeeks_. 1. Else if _unit_ is *"day"*, then 1. Set _days_ to RoundNumberToIncrement(_fractionalDays_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalDays_ - _days_. + 1. Set _total_ to _fractionalDays_. 1. Else if _unit_ is *"hour"*, then 1. Let _fractionalHours_ be (_fractionalSeconds_ / 60 + _minutes_) / 60 + _hours_. 1. Set _hours_ to RoundNumberToIncrement(_fractionalHours_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalHours_ - _hours_. + 1. Set _total_ to _fractionalHours_. 1. Set _minutes_, _seconds_, _milliseconds_, _microseconds_, and _nanoseconds_ to 0. 1. Else if _unit_ is *"minute"*, then 1. Let _fractionalMinutes_ be _fractionalSeconds_ / 60 + _minutes_. 1. Set _minutes_ to RoundNumberToIncrement(_fractionalMinutes_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalMinutes_ - _minutes_. + 1. Set _total_ to _fractionalMinutes_. 1. Set _seconds_, _milliseconds_, _microseconds_, and _nanoseconds_ to 0. 1. Else if _unit_ is *"second"*, then 1. Set _seconds_ to RoundNumberToIncrement(_fractionalSeconds_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalSeconds_ - _seconds_. + 1. Set _total_ to _fractionalSeconds_. 1. Set _milliseconds_, _microseconds_, and _nanoseconds_ to 0. 1. Else if _unit_ is *"millisecond"*, then 1. Let _fractionalMilliseconds_ be _nanoseconds_ × 10-6 + _microseconds_ × 10-3 + _milliseconds_. 1. Set _milliseconds_ to RoundNumberToIncrement(_fractionalMilliseconds_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalMilliseconds_ - _milliseconds_. + 1. Set _total_ to _fractionalMilliseconds_. 1. Set _microseconds_ and _nanoseconds_ to 0. 1. Else if _unit_ is *"microsecond"*, then 1. Let _fractionalMicroseconds_ be _nanoseconds_ × 10-3 + _microseconds_. 1. Set _microseconds_ to RoundNumberToIncrement(_fractionalMicroseconds_, _increment_, _roundingMode_). - 1. Set _remainder_ to _fractionalMicroseconds_ - _microseconds_. + 1. Set _total_ to _fractionalMicroseconds_. 1. Set _nanoseconds_ to 0. 1. Else, 1. Assert: _unit_ is *"nanosecond"*. - 1. Set _remainder_ to _nanoseconds_. + 1. Set _total_ to _nanoseconds_. 1. Set _nanoseconds_ to RoundNumberToIncrement(_nanoseconds_, _increment_, _roundingMode_). - 1. Set _remainder_ to _remainder_ - _nanoseconds_. 1. Let _duration_ be ? CreateDurationRecord(_years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_). 1. Return the Record { [[DurationRecord]]: _duration_, - [[Remainder]]: _remainder_ + [[Total]]: _total_ }.