wanasit / chrono

A natural language date parser in Javascript
MIT License
4.61k stars 339 forks source link

inconsistent behaviour #464

Closed Mtillmann closed 2 years ago

Mtillmann commented 2 years ago
    let schedules = [
            ['next_morning', chrono.parse('Tomorrow at 10:00', new Date('08-02-2022'))[0].start.date()],
            ['next_evening', chrono.parse('Tomorrow at 19:00', new Date('08-02-2022'))[0].start.date()],
            ['next_saturday', chrono.parse('next Saturday at 11:00', new Date('08-02-2022'))[0].start.date()],
            ['next_sunday', chrono.parse('next Sunday at 15:00', new Date('08-02-2022'))[0].start.date()]
        ];

yields

[
 [ 'next_morning', 2022-08-03T08:00:00.000Z ],
 [ 'next_evening', 2022-08-03T17:00:00.000Z ],
 [ 'next_saturday', 2022-08-13T09:00:00.000Z ],
 [ 'next_sunday', 2022-08-07T13:00:00.000Z ]
]

next_sunday works as expected while next_saturday is one week off. Am I missing something?

Mtillmann commented 2 years ago

FWIW, the following chrono.parse-syntax seems to yield the desired values:

 let referenceDate = new Date('08-02-2022'),
      parsingOptions = {
          forwardDate: true
      },
      schedules = [
          ['next_morning', chrono.parse('Tomorrow, 10:00', referenceDate, parsingOptions)[0].start.date()],
          ['next_evening', chrono.parse('Tomorrow, 19:00', referenceDate, parsingOptions)[0].start.date()],
          ['next_saturday', chrono.parse('Saturday, 10:00', referenceDate, parsingOptions)[0].start.date()],
          ['next_sunday', chrono.parse('Sunday, 15:00', referenceDate, parsingOptions)[0].start.date()]
      ];
wanasit commented 2 years ago

Hello. Thanks for reporting.

What happened was the reference date "August 2, 2022 - Tuesday" (enum date 2)

Calculating "next" based on the date-enum-offset rather than date-window seems like a bug.

However, deciding e.g. what is "Saturday" vs "Next Saturday" has been a difficult edge case, I need some more time to check if it doesn't break any current tests.

wanasit commented 2 years ago

The d0551cd350f00fce11fa7e9fa860e9d93679047d fixes this issue, but there are other changes relating to the day of the week I want to clean up.

It should be released in the new minor version (2.4.0) sometime next week.

Mtillmann commented 2 years ago

@wanasit Thanks a lot for taking the time mate 🙇

wanasit commented 2 years ago

Published. Please update to 2.4.1+