Skip to content

Commit

Permalink
Create DifferenceInstant abstract operation in spec
Browse files Browse the repository at this point in the history
This operation will be reused in ZonedDateTime.until() and since().

See: #569
  • Loading branch information
ptomato authored and Ms2ger committed Nov 10, 2020
1 parent 116256a commit b735a9d
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,7 @@ <h1>Temporal.Instant.prototype.until ( _other_ [ , _options_ ] )</h1>
1. Let _roundingMode_ be ? ToTemporalRoundingMode(_options_, *"trunc"*).
1. Let _maximum_ be ! MaximumTemporalDurationRoundingIncrement(_smallestUnit_).
1. Let _roundingIncrement_ be the mathematical value of ? ToTemporalRoundingIncrement(_options_, _maximum_, *false*).
1. If _smallestUnit_ is *"hours"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 3.6 × 10<sup>12</sup>.
1. If _smallestUnit_ is *"minutes"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 6 × 10<sup>10</sup>.
1. Else if _smallestUnit_ is *"seconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>9</sup>.
1. Else if _smallestUnit_ is *"milliseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>6</sup>.
1. Else if _smallestUnit_ is *"microseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>3</sup>.
1. Else,
1. Assert: _smallestUnit_ is *"nanoseconds"*.
1. Let _incrementNs_ be _roundingIncrement_.
1. Let _diff_ be the mathematical value of _otherInstant_.[[Nanoseconds]] − _instant_.[[Nanoseconds]].
1. Let _roundedNs_ be ? RoundNumberToIncrement(_diff_, _incrementNs_, _roundingMode_).
1. Let _roundedNs_ be ? DifferenceInstant(_instant_.[[Nanoseconds]], _other_.[[Nanoseconds]], _roundingIncrement_, _smallestUnit_, _roundingMode_).
1. Let _result_ be ! BalanceDuration(0, 0, 0, 0, 0, 0, _roundedNs_, _largestUnit_).
1. Return ? CreateTemporalDuration(0, 0, 0, 0, _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
</emu-alg>
Expand All @@ -318,21 +304,7 @@ <h1>Temporal.Instant.prototype.since ( _other_ [ , _options_ ] )</h1>
1. Let _roundingMode_ be ? ToTemporalRoundingMode(_options_, *"trunc"*).
1. Let _maximum_ be ! MaximumTemporalDurationRoundingIncrement(_smallestUnit_).
1. Let _roundingIncrement_ be the mathematical value of ? ToTemporalRoundingIncrement(_options_, _maximum_, *false*).
1. If _smallestUnit_ is *"hours"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 3.6 × 10<sup>12</sup>.
1. If _smallestUnit_ is *"minutes"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 6 × 10<sup>10</sup>.
1. Else if _smallestUnit_ is *"seconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>9</sup>.
1. Else if _smallestUnit_ is *"milliseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>6</sup>.
1. Else if _smallestUnit_ is *"microseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>3</sup>.
1. Else,
1. Assert: _smallestUnit_ is *"nanoseconds"*.
1. Let _incrementNs_ be _roundingIncrement_.
1. Let _diff_ be the mathematical value of _instant_.[[Nanoseconds]] − _otherInstant_.[[Nanoseconds]].
1. Let _roundedNs_ be ? RoundNumberToIncrement(_diff_, _incrementNs_, _roundingMode_).
1. Let _roundedNs_ be ? DifferenceInstant(_other_.[[Nanoseconds]], _instant_.[[Nanoseconds]], _roundingIncrement_, _smallestUnit_, _roundingMode_).
1. Let _result_ be ! BalanceDuration(0, 0, 0, 0, 0, 0, _roundedNs_, _largestUnit_).
1. Return ? CreateTemporalDuration(0, 0, 0, 0, _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
</emu-alg>
Expand Down Expand Up @@ -642,6 +614,30 @@ <h1>AddInstant ( _epochNanoseconds_, _hours_, _minutes_, _seconds_, _millisecond
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-differenceinstant" aoid="DifferenceInstant">
<h1>DifferenceInstant ( _ns1_, _ns2_, _roundingIncrement_, _smallestUnit_, _roundingMode_ )</h1>
<p>
The abstract operation DifferenceInstant computes the difference between two exact times expressed in nanoseconds since the Unix epoch, and rounds the result according to the given parameters.
</p>
<emu-alg>
1. If _smallestUnit_ is *"hours"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 3.6 × 10<sup>12</sup>.
1. If _smallestUnit_ is *"minutes"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 6 × 10<sup>10</sup>.
1. Else if _smallestUnit_ is *"seconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>9</sup>.
1. Else if _smallestUnit_ is *"milliseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>6</sup>.
1. Else if _smallestUnit_ is *"microseconds"*, then
1. Let _incrementNs_ be _roundingIncrement_ × 10<sup>3</sup>.
1. Else,
1. Assert: _smallestUnit_ is *"nanoseconds"*.
1. Let _incrementNs_ be _roundingIncrement_.
1. Let _diff_ be _ns2__ns1_.
1. Return ? RoundNumberToIncrement(_diff_, _incrementNs_, _roundingMode_).
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-roundtemporalinstant" aoid="RoundTemporalInstant">
<h1>RoundTemporalInstant ( _ns_, _increment_, _unit_, _roundingMode_ )</h1>
<p>
Expand Down

0 comments on commit b735a9d

Please sign in to comment.