UNPKG

postgrejs

Version:

Professional PostgreSQL client NodeJS

90 lines (89 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayTimestampType = exports.TimestampType = void 0; const constants_js_1 = require("../constants.js"); const parse_datetime_js_1 = require("../util/parse-datetime.js"); const timeShift = 946684800000; const timeMul = 4294967296; exports.TimestampType = { name: 'timestamp', oid: constants_js_1.DataTypeOIDs.timestamp, jsType: 'Date', parseBinary(v, options) { const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamp); const hi = v.readInt32BE(); const lo = v.readUInt32BE(4); if (lo === 0xffffffff && hi === 0x7fffffff) return fetchAsString ? 'infinity' : Infinity; if (lo === 0x00000000 && hi === -0x80000000) return fetchAsString ? '-infinity' : -Infinity; // Shift from 2000 to 1970 let d = new Date((lo + hi * timeMul) / 1000 + timeShift); if (fetchAsString || !options.utcDates) { d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()); } return fetchAsString ? dateToTimestampString(d) : d; }, encodeBinary(buf, v, options) { if (typeof v === 'string') v = (0, parse_datetime_js_1.parseDateTime)(v, true, false, options.utcDates); if (v === Infinity) { buf.writeInt32BE(0x7fffffff); // hi buf.writeUInt32BE(0xffffffff); // lo return; } if (v === -Infinity) { buf.writeInt32BE(-0x80000000); // hi buf.writeUInt32BE(0x00000000); // lo return; } if (!(v instanceof Date)) v = new Date(v); // Postgresql ignores timezone data so we are let n = options.utcDates ? v.getTime() : v.getTime() - v.getTimezoneOffset() * 60 * 1000; n = (n - timeShift) * 1000; const hi = Math.floor(n / timeMul); const lo = n - hi * timeMul; buf.writeInt32BE(hi); buf.writeUInt32BE(lo); }, parseText(v, options) { if (options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamp)) return v; return (0, parse_datetime_js_1.parseDateTime)(v, true, false, options.utcDates); }, isType(v) { return (v instanceof Date && !(v.getFullYear() === 1970 && v.getMonth() === 0 && v.getDate() === 1) && !(v.getHours() === 0 && v.getMinutes() === 0 && v.getSeconds() === 0 && v.getMilliseconds() === 0)); }, }; function padZero(v) { return v < 9 ? '0' + v : '' + v; } function dateToTimestampString(d) { return (d.getFullYear() + '-' + padZero(d.getMonth() + 1) + '-' + padZero(d.getDate()) + ' ' + padZero(d.getHours()) + ':' + padZero(d.getMinutes()) + ':' + padZero(d.getSeconds())); } exports.ArrayTimestampType = { ...exports.TimestampType, name: '_timestamp', oid: constants_js_1.DataTypeOIDs._timestamp, elementsOID: constants_js_1.DataTypeOIDs.timestamp, };