forked from oguimbal/pg-mem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.ts
29 lines (27 loc) · 905 Bytes
/
date.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { FunctionDefinition } from '../interfaces';
import moment from 'moment';
import { DataType, QueryError } from '../interfaces-private';
import { nullIsh } from '../utils';
export const dateFunctions: FunctionDefinition[] = [
{
name: 'to_date',
args: [DataType.text, DataType.text],
returns: DataType.date,
implementation: (data, format) => {
if (nullIsh(data) || nullIsh(format)) {
return null; // if one argument is null => null
}
const ret = moment.utc(data, format);
if (!ret.isValid()) {
throw new QueryError(`The text '${data}' does not match the date format ${format}`);
}
return ret.toDate();
}
},
{
name: 'now',
returns: DataType.timestamptz,
impure: true,
implementation: () => new Date(),
},
];