Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of millis and micros #68

Merged
merged 3 commits into from
May 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ function toPrimitive_TIME_MILLIS(value: string | number) {
if (typeof value === `string`) {
v = parseInt(value, 10);
}
if (v < 0 || v > 0xffffffffffffffff || typeof v !== 'number') {
// Year 2255 bug. Should eventually switch to bigint
if (v < 0 || v > (Number.MAX_SAFE_INTEGER - 1) || typeof v !== 'number') {
throw 'invalid value for TIME_MILLIS: ' + value;
}

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

/* convert from integer */
{
try {
// Will throw if NaN
const v = BigInt(value);
if (v < 0n /*|| isNaN(v)*/) {
throw 'invalid value for TIMESTAMP_MICROS: ' + value;
if (v < 0n) {
throw 'Cannot be less than zero';
}

return v;
} catch (e) {
throw 'invalid value for TIMESTAMP_MICROS: ' + value;
}
}

function fromPrimitive_TIMESTAMP_MICROS(value: number | bigint) {
return typeof value === 'bigint' ? new Date(Number(value / 1000n)): new Date(value / 1000);
if (typeof value === 'bigint') return new Date(Number(value / 1000n));
return new Date(value / 1000);
}

function toPrimitive_INTERVAL(value: INTERVAL) {
Expand Down