Skip to content

Commit

Permalink
Merge pull request #418 from naoina/master
Browse files Browse the repository at this point in the history
Fix the issue where next execution time is incorrect in some cases
  • Loading branch information
ncb000gt authored Apr 23, 2019
2 parents d3c5b01 + c08522f commit 825696a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@

if (
!(date.date() in this.dayOfMonth) &&
Object.keys(this.dayOfMonth).length !== 31
Object.keys(this.dayOfMonth).length !== 31 &&
!(
date.day() in this.dayOfWeek &&
Object.keys(this.dayOfWeek).length !== 7
)
) {
date.add(1, 'd');
if (date.days() === prevDay) {
Expand All @@ -274,7 +278,11 @@

if (
!(date.day() in this.dayOfWeek) &&
Object.keys(this.dayOfWeek).length !== 7
Object.keys(this.dayOfWeek).length !== 7 &&
!(
date.date() in this.dayOfMonth &&
Object.keys(this.dayOfMonth).length !== 31
)
) {
date.add(1, 'd');
if (date.days() === prevDay) {
Expand Down
24 changes: 24 additions & 0 deletions tests/test-crontime.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ describe('crontime', function() {
new Date(Date.UTC(2019, 1, 1, 0, 0)).valueOf()
);
});
it('should generate the right next day when cron is set to both day of the month and day of the week (1)', function() {
var cronTime = new cron.CronTime('0 8 1 * 4');
var previousDate = new Date(Date.UTC(2019, 3, 22, 0, 0));
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
expect(nextDate.valueOf()).to.equal(
new Date(Date.UTC(2019, 3, 25, 8, 0)).valueOf()
);
});
it('should generate the right next day when cron is set to both day of the month and day of the week (2)', function() {
var cronTime = new cron.CronTime('0 8 1 * 4');
var previousDate = new Date(Date.UTC(2019, 3, 26, 0, 0));
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
expect(nextDate.valueOf()).to.equal(
new Date(Date.UTC(2019, 4, 1, 8, 0)).valueOf()
);
});
it('should generate the right next day when cron is set to both day of the month and day of the week (3)', function() {
var cronTime = new cron.CronTime('0 8 1 * 4');
var previousDate = new Date(Date.UTC(2019, 7, 1, 7, 59));
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
expect(nextDate.valueOf()).to.equal(
new Date(Date.UTC(2019, 7, 1, 8, 0)).valueOf()
);
});

it('should accept 0 as a valid UTC offset', function() {
var clock = sinon.useFakeTimers();
Expand Down

0 comments on commit 825696a

Please sign in to comment.