2017-03-08 05:52:17 +00:00
|
|
|
import BaseExtension from './base/base-extension';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
|
|
|
export default class NowExtension extends BaseExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['now'];
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
run (context, dateType = 'iso-8601') {
|
|
|
|
if (typeof dateType === 'string') {
|
|
|
|
dateType = dateType.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const now = new Date();
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
switch (dateType) {
|
2017-02-20 18:32:27 +00:00
|
|
|
case 'millis':
|
|
|
|
case 'ms':
|
|
|
|
return now.getTime();
|
|
|
|
case 'unix':
|
|
|
|
case 'seconds':
|
|
|
|
case 's':
|
|
|
|
return Math.round(now.getTime() / 1000);
|
|
|
|
case 'iso-8601':
|
|
|
|
return now.toISOString();
|
2017-02-27 21:00:13 +00:00
|
|
|
default:
|
|
|
|
throw new Error(`Invalid date type "${dateType}"`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|