Skip to content

Unexpected behavior with hours range #329

@PierreDemailly

Description

@PierreDemailly

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions