Skip to content

Commit e456829

Browse files
committed
fix: chrono-node upgrade changed from 60 minutes ago to now behavior
1 parent 5758efc commit e456829

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/cubejs-api-gateway/dateParser.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,24 @@ module.exports = (dateString, timezone) => {
4646
} else if (dateString.match(/^from (.*) to (.*)$/)) {
4747
// eslint-disable-next-line no-unused-vars
4848
const [all, from, to] = dateString.match(/^from (.*) to (.*)$/);
49-
const fromResults = chrono.parse(from, moment().tz(timezone));
50-
const toResults = chrono.parse(to, moment().tz(timezone));
49+
const fromResults = chrono.parse(from, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
50+
const toResults = chrono.parse(to, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
5151
if (!fromResults) {
5252
throw new UserError(`Can't parse date: '${from}'`);
5353
}
5454
if (!toResults) {
5555
throw new UserError(`Can't parse date: '${to}'`);
5656
}
5757
const exactGranularity = ['second', 'minute', 'hour'].find(g => dateString.indexOf(g) !== -1) || 'day';
58+
console.log(fromResults[0].start);
59+
console.log(toResults[0].start);
5860
momentRange = [
5961
momentFromResult(fromResults[0].start, timezone),
6062
momentFromResult(toResults[0].start, timezone)
6163
];
6264
momentRange = [momentRange[0].startOf(exactGranularity), momentRange[1].endOf(exactGranularity)];
6365
} else {
64-
const results = chrono.parse(dateString, moment().tz(timezone));
66+
const results = chrono.parse(dateString, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
6567
if (!results) {
6668
throw new UserError(`Can't parse date: '${dateString}'`);
6769
}

packages/cubejs-api-gateway/dateParser.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* globals describe,test,expect */
22

3+
const moment = require('moment-timezone');
34
const dateParser = require('./dateParser');
45

56
describe(`dateParser`, () => {
@@ -43,6 +44,22 @@ describe(`dateParser`, () => {
4344
);
4445
});
4546

47+
test(`from 1 hour ago to now LA`, () => {
48+
const date = new Date();
49+
const from = moment().tz('America/Los_Angeles').subtract({
50+
hours: 1,
51+
minutes: date.getMinutes(),
52+
seconds: date.getSeconds(),
53+
milliseconds: date.getMilliseconds()
54+
});
55+
expect(dateParser('from 1 hour ago to now', 'America/Los_Angeles')).toStrictEqual(
56+
[
57+
from.format(moment.HTML5_FMT.DATETIME_LOCAL_MS),
58+
from.clone().add({ hours: 2 }).subtract({ milliseconds: 1 }).format(moment.HTML5_FMT.DATETIME_LOCAL_MS)
59+
]
60+
);
61+
});
62+
4663
test(`from 7 days ago to now`, () => {
4764
expect(dateParser('from 7 days ago to now', 'UTC')).toStrictEqual(
4865
[dateParser('last 7 days', 'UTC')[0], dateParser('today', 'UTC')[1]]

0 commit comments

Comments
 (0)