Skip to content

Commit

Permalink
Refactor Instant.add() and subtract() into abstract operation
Browse files Browse the repository at this point in the history
This operation will be reused in ZonedDateTime.add() and subtract() as
well.

See: #569
  • Loading branch information
ptomato authored and Ms2ger committed Nov 3, 2020
1 parent 77bd78c commit 72a02b8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
13 changes: 13 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,19 @@ export const ES = ObjectAssign({}, ES2020, {
ES.RejectDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds };
},
AddInstant: (epochNanoseconds, h, min, s, ms, µs, ns) => {
let sum = bigInt(0);
sum = sum.plus(bigInt(ns));
sum = sum.plus(bigInt(µs).multiply(1e3));
sum = sum.plus(bigInt(ms).multiply(1e6));
sum = sum.plus(bigInt(s).multiply(1e9));
sum = sum.plus(bigInt(min).multiply(60 * 1e9));
sum = sum.plus(bigInt(h).multiply(60 * 60 * 1e9));

const result = bigInt(epochNanoseconds).plus(sum);
ES.RejectInstantRange(result);
return result;
},
RoundNumberToIncrement: (quantity, increment, mode) => {
const quotient = quantity / increment;
let round;
Expand Down
42 changes: 18 additions & 24 deletions polyfill/lib/instant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,15 @@ export class Instant {
nanoseconds
} = ES.ToLimitedTemporalDuration(temporalDurationLike, ['years', 'months', 'weeks', 'days']);
ES.RejectDurationSign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);

let sum = bigInt(0);
sum = sum.plus(bigInt(nanoseconds));
sum = sum.plus(bigInt(microseconds).multiply(1e3));
sum = sum.plus(bigInt(milliseconds).multiply(1e6));
sum = sum.plus(bigInt(seconds).multiply(1e9));
sum = sum.plus(bigInt(minutes).multiply(60 * 1e9));
sum = sum.plus(bigInt(hours).multiply(60 * 60 * 1e9));

const ns = bigInt(GetSlot(this, EPOCHNANOSECONDS)).plus(sum);
ES.RejectInstantRange(ns);

const ns = ES.AddInstant(
GetSlot(this, EPOCHNANOSECONDS),
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
);
const Construct = ES.SpeciesConstructor(this, Instant);
const result = new Construct(bigIntIfAvailable(ns));
if (!ES.IsTemporalInstant(result)) throw new TypeError('invalid result');
Expand All @@ -86,18 +83,15 @@ export class Instant {
nanoseconds
} = ES.ToLimitedTemporalDuration(temporalDurationLike, ['years', 'months', 'weeks', 'days']);
ES.RejectDurationSign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);

let sum = bigInt(0);
sum = sum.plus(bigInt(nanoseconds));
sum = sum.plus(bigInt(microseconds).multiply(1e3));
sum = sum.plus(bigInt(milliseconds).multiply(1e6));
sum = sum.plus(bigInt(seconds).multiply(1e9));
sum = sum.plus(bigInt(minutes).multiply(60 * 1e9));
sum = sum.plus(bigInt(hours).multiply(60 * 60 * 1e9));

const ns = bigInt(GetSlot(this, EPOCHNANOSECONDS)).minus(sum);
ES.RejectInstantRange(ns);

const ns = ES.AddInstant(
GetSlot(this, EPOCHNANOSECONDS),
-hours,
-minutes,
-seconds,
-milliseconds,
-microseconds,
-nanoseconds
);
const Construct = ES.SpeciesConstructor(this, Instant);
const result = new Construct(bigIntIfAvailable(ns));
if (!ES.IsTemporalInstant(result)) throw new TypeError('invalid result');
Expand Down
35 changes: 19 additions & 16 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,7 @@ <h1>Temporal.Instant.prototype.add ( _temporalDurationLike_ )</h1>
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _duration_ be ? ToLimitedTemporalDuration(_temporalDurationLike_, « *"years"*, *"months"*, *"weeks"*, *"days"* »).
1. Perform ? RejectDurationSign(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _ns_ be _instant_.[[Nanoseconds]] +
_duration_.[[Nanoseconds]] +
_duration_.[[Microseconds]] × 1000 +
_duration_.[[Milliseconds]] × 1,000,000 +
_duration_.[[Seconds]] × 1,000,000,000 +
_duration_.[[Minutes]] × 60,000,000,000 +
_duration_.[[Hours]] × 3,600,000,000,000.
1. Perform ? RejectInstant(_ns_).
1. Let _ns_ be ! AddInstant(_instant_.[[EpochNanoseconds]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Return ? CreateTemporalInstantFromInstance(_instant_, _ns_).
</emu-alg>
</emu-clause>
Expand All @@ -264,14 +257,7 @@ <h1>Temporal.Instant.prototype.subtract ( _temporalDurationLike_ )</h1>
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _duration_ be ? ToLimitedTemporalDuration(_temporalDurationLike_, « *"years"*, *"months"*, *"weeks"*, *"days"* »).
1. Perform ? RejectDurationSign(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _ns_ be _instant_.[[Nanoseconds]] −
_duration_.[[Nanoseconds]] −
_duration_.[[Microseconds]] × 1000 −
_duration_.[[Milliseconds]] × 1,000,000 −
_duration_.[[Seconds]] × 1,000,000,000 −
_duration_.[[Minutes]] × 60,000,000,000 −
_duration_.[[Hours]] × 3,600,000,000,000.
1. Perform ? RejectInstant(_ns_).
1. Let _ns_ be ! AddInstant(_instant_.[[EpochNanoseconds]], −_duration_.[[Hours]], −_duration_.[[Minutes]], −_duration_.[[Seconds]], −_duration_.[[Milliseconds]], −_duration_.[[Microseconds]], −_duration_.[[Nanoseconds]]).
1. Return ? CreateTemporalInstantFromInstance(_instant_, _ns_).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -632,6 +618,23 @@ <h1>CompareEpochNanoseconds ( _epochNanosecondsOne_, _epochNanosecondsTwo_ )</h1
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-addinstant" aoid="AddInstant">
<h1>AddInstant ( _epochNanoseconds_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_ )</h1>
<p>
The abstract operation AddInstant adds a duration in various time units to a number of nanoseconds since the Unix epoch.
</p>
<emu-alg>
1. Let _result_ be _epochNanoseconds_ + _nanoseconds_ +
_microseconds_ × 1000 +
_milliseconds_ × 1,000,000 +
_seconds_ × 1,000,000,000 +
_minutes_ × 60,000,000,000 +
_hours_ × 3,600,000,000,000.
1. Perform ? RejectInstant(_result_).
1. Return _result_.
</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 72a02b8

Please sign in to comment.