Skip to content

Commit 73cf70d

Browse files
authored
Better handling of millis and micros (#68)
* Clean up TIMESTAMP issues ZJONSSON#65 ZJONSSON#45 * Use MAX_SAFE_INTEGER for testing toPrimitive_TIME_MILLIS
1 parent d382d0f commit 73cf70d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ function toPrimitive_TIME_MILLIS(value: string | number) {
386386
if (typeof value === `string`) {
387387
v = parseInt(value, 10);
388388
}
389-
if (v < 0 || v > 0xffffffffffffffff || typeof v !== 'number') {
389+
// Year 2255 bug. Should eventually switch to bigint
390+
if (v < 0 || v > (Number.MAX_SAFE_INTEGER - 1) || typeof v !== 'number') {
390391
throw 'invalid value for TIME_MILLIS: ' + value;
391392
}
392393

@@ -461,18 +462,22 @@ function toPrimitive_TIMESTAMP_MICROS(value: Date | string | number | bigint) {
461462
}
462463

463464
/* convert from integer */
464-
{
465+
try {
466+
// Will throw if NaN
465467
const v = BigInt(value);
466-
if (v < 0n /*|| isNaN(v)*/) {
467-
throw 'invalid value for TIMESTAMP_MICROS: ' + value;
468+
if (v < 0n) {
469+
throw 'Cannot be less than zero';
468470
}
469471

470472
return v;
473+
} catch (e) {
474+
throw 'invalid value for TIMESTAMP_MICROS: ' + value;
471475
}
472476
}
473477

474478
function fromPrimitive_TIMESTAMP_MICROS(value: number | bigint) {
475-
return typeof value === 'bigint' ? new Date(Number(value / 1000n)): new Date(value / 1000);
479+
if (typeof value === 'bigint') return new Date(Number(value / 1000n));
480+
return new Date(value / 1000);
476481
}
477482

478483
function toPrimitive_INTERVAL(value: INTERVAL) {

0 commit comments

Comments
 (0)