@brimdata/zealot
Version:
The Javascript Client for Zed Lakes
39 lines (38 loc) • 1.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "createTime", {
enumerable: true,
get: ()=>createTime
});
const _utils = require("./utils");
function fracSecToNs(string) {
const [sec, frac] = string.split(".");
const secNs = BigInt(parseInt(sec)) * BigInt(1e9);
const fracNs = BigInt(parseInt(frac) * Math.pow(10, 9 - frac.length));
return secNs + fracNs;
}
function convertToNs(val) {
if ((0, _utils.isDate)(val)) return BigInt(val.getTime()) * BigInt(1e6);
if ((0, _utils.isBigInt)(val)) return val;
if ((0, _utils.isTs)(val)) return BigInt(1e9) * BigInt(val.sec) + BigInt(val.ns);
if ((0, _utils.isFracSec)(val)) return fracSecToNs(val);
throw new Error(`Unknown time format: ${val}`);
}
function createTime(val = new Date()) {
const ns = convertToNs(val);
return {
toNs () {
return ns;
},
toTs () {
const sec = ns / BigInt(1e9);
const restNs = ns - sec * BigInt(1e9);
return {
sec: Number(sec),
ns: Number(restNs)
};
}
};
}