CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/database/pool/util.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Postgres 14 is different than 13 an earlier.
7
// extract(epoch from timestamp) returns a "numeric", which is converted to a string by the pg driver.
8
// we convert this explicitly to a floating point number to get the ms since epoch.
9
// Note: JavaScript's new Date(...) has no hesitation converting from a float.
10
export function timeInSeconds(field: string, asField?: string): string {
11
return ` (EXTRACT(EPOCH FROM ${field})*1000)::FLOAT as ${asField ?? field} `;
12
}
13
14
// Given number of seconds **in the future**.
15
export function expireTime(ttl_s: number = 0): Date {
16
return new Date(Date.now() + ttl_s * 1000);
17
}
18
19