harrisiirak / cron-parser

Node.js library for parsing crontab instructions
MIT License
1.33k stars 156 forks source link

Unexpected behavior with hours range #329

Open PierreDemailly opened 1 year ago

PierreDemailly commented 1 year ago

Given a cron expression * 6-20 * * *, at 6:01. I expect .prev() to be 6:00 and then .next() to be 6:01. Actually .prev() is 6:00 as expected but then .next() is 7:00 instead of 6:01.

Reproduction code

import cronParser from 'cron-parser';

const cronExpressions = ['* 6-20 * * *', '* * * * *'];
const testingDate = [
  '01-01-2023 6:00:00',
  '01-01-2023 6:01:00',
  '01-01-2023 6:02:00',
];

for (const cronExpression of cronExpressions) {
  console.log('-----');
  console.log(`Given cron expression [${cronExpression}]`);
  console.log('-----');

  for (const date of testingDate) {
    console.log(`Given date ${date}`);
    const cron = cronParser.parseExpression(cronExpression, {
      currentDate: new Date(date),
    });

    const previous = cron.prev().toString();
    console.log('previous \t', previous);

    const current = cron.next().toString();
    console.log('current \t', current);

    const next = cron.next().toString();
    console.log('next \t\t', next);
  }
}

Given cron expression [* 6-20 * * *]

Given date 01-01-2023 6:00:00 previous .. Sat Dec 31 2022 20:59:00 GMT+0100 current ..... Sun Jan 01 2023 06:00:00 GMT+0100 next .......... Sun Jan 01 2023 06:01:00 GMT+0100 Given date 01-01-2023 6:01:00 previous .. Sun Jan 01 2023 06:00:00 GMT+0100 current ..... Sun Jan 01 2023 07:00:00 GMT+0100 <------------ ? next .......... Sun Jan 01 2023 07:01:00 GMT+0100 Given date 01-01-2023 6:02:00 previous .. Sun Jan 01 2023 06:01:00 GMT+0100 current ..... Sun Jan 01 2023 06:02:00 GMT+0100 next .......... Sun Jan 01 2023 06:03:00 GMT+0100

See this StackBlitz

Can you confirm whether there is a bug on your side or if I do something wrong?

harrisiirak commented 1 year ago

Hi @PierreDemailly!

Thanks for reporting this! Looks like a bug to me. By a quick look, it seems that DST handling craps itself out when mixing prev/next calls and creates an invalid state. Needs some more time (no ETA) for in-depth investigation. I'll later also try to test with current ongoing TS rewrite, as it may have been already resolved over there.