Skip to content

Commit

Permalink
Editorial: Avoid dividing by zero in ZonedDateTime#round.
Browse files Browse the repository at this point in the history
Ref. #1502.
  • Loading branch information
Ms2ger committed Jun 28, 2021
1 parent 0e24fd3 commit cd616a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ export class ZonedDateTime {
const instantStart = ES.BuiltinTimeZoneGetInstantFor(timeZone, dtStart, 'compatible');
const endNs = ES.AddZonedDateTime(instantStart, timeZone, calendar, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
const dayLengthNs = endNs.subtract(GetSlot(instantStart, EPOCHNANOSECONDS));
if (dayLengthNs.isZero()) {
throw new RangeError('can not round a ZonedDateTime in a calendar with zero-length days');
}
({ year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = ES.RoundISODateTime(
year,
month,
Expand Down
23 changes: 23 additions & 0 deletions polyfill/test/ZonedDateTime/prototype/round/div-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.zoneddatetime.prototype.round
features: [Temporal]
---*/

class Calendar extends Temporal.Calendar {
constructor() {
super("iso8601")
}
dateAdd(d) {
return d;
}
};

const zdt = new Temporal.ZonedDateTime(0n, "UTC", new Calendar());

const units = ["day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"];
for (const smallestUnit of units) {
assert.throws(RangeError, () => zdt.round({ smallestUnit }));
}
2 changes: 2 additions & 0 deletions spec/zoneddatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,8 @@ <h1>Temporal.ZonedDateTime.prototype.round ( _options_ )</h1>
1. Let _startNs_ be _instantStart_.[[Nanoseconds]].
1. Let _endNs_ be ? AddZonedDateTime(_startNs_, _timeZone_, _zonedDateTime_.[[Calendar]], 0, 0, 0, 1, 0, 0, 0, 0, 0, 0).
1. Let _dayLengthNs_ be _endNs__startNs_.
1. If _dayLengthNs_ is 0, then
1. Throw a *RangeError* exception.
1. Let _roundResult_ be ? RoundISODateTime(_temporalDateTime_.[[ISOYear]], _temporalDateTime_.[[ISOMonth]], _temporalDateTime_.[[ISODay]], _temporalDateTime_.[[ISOHour]], _temporalDateTime_.[[ISOMinute]], _temporalDateTime_.[[ISOSecond]], _temporalDateTime_.[[ISOMillisecond]], _temporalDateTime_.[[ISOMicrosecond]], _temporalDateTime_.[[ISONanosecond]], _roundingIncrement_, _smallestUnit_, _roundingMode_, _dayLengthNs_).
1. Let _offsetNanoseconds_ be ? GetOffsetNanosecondsFor(_timeZone_, _instant_).
1. Let _epochNanoseconds_ be ? InterpretISODateTimeOffset(_roundResult_.[[Year]], _roundResult_.[[Month]], _roundResult_.[[Day]], _roundResult_.[[Hour]], _roundResult_.[[Minute]], _roundResult_.[[Second]], _roundResult_.[[Millisecond]], _roundResult_.[[Microsecond]], _roundResult_.[[Nanosecond]], _offsetNanoseconds_, _timeZone_, *"compatible"*, *"prefer"*).
Expand Down

0 comments on commit cd616a1

Please sign in to comment.