Skip to content

prev() wrongly skipping back an extra day on first call in specific case of time change #378

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

Open
ellis opened this issue May 8, 2025 · 1 comment
Assignees
Labels

Comments

@ellis
Copy link

ellis commented May 8, 2025

Here is sample code for v5.1.1 that demonstrates the skipped day:

import { CronExpressionParser } from 'cron-parser';
var interval = CronExpressionParser.parse("0 2 * * *", {tz: "America/New_York", currentDate: "2020-03-08T03:01:00.000-04:00"});
console.log(interval.prev().toISOString())
// 2020-03-07T07:00:00.000Z - ERROR: should be 2020-03-08T07:00:00.000Z
console.log(interval.next().toISOString())
// 2020-03-08T07:00:00.000Z

Here is another example where the error is even clearer:

import { CronExpressionParser } from 'cron-parser';
const interval = CronExpressionParser.parse("0 2 * * *", {tz: "America/New_York", currentDate: "2020-03-08T03:01:00.000-04:00"});
console.log(interval.next().toISOString())
// 2020-03-09T06:00:00.000Z
console.log(interval.prev().toISOString())
// 2020-03-07T07:00:00.000Z - ERROR: should be 2020-03-08T07:00:00.000Z

The equivalent code had previously worked in 4.9.0; my unit tests caught the error after I upgraded cron-parser today.

@harrisiirak
Copy link
Owner

harrisiirak commented May 8, 2025

Hi @ellis!

Thanks for reporting this. Strangely enough, v4.9.0 and the latest master (also tested with the initial v5.0.0 release) produce the same faulty result for me.

import { CronExpressionParser } from './src';
import cronParser from './benchmarks/versions/4.9.0/node_modules/cron-parser';

const interval = CronExpressionParser.parse('0 2 * * *', {
  tz: 'America/New_York',
  currentDate: '2020-03-08T03:01:00.000-04:00',
});
console.log(interval.next().toISOString());
console.log(interval.prev().toISOString());

// 2020-03-09T06:00:00.000Z
// 2020-03-07T07:00:00.000Z

const interval2 = cronParser.parseExpression('0 2 * * *', {
  tz: 'America/New_York',
  currentDate: '2020-03-08T03:01:00.000-04:00',
});
console.log(interval2.next().toISOString());
console.log(interval2.prev().toISOString());

// 2020-03-09T06:00:00.000Z
// 2020-03-07T07:00:00.000Z

However, this behavior is rather bizarre and unexpected. I have a feeling it might be related to issue #273, as the current problem also appears to involve DST.

Going to investigate this soon.

Best regards

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

No branches or pull requests

2 participants