Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

currentDate is not included in timespan range #356

Open
Eomm opened this issue Sep 17, 2024 · 1 comment
Open

currentDate is not included in timespan range #356

Eomm opened this issue Sep 17, 2024 · 1 comment

Comments

@Eomm
Copy link

Eomm commented Sep 17, 2024

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?

@harrisiirak
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants