diff --git a/docs/cookbook/stockExchangeTimeZone.mjs b/docs/cookbook/stockExchangeTimeZone.mjs index b8cbb77a45..024962a6cf 100644 --- a/docs/cookbook/stockExchangeTimeZone.mjs +++ b/docs/cookbook/stockExchangeTimeZone.mjs @@ -222,7 +222,30 @@ assert.equal(monday.hoursInDay, 24); const friday = monday.add({ days: 4 }); assert.equal(friday.hoursInDay, 72); -// Adding 1 day to Friday gets you the next Monday +// Adding 1 day to Friday gets you the next Monday (disambiguates forward) assert.equal(friday.add({ days: 1 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]'); // Adding 3 days to Friday also gets you the next Monday assert.equal(friday.add({ days: 3 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]'); + +const nextMonday = monday.add({ weeks: 1 }); + +// Subtracting 1 day from Monday gets you the same day (disambiguates forward) +assert.equal(nextMonday.subtract({ days: 1 }).toString(), '2022-08-29T09:30:00-04:00[NYSE]'); +// Subtracting 3 days from Monday gets you the previous Friday +assert.equal(nextMonday.subtract({ days: 3 }).toString(), '2022-08-26T09:30:00-04:00[NYSE]'); + +// Difference between Friday and Monday is 72 hours or 3 days +const fridayUntilMonday = friday.until(nextMonday); +assert.equal(fridayUntilMonday.toString(), 'PT72H'); +assert.equal(fridayUntilMonday.total('hours'), 72); +assert.equal(fridayUntilMonday.total('days'), 3); + +const mondaySinceFriday = nextMonday.since(friday); +assert.equal(mondaySinceFriday.toString(), 'PT72H'); +assert.equal(mondaySinceFriday.total('hours'), 72); +assert.equal(mondaySinceFriday.total('days'), 3); + +// One week is still 7 days +const oneWeek = Temporal.Duration.from({ weeks: 1 }); +assert.equal(oneWeek.total({ unit: 'days', relativeTo: monday }), 7); +assert.equal(oneWeek.total({ unit: 'days', relativeTo: friday }), 7); diff --git a/polyfill/lib/ecmascript.mjs b/polyfill/lib/ecmascript.mjs index b15971572c..2bcb3f68f6 100644 --- a/polyfill/lib/ecmascript.mjs +++ b/polyfill/lib/ecmascript.mjs @@ -3162,20 +3162,34 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec) // back inside the period where it belongs. Note that this case only can // happen for positive durations because the only direction that // `disambiguation: 'compatible'` can change clock time is forwards. - if (sign === 1) { - while (days > 0 && relativeResult.epochNs.greater(endNs)) { - days--; - relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days); - // may do disambiguation + if (sign === 1 && days > 0 && relativeResult.epochNs.greater(endNs)) { + days--; + relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days); + // may do disambiguation + if (days > 0 && relativeResult.epochNs.greater(endNs)) { + throw new RangeError('inconsistent result from custom time zone getInstantFor()'); } } norm = TimeDuration.fromEpochNsDiff(endNs, relativeResult.epochNs); - let isOverflow = false; - let dayLengthNs; - do { - // calculate length of the next day (day that contains the time remainder) - const oneDayFarther = AddDaysToZonedDateTime( + // calculate length of the next day (day that contains the time remainder) + let oneDayFarther = AddDaysToZonedDateTime( + relativeResult.instant, + relativeResult.dateTime, + timeZoneRec, + calendar, + sign + ); + let dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs); + const oneDayLess = norm.subtract(dayLengthNs); + let isOverflow = oneDayLess.sign() * sign >= 0; + if (isOverflow) { + norm = oneDayLess; + relativeResult = oneDayFarther; + days += sign; + + // ensure there was no more overflow + oneDayFarther = AddDaysToZonedDateTime( relativeResult.instant, relativeResult.dateTime, timeZoneRec, @@ -3185,12 +3199,8 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec) dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs); isOverflow = norm.subtract(dayLengthNs).sign() * sign >= 0; - if (isOverflow) { - norm = norm.subtract(dayLengthNs); - relativeResult = oneDayFarther; - days += sign; - } - } while (isOverflow); + if (isOverflow) throw new RangeError('inconsistent result from custom time zone getInstantFor()'); + } if (days !== 0 && MathSign(days) != sign) { throw new RangeError('Time zone or calendar converted nanoseconds into a number of days with the opposite sign'); } diff --git a/spec/zoneddatetime.html b/spec/zoneddatetime.html index 54807fd9ea..dc4aa3089c 100644 --- a/spec/zoneddatetime.html +++ b/spec/zoneddatetime.html @@ -1443,22 +1443,22 @@