wanasit / chrono

A natural language date parser in Javascript
MIT License
4.6k stars 341 forks source link

Timezone parsing reference not working as expected with `forwardDate: true` specified #553

Open sarahmaas opened 6 months ago

sarahmaas commented 6 months ago

Apologies for all of the setup here for the date, but this is what I'm working with:

// Node for commented results is v18.11.0
var chronoNode = require("chrono-node"); // v2.7.0
var moment = require("moment-timezone"); // v0.5.43
var baseDate = '2020-02-16 10:00:00';
var timeZoneId = 'America/New_York';
var momentDate = moment(baseDate).tz(timeZoneId, true);
var jsDate = momentDate.toDate(); 
// Tue Feb 18 2020 07:00:00 GMT-0800 (Pacific Standard Time)
var timezoneAbbr = moment(momentDate).tz(timeZoneId).zoneAbbr();
// EST
const referenceDate = {
    instant: jsDate,
    timezone: timezoneAbbr
};

function testParser(text, referenceDate) {
    let result;
    result = chronoNode.parseDate(text, referenceDate);
    console.log(result);

    result = chronoNode.parseDate(text, referenceDate, { forwardDate: true });
    console.log(result);

    result = chronoNode.parse(text, referenceDate);
    console.log(result);

    result = chronoNode.parse(text, referenceDate, { forwardDate: true });
    console.log(result);
}

testParser('every week on Sunday at noon', referenceDate); // after both timezones
/* gives dates:
Sun Feb 16 2020 09:00:00 GMT-0800 (Pacific Standard Time)
Sun Feb 16 2020 09:00:00 GMT-0800 (Pacific Standard Time)
This is working as expected, since it is two hours before noon in the referenced timezone.
*/

testParser('every week on Sunday at 8 AM', referenceDate); // before both timezones
/* gives dates:
Sun Feb 16 2020 05:00:00 GMT-0800 (Pacific Standard Time)
Sun Feb 23 2020 05:00:00 GMT-0800 (Pacific Standard Time)
Also working as expected
*/

testParser('every week on Sunday at 10 AM', referenceDate); // exact time it is currently
/* gives dates without, then with forwardDate:
Sun Feb 16 2020 07:00:00 GMT-0800 (Pacific Standard Time)
Sun Feb 16 2020 07:00:00 GMT-0800 (Pacific Standard Time)
Also working as expected
*/

testParser('every week on Sunday at noon starting Feb 16', referenceDate); // today, in two hours
/* gives dates without, then with forwardDate:
Sun Feb 16 2020 09:00:00 GMT-0800 (Pacific Standard Time)
Sun Feb 16 2020 09:00:00 GMT-0800 (Pacific Standard Time)
Also working as expected
*/

testParser('every 8 weeks on Sunday at noon starting Feb 16', referenceDate); // today, in two hours
/* gives dates without, then with forwardDate:
Sun Feb 16 2020 09:00:00 GMT-0800 (Pacific Standard Time)
Sun Apr 12 2020 13:00:00 GMT-0700 (Pacific Daylight Time) 
potentially expected date because it's dropping 'every', but not expected time
*/

This is adding 5 hours to what should be a 3 hour time difference with a daylight savings change of 1 hour in that time frame. It seems to be related to parse adding the implied value of timezoneOffset: -480.

wanasit commented 3 months ago

Sorry, I don't understand the setup and question.

First, Chrono doesn't support "every" or "every week" out-of-box. Is this some additional part that you date?

Second, from what I tested, the input 'every 8 weeks [on Sunday at noon] starting [Feb 16]' should output two separate results, which one are you referring to that has the problem?

If you believe the problem is related to the timezone, could you provide a short example around that daylight-saving window change?