Skip to content

Commit 652a001

Browse files
committed
TimeZone.getPreviousTransition() should never return its input
getPreviousTransition() returns the last transition before the instant passed into it, not the last transition before or equal to. Otherwise, you would have to manually subtract a nanosecond in between each call if you wanted to get a series of previous transitions. Add similar tests for getNextTransition(), although that wasn't broken. No change needed to the spec text or documentation, which are already correct.
1 parent 21e89ab commit 652a001

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/ecmascript.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ export const ES = ObjectAssign({}, ES2020, {
24322432
},
24332433
GetIANATimeZonePreviousTransition: (epochNanoseconds, id) => {
24342434
const lowercap = BEFORE_FIRST_DST; // 1847-01-01T00:00:00Z
2435-
let rightNanos = epochNanoseconds;
2435+
let rightNanos = bigInt(epochNanoseconds).minus(1);
24362436
let rightOffsetNs = ES.GetIANATimeZoneOffsetNanoseconds(rightNanos, id);
24372437
let leftNanos = rightNanos;
24382438
let leftOffsetNs = rightOffsetNs;

test/timezone.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,15 @@ describe('TimeZone', () => {
209209
for (let i = 0, txn = inst; i < 4; i++) {
210210
const transition = zone.getNextTransition(txn);
211211
assert(transition);
212+
assert(!transition.equals(txn));
212213
txn = transition;
213214
}
214215
});
215216
it(`(${zone}).getPreviousTransition() x 4 transitions`, () => {
216217
for (let i = 0, txn = inst; i < 4; i++) {
217218
const transition = zone.getPreviousTransition(txn);
218219
assert(transition);
220+
assert(!transition.equals(txn));
219221
txn = transition;
220222
}
221223
});
@@ -423,6 +425,11 @@ describe('TimeZone', () => {
423425
equal(nyc.getNextTransition(a1).toString(), '2019-11-03T06:00:00Z');
424426
equal(nyc.getNextTransition(a2).toString(), '1883-11-18T17:00:00Z');
425427
});
428+
it('should not return the same as its input if the input is a transition point', () => {
429+
const inst = Temporal.Instant.from('2019-01-01T00:00Z');
430+
equal(`${nyc.getNextTransition(inst)}`, '2019-03-10T07:00:00Z');
431+
equal(`${nyc.getNextTransition(nyc.getNextTransition(inst))}`, '2019-11-03T06:00:00Z');
432+
});
426433
it('casts argument', () => {
427434
equal(`${nyc.getNextTransition('2019-04-16T21:01Z')}`, '2019-11-03T06:00:00Z');
428435
});
@@ -441,6 +448,11 @@ describe('TimeZone', () => {
441448
equal(london.getPreviousTransition(a1).toString(), '2020-03-29T01:00:00Z');
442449
equal(london.getPreviousTransition(a2).toString(), '1847-12-01T00:01:15Z');
443450
});
451+
it('should not return the same as its input if the input is a transition point', () => {
452+
const inst = Temporal.Instant.from('2020-06-01T00:00Z');
453+
equal(`${london.getPreviousTransition(inst)}`, '2020-03-29T01:00:00Z');
454+
equal(`${london.getPreviousTransition(london.getPreviousTransition(inst))}`, '2019-10-27T01:00:00Z');
455+
});
444456
it('casts argument', () => {
445457
equal(`${london.getPreviousTransition('2020-06-11T21:01Z')}`, '2020-03-29T01:00:00Z');
446458
});

0 commit comments

Comments
 (0)