cron-parser icon indicating copy to clipboard operation
cron-parser copied to clipboard

currentDate is not included in timespan range

Open Eomm opened this issue 1 year ago • 3 comments

Hi, given the following example:

const cronParser = require('cron-parser');

// START DATE NOT INCLUDED
try {
  const intervalCron = cronParser.parseExpression('0 12 * * *', {
    tz: 'UTC',
    currentDate: new Date('2020-02-19T12:00:00.000000+00:00'),
    endDate: new Date('2020-02-19T13:00:00.000000+00:00'),
  });

  const nextRun = intervalCron.next().toDate();
  console.log({ nextRun });
} catch (error) {
  console.error(error.message);
}

// END DATE INCLUDED
const intervalCron = cronParser.parseExpression('0 13 * * *', {
  tz: 'UTC',
  currentDate: new Date('2020-02-19T12:00:00.000000+00:00'),
  endDate: new Date('2020-02-19T13:00:00.000000+00:00'),
});
const nextRun = intervalCron.next().toDate();
console.log({ nextRun });

By using the currentDate and endDate as the timespan range, it seems that the endDate is included while currentDate is not: currentDate < next-cron <= endDate.

I think the currentDate should be included and the first example should work without throwing the Out of the timespan range error.

WDYT?

Eomm avatar Sep 17 '24 14:09 Eomm

Hi @Eomm, thanks for reporting this!

It should be theoretically inclusive range, but there is this workaround to ensure that we won't get stuck into loop without time iteration. This is going to break inclusive range for seconds in that sense. If removed, it will fix your use case and it will return expected behaviour, but will break some others (in the tests).

If you provide * 0 12 * * * (currently it defaults to 0 0 12 * * * if you don't specify seconds), it will also work, but instead of 2020-02-19T12:00:00, you will get 2020-02-19T12:00:01 as nextRun value. Therefore, it will not behave as inclusive range.

Ideally it should also work as inclusive range, but I don't have enough time at the moment to create a proper bugfix (yeah, it's a bug) for that.

Best regards

harrisiirak avatar Sep 17 '24 16:09 harrisiirak

Hey, @harrisiirak This has been open for a while now and my team has hit it as well. Do you have a rough idea for a fix - maybe I could contribute?

Mitko-Kerezov avatar Jun 18 '25 15:06 Mitko-Kerezov

Hi @Mitko-Kerezov!

Haven't had much time to investigate further (trying to survive between two infants and a day job), but It's most likely still present in v5, if you're using the latest version. I was debugging the previous major version (v4).

The code I referred to in my original comment is here. I was recently briefly debugging one another issue, and it seemed that the culprit was also there.

If you have the time and motivation to debug and fix it, any contributions are welcome - I'm more than happy to review them!

Best regards

harrisiirak avatar Jun 18 '25 16:06 harrisiirak