-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtime.ts
29 lines (26 loc) · 921 Bytes
/
time.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 ts, { factory } from "typescript";
export function transformTime(expression: ts.CallExpression): ts.Expression {
let typeName = "UnixTimestamp";
const [kindName] = expression.arguments;
if (kindName !== undefined && ts.isStringLiteral(kindName)) {
typeName = kindName.text;
}
switch (typeName) {
case "DateTime":
return factory.createNonNullExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(factory.createIdentifier("DateTime"), "fromIsoDate"),
undefined,
[factory.createStringLiteral(new Date().toISOString())],
),
);
case "UnixTimestamp":
return factory.createNumericLiteral(Math.floor(new Date().valueOf() / 1000));
case "UnixTimestampMillis":
return factory.createNumericLiteral(new Date().valueOf());
case "ISO-8601":
return factory.createStringLiteral(new Date().toISOString());
default:
throw `Invalid input`;
}
}