Skip to content

Commit

Permalink
fix: chrono-node upgrade changed from 60 minutes ago to now behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltiunov committed Jul 8, 2020
1 parent 5758efc commit e456829
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/cubejs-api-gateway/dateParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ module.exports = (dateString, timezone) => {
} else if (dateString.match(/^from (.*) to (.*)$/)) {
// eslint-disable-next-line no-unused-vars
const [all, from, to] = dateString.match(/^from (.*) to (.*)$/);
const fromResults = chrono.parse(from, moment().tz(timezone));
const toResults = chrono.parse(to, moment().tz(timezone));
const fromResults = chrono.parse(from, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
const toResults = chrono.parse(to, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
if (!fromResults) {
throw new UserError(`Can't parse date: '${from}'`);
}
if (!toResults) {
throw new UserError(`Can't parse date: '${to}'`);
}
const exactGranularity = ['second', 'minute', 'hour'].find(g => dateString.indexOf(g) !== -1) || 'day';
console.log(fromResults[0].start);
console.log(toResults[0].start);
momentRange = [
momentFromResult(fromResults[0].start, timezone),
momentFromResult(toResults[0].start, timezone)
];
momentRange = [momentRange[0].startOf(exactGranularity), momentRange[1].endOf(exactGranularity)];
} else {
const results = chrono.parse(dateString, moment().tz(timezone));
const results = chrono.parse(dateString, moment().tz(timezone).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
if (!results) {
throw new UserError(`Can't parse date: '${dateString}'`);
}
Expand Down
17 changes: 17 additions & 0 deletions packages/cubejs-api-gateway/dateParser.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals describe,test,expect */

const moment = require('moment-timezone');
const dateParser = require('./dateParser');

describe(`dateParser`, () => {
Expand Down Expand Up @@ -43,6 +44,22 @@ describe(`dateParser`, () => {
);
});

test(`from 1 hour ago to now LA`, () => {
const date = new Date();
const from = moment().tz('America/Los_Angeles').subtract({
hours: 1,
minutes: date.getMinutes(),
seconds: date.getSeconds(),
milliseconds: date.getMilliseconds()
});
expect(dateParser('from 1 hour ago to now', 'America/Los_Angeles')).toStrictEqual(
[
from.format(moment.HTML5_FMT.DATETIME_LOCAL_MS),
from.clone().add({ hours: 2 }).subtract({ milliseconds: 1 }).format(moment.HTML5_FMT.DATETIME_LOCAL_MS)
]
);
});

test(`from 7 days ago to now`, () => {
expect(dateParser('from 7 days ago to now', 'UTC')).toStrictEqual(
[dateParser('last 7 days', 'UTC')[0], dateParser('today', 'UTC')[1]]
Expand Down

0 comments on commit e456829

Please sign in to comment.