postgrejs
Version:
Professional PostgreSQL client NodeJS
67 lines (66 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayTimeType = exports.TimeType = void 0;
const constants_js_1 = require("../constants.js");
const parse_time_js_1 = require("../util/parse-time.js");
const timeMul = 4294967296;
exports.TimeType = {
name: 'time',
oid: constants_js_1.DataTypeOIDs.time,
jsType: 'string',
parseBinary(v, options) {
const fetchAsString = options.fetchAsString &&
options.fetchAsString.includes(constants_js_1.DataTypeOIDs.time);
const hi = v.readInt32BE();
const lo = v.readUInt32BE(4);
let d = new Date((lo + hi * timeMul) / 1000);
if (fetchAsString || !options.utcDates) {
d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
}
return fetchAsString ? dateToTimeString(d) : d;
},
encodeBinary(buf, v, options) {
if (typeof v === 'string')
v = (0, parse_time_js_1.parseTime)(v, false, options.utcDates);
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 * 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.time))
return v;
return (0, parse_time_js_1.parseTime)(v, false, options.utcDates);
},
isType(v) {
return ((v instanceof Date &&
v.getFullYear() === 1970 &&
v.getMonth() === 0 &&
v.getDate() === 1) ||
(typeof v === 'string' && parse_time_js_1.STRICT_TIME_PATTERN.test(v)));
},
};
function padZero(v) {
return v < 9 ? '0' + v : '' + v;
}
function dateToTimeString(d) {
return (padZero(d.getHours()) +
':' +
padZero(d.getMinutes()) +
':' +
padZero(d.getSeconds()));
}
exports.ArrayTimeType = {
...exports.TimeType,
name: '_time',
oid: constants_js_1.DataTypeOIDs._time,
elementsOID: constants_js_1.DataTypeOIDs.time,
};