@cocalc/database
Version:
CoCalc: code for working with our PostgreSQL database
21 lines • 958 B
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.expireTime = exports.timeInSeconds = void 0;
// Postgres 14 is different than 13 an earlier.
// extract(epoch from timestamp) returns a "numeric", which is converted to a string by the pg driver.
// we convert this explicitly to a floating point number to get the ms since epoch.
// Note: JavaScript's new Date(...) has no hesitation converting from a float.
function timeInSeconds(field, asField) {
return ` (EXTRACT(EPOCH FROM ${field})*1000)::FLOAT as ${asField ?? field} `;
}
exports.timeInSeconds = timeInSeconds;
// Given number of seconds **in the future**.
function expireTime(ttl_s = 0) {
return new Date(new Date().valueOf() + ttl_s * 1000);
}
exports.expireTime = expireTime;
//# sourceMappingURL=util.js.map