Skip to content

Commit 6354adf

Browse files
committed
Normative: Prevent indefinite loops in NormalizedTimeDurationToDays
It's possible to make at least the second loop continue indefinitely with a contrived calendar and time zone. DRAFT: Still to be determined if this precludes any non-contrived use cases. If so, we will keep the loops, but still put an upper limit on the number of iterations. Includes a few more tests in the NYSE time zone cookbook example to make sure that a time zone transition of >24h continues to work.
1 parent e661b2b commit 6354adf

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

docs/cookbook/stockExchangeTimeZone.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,30 @@ assert.equal(monday.hoursInDay, 24);
222222
const friday = monday.add({ days: 4 });
223223
assert.equal(friday.hoursInDay, 72);
224224

225-
// Adding 1 day to Friday gets you the next Monday
225+
// Adding 1 day to Friday gets you the next Monday (disambiguates forward)
226226
assert.equal(friday.add({ days: 1 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]');
227227
// Adding 3 days to Friday also gets you the next Monday
228228
assert.equal(friday.add({ days: 3 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]');
229+
230+
const nextMonday = monday.add({ weeks: 1 });
231+
232+
// Subtracting 1 day from Monday gets you the same day (disambiguates forward)
233+
assert.equal(nextMonday.subtract({ days: 1 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]');
234+
// Subtracting 3 days from Monday gets you the previous Friday
235+
assert.equal(nextMonday.subtract({ days: 3 }).toString(), '2022-08-26T09:30:00-04:00[NYSE]');
236+
237+
// Difference between Friday and Monday is 72 hours or 3 days
238+
const fridayUntilMonday = friday.until(nextMonday);
239+
assert.equal(fridayUntilMonday.toString(), 'PT72H');
240+
assert.equal(fridayUntilMonday.total('hours'), 72);
241+
assert.equal(fridayUntilMonday.total('days'), 3);
242+
243+
const mondaySinceFriday = nextMonday.since(friday);
244+
assert.equal(mondaySinceFriday.toString(), 'PT72H');
245+
assert.equal(mondaySinceFriday.total('hours'), 72);
246+
assert.equal(mondaySinceFriday.total('days'), 3);
247+
248+
// One week is still 7 days
249+
const oneWeek = Temporal.Duration.from({ weeks: 1 });
250+
assert.equal(oneWeek.total({ unit: 'days', relativeTo: monday }), 7);
251+
assert.equal(oneWeek.total({ unit: 'days', relativeTo: friday }), 7);

polyfill/lib/ecmascript.mjs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,20 +3162,34 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec)
31623162
// back inside the period where it belongs. Note that this case only can
31633163
// happen for positive durations because the only direction that
31643164
// `disambiguation: 'compatible'` can change clock time is forwards.
3165-
if (sign === 1) {
3166-
while (days > 0 && relativeResult.epochNs.greater(endNs)) {
3167-
days--;
3168-
relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days);
3169-
// may do disambiguation
3165+
if (sign === 1 && days > 0 && relativeResult.epochNs.greater(endNs)) {
3166+
days--;
3167+
relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days);
3168+
// may do disambiguation
3169+
if (days > 0 && relativeResult.epochNs.greater(endNs)) {
3170+
throw new RangeError('inconsistent result from custom time zone getInstantFor()');
31703171
}
31713172
}
31723173
norm = TimeDuration.fromEpochNsDiff(endNs, relativeResult.epochNs);
31733174

3174-
let isOverflow = false;
3175-
let dayLengthNs;
3176-
do {
3177-
// calculate length of the next day (day that contains the time remainder)
3178-
const oneDayFarther = AddDaysToZonedDateTime(
3175+
// calculate length of the next day (day that contains the time remainder)
3176+
let oneDayFarther = AddDaysToZonedDateTime(
3177+
relativeResult.instant,
3178+
relativeResult.dateTime,
3179+
timeZoneRec,
3180+
calendar,
3181+
sign
3182+
);
3183+
let dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs);
3184+
const oneDayLess = norm.subtract(dayLengthNs);
3185+
let isOverflow = oneDayLess.sign() * sign >= 0;
3186+
if (isOverflow) {
3187+
norm = oneDayLess;
3188+
relativeResult = oneDayFarther;
3189+
days += sign;
3190+
3191+
// ensure there was no more overflow
3192+
oneDayFarther = AddDaysToZonedDateTime(
31793193
relativeResult.instant,
31803194
relativeResult.dateTime,
31813195
timeZoneRec,
@@ -3185,12 +3199,8 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec)
31853199

31863200
dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs);
31873201
isOverflow = norm.subtract(dayLengthNs).sign() * sign >= 0;
3188-
if (isOverflow) {
3189-
norm = norm.subtract(dayLengthNs);
3190-
relativeResult = oneDayFarther;
3191-
days += sign;
3192-
}
3193-
} while (isOverflow);
3202+
if (isOverflow) throw new RangeError('inconsistent result from custom time zone getInstantFor()');
3203+
}
31943204
if (days !== 0 && MathSign(days) != sign) {
31953205
throw new RangeError('Time zone or calendar converted nanoseconds into a number of days with the opposite sign');
31963206
}

spec/zoneddatetime.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,22 +1443,22 @@ <h1>
14431443
1. Let _dateDifference_ be ? DifferenceISODateTime(_startDateTime_.[[ISOYear]], _startDateTime_.[[ISOMonth]], _startDateTime_.[[ISODay]], _startDateTime_.[[ISOHour]], _startDateTime_.[[ISOMinute]], _startDateTime_.[[ISOSecond]], _startDateTime_.[[ISOMillisecond]], _startDateTime_.[[ISOMicrosecond]], _startDateTime_.[[ISONanosecond]], _endDateTime_.[[ISOYear]], _endDateTime_.[[ISOMonth]], _endDateTime_.[[ISODay]], _endDateTime_.[[ISOHour]], _endDateTime_.[[ISOMinute]], _endDateTime_.[[ISOSecond]], _endDateTime_.[[ISOMillisecond]], _endDateTime_.[[ISOMicrosecond]], _endDateTime_.[[ISONanosecond]], _zonedRelativeTo_.[[Calendar]], *"day"*, OrdinaryObjectCreate(*null*)).
14441444
1. Let _days_ be _dateDifference_.[[Days]].
14451445
1. Let _relativeResult_ be ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1446-
1. If _sign_ is 1, then
1447-
1. Repeat, while _days_ &gt; 0 and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_,
1448-
1. Set _days_ to _days_ - 1.
1449-
1. Set _relativeResult_ to ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1446+
1. If _sign_ = 1, and _days_ &gt; 0, and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_, then
1447+
1. Set _days_ to _days_ - 1.
1448+
1. Set _relativeResult_ to ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1449+
1. If _days_ &gt; 0 and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_, throw a *RangeError* exception.
14501450
1. Set _norm_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_endNs_, _relativeResult_.[[EpochNanoseconds]]).
1451-
1. Let _done_ be *false*.
1452-
1. Let _dayLengthNs_ be ~unset~.
1453-
1. Repeat, while _done_ is *false*,
1454-
1. Let _oneDayFarther_ be ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
1451+
1. Let _oneDayFarther_ be ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
1452+
1. Let _dayLengthNs_ be NormalizedTimeDurationFromEpochNanosecondsDifference(_oneDayFarther.[[EpochNanoseconds]], _relativeResult_.[[EpochNanoseconds]]).
1453+
1. Let _oneDayLess_ be ? SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_).
1454+
1. If NormalizedTimeDurationSign(_oneDayLess_) &times; _sign_ &ge; 0, then
1455+
1. Set _norm_ to _oneDayLess_.
1456+
1. Set _relativeResult_ to _oneDayFarther_.
1457+
1. Set _days_ to _days_ + _sign_.
1458+
1. Set _oneDayFarther_ to ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
14551459
1. Set _dayLengthNs_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_oneDayFarther.[[EpochNanoseconds]], _relativeResult_.[[EpochNanoseconds]]).
14561460
1. If NormalizedTimeDurationSign(? SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_)) &times; _sign_ &ge; 0, then
1457-
1. Set _norm_ to ? SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_).
1458-
1. Set _relativeResult_ to _oneDayFarther_.
1459-
1. Set _days_ to _days_ + _sign_.
1460-
1. Else,
1461-
1. Set _done_ to *true*.
1461+
1. Throw a *RangeError* exception.
14621462
1. If _days_ &lt; 0 and _sign_ = 1, throw a *RangeError* exception.
14631463
1. If _days_ &gt; 0 and _sign_ = -1, throw a *RangeError* exception.
14641464
1. If NormalizedTimeDurationSign(_norm_) = -1, then

0 commit comments

Comments
 (0)