Skip to content

Commit 62ff777

Browse files
authored
Treat the System time since Epoch as a bigint (#58)
1 parent d24575f commit 62ff777

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/ecmascript.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ import {
5555
NANOSECONDS
5656
} from './slots';
5757
import { IsBuiltinCalendar } from './calendar';
58-
59-
const DAYMILLIS = 86400000;
60-
const NS_MIN = bigInt(-86400).multiply(1e17);
61-
const NS_MAX = bigInt(86400).multiply(1e17);
58+
const DAY_SECONDS = 86400;
59+
const DAY_NANOS = bigInt(DAY_SECONDS).multiply(1e9);
60+
const NS_MIN = bigInt(-DAY_SECONDS).multiply(1e17);
61+
const NS_MAX = bigInt(DAY_SECONDS).multiply(1e17);
6262
const YEAR_MIN = -271821;
6363
const YEAR_MAX = 275760;
6464
const BEFORE_FIRST_DST = bigInt(-388152).multiply(1e13); // 1847-01-01T00:00:00Z
@@ -2266,13 +2266,13 @@ export const ES = ObjectAssign({}, ES2020, {
22662266
return ES.BalanceISODateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
22672267
},
22682268
GetIANATimeZoneNextTransition: (epochNanoseconds, id) => {
2269-
const uppercap = ES.SystemUTCEpochNanoSeconds() + 366 * DAYMILLIS * 1e6;
2269+
const uppercap = ES.SystemUTCEpochNanoSeconds().plus(DAY_NANOS.multiply(366));
22702270
let leftNanos = epochNanoseconds;
22712271
const leftOffsetNs = ES.GetIANATimeZoneOffsetNanoseconds(leftNanos, id);
22722272
let rightNanos = leftNanos;
22732273
let rightOffsetNs = leftOffsetNs;
22742274
while (leftOffsetNs === rightOffsetNs && bigInt(leftNanos).compare(uppercap) === -1) {
2275-
rightNanos = bigInt(leftNanos).plus(2 * 7 * DAYMILLIS * 1e6);
2275+
rightNanos = bigInt(leftNanos).plus(DAY_NANOS.multiply(2 * 7));
22762276
rightOffsetNs = ES.GetIANATimeZoneOffsetNanoseconds(rightNanos, id);
22772277
if (leftOffsetNs === rightOffsetNs) {
22782278
leftNanos = rightNanos;
@@ -2295,7 +2295,7 @@ export const ES = ObjectAssign({}, ES2020, {
22952295
let leftNanos = rightNanos;
22962296
let leftOffsetNs = rightOffsetNs;
22972297
while (rightOffsetNs === leftOffsetNs && bigInt(rightNanos).compare(lowercap) === 1) {
2298-
leftNanos = bigInt(rightNanos).minus(2 * 7 * DAYMILLIS * 1e6);
2298+
leftNanos = bigInt(rightNanos).minus(DAY_NANOS.multiply(2 * 7));
22992299
leftOffsetNs = ES.GetIANATimeZoneOffsetNanoseconds(leftNanos, id);
23002300
if (rightOffsetNs === leftOffsetNs) {
23012301
rightNanos = leftNanos;
@@ -2331,10 +2331,9 @@ export const ES = ObjectAssign({}, ES2020, {
23312331
GetIANATimeZoneEpochValue: (id, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond) => {
23322332
const ns = ES.GetEpochFromISOParts(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
23332333
if (ns === null) throw new RangeError('DateTime outside of supported range');
2334-
const dayNanos = bigInt(DAYMILLIS).multiply(1e6);
2335-
let nsEarlier = ns.minus(dayNanos);
2334+
let nsEarlier = ns.minus(DAY_NANOS);
23362335
if (nsEarlier.lesser(NS_MIN)) nsEarlier = ns;
2337-
let nsLater = ns.plus(dayNanos);
2336+
let nsLater = ns.plus(DAY_NANOS);
23382337
if (nsLater.greater(NS_MAX)) nsLater = ns;
23392338
const earliest = ES.GetIANATimeZoneOffsetNanoseconds(nsEarlier, id);
23402339
const latest = ES.GetIANATimeZoneOffsetNanoseconds(nsLater, id);

test/timezone.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ describe('TimeZone', () => {
417417
});
418418
describe('getNextTransition works', () => {
419419
const nyc = Temporal.TimeZone.from('America/New_York');
420+
const noTransitionTZ = Temporal.TimeZone.from('Etc/GMT+10');
421+
420422
it('should not have bug #510', () => {
421423
// See https://github.com/tc39/proposal-temporal/issues/510 for more.
422424
const a1 = Temporal.Instant.from('2019-04-16T21:01Z');
@@ -430,6 +432,10 @@ describe('TimeZone', () => {
430432
equal(`${nyc.getNextTransition(inst)}`, '2019-03-10T07:00:00Z');
431433
equal(`${nyc.getNextTransition(nyc.getNextTransition(inst))}`, '2019-11-03T06:00:00Z');
432434
});
435+
it('should work for timezones with no scheduled transitions in the near future', () => {
436+
const start = Temporal.Instant.from('1945-10-15T13:00:00Z');
437+
equal(noTransitionTZ.getNextTransition(start), null);
438+
});
433439
it('casts argument', () => {
434440
equal(`${nyc.getNextTransition('2019-04-16T21:01Z')}`, '2019-11-03T06:00:00Z');
435441
});

0 commit comments

Comments
 (0)