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

interval.prev(): loops infinitely in Brazilian locale #100

Closed
m0nzderr opened this issue Jun 28, 2017 · 6 comments
Closed

interval.prev(): loops infinitely in Brazilian locale #100

m0nzderr opened this issue Jun 28, 2017 · 6 comments
Assignees
Labels

Comments

@m0nzderr
Copy link

m0nzderr commented Jun 28, 2017

In Brazil, when it reaches the value of "2017-10-16T00:59:59.999", the operation

CronDate.prototype.subtractDay = function() {
  this._date.subtract(1, 'day').endOf('day');
};

stays at the same date, causing infinite loop.

see issue moment/moment#3132 :

moment("2017-10-16T00:59:59.999").subtract(1,'day').endOf('day')
=> "2017-10-16T00:59:59.999"

Until the issue is not fixed, I'd suggest to add a simple workaround for it, something like detecting the "no change" situation and forcing the date to change.

@harrisiirak
Copy link
Owner

@m0nzderr thanks for reporting! Can you provide me some example(s)?

Not sure how to reproduce this one. Tried to change the local tz + changed the opts.tz: not getting the infinite loop.

Example code:

const interval = parser.parseExpression('0 0 * * * *', {
  currentDate: '2017-10-16T00:59:59.999',
  tz: 'America/Sao_Paulo'
});

console.log(interval.next().toISOString());
console.log(interval.next().toISOString());
console.log(interval.prev().toISOString());
console.log(interval.next().toISOString());

@m0nzderr
Copy link
Author

@harrisiirak , sorry for taking too long to respond.
Here is one of the cases that loops forever:

const parser = require("cron-parser")
const interval = parser.parseExpression('0 0 0 1 1 *', {
  currentDate: 1498359600000,
  tz: 'America/Sao_Paulo'
});

console.log(interval.next().toISOString());
console.log(interval.prev().toISOString()); // it gets stuck here forever around DST switch
console.log(interval.next().toISOString());
console.log(interval.next().toISOString());

In my production code I had to overcome this problem by overriding your methods with a hack that prevents it from getting stuck on the same date:

CronDate.prototype.subtractDay = function () {
    let before = this._date.date();
    this._date.subtract(1, 'day').endOf('day');
    while (this._date.date() === before) {
        this._date.subtract(1, 'h');
    }
};

CronDate.prototype.addDay = function () {
    let before = this._date.date();
    this._date.add(1, 'day').startOf('day');
    while (this._date.date() === before) {
        this._date.add(1, 'h');
    }
};

@santigimeno
Copy link
Contributor

@m0nzderr I think your patch looks good. Could you send a PR?

@m0nzderr
Copy link
Author

@santigimeno got it!

@harrisiirak
Copy link
Owner

@m0nzderr any updates?

@harrisiirak
Copy link
Owner

Closing this issues as there are no further updates on this.

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

3 participants