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

Fixing _getNextDateFrom function logic #359

Merged
merged 5 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions lib/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,9 @@
return date;
}

//add 1 second so next time isn't now (can cause timeout to be 0 or negative number)
var now = new Date();
var targetSecond = date.seconds();
var diff = Math.abs(targetSecond - now.getSeconds())
// there was a problem when `date` is 1424777177999 and `now` is 1424777178000
// 1 ms diff but this is another second...
if ( diff == 0 || (diff == 1 && now.getMilliseconds() <= date.milliseconds() ) ) {
//console.log('add 1 second?');
date = date.add(1, 's');
}

//If the i argument is not given, return the next send time
if (isNaN(i) || i < 0) {
Expand All @@ -146,8 +139,7 @@
for (; i > 0; i--) {
date = this._getNextDateFrom(date);
dates.push(moment(date));
if (i > 1)
date.add(1,'s');

}

return dates;
Expand Down Expand Up @@ -181,14 +173,22 @@
/**
* get next date that matches parsed cron time
*/
_getNextDateFrom: function(start) {
var date = moment(start);

_getNextDateFrom: function(start,zone) {
var date;
var firstDate=moment(start).valueOf();
if(zone){
date=moment(start).tz(zone);
}
else{
date = moment(start);
}
if (!this.realDate) {
const milliseconds = (start.milliseconds && start.milliseconds()) || (start.getMilliseconds && start.getMilliseconds()) || 0;
if (milliseconds > 0) {
date.milliseconds(0);
date.seconds(date.seconds() + 1);
}
const milliseconds = (start.milliseconds && start.milliseconds()) || (start.getMilliseconds && start.getMilliseconds()) || 0;
if (milliseconds > 0) {
date.milliseconds(0);
date.seconds(date.seconds() + 1);
}
}

if (date.toString() == 'Invalid date') {
Expand All @@ -209,24 +209,23 @@
var diff = date - start,
origDate = new Date(date);

if (!(date.month() in this.month)) {
if (!(date.month() in this.month) && Object.keys(this.month).length!=12) {
date.add(1, 'M');
date.date(1);
date.hours(0);
date.minutes(0);
date.seconds(0);
continue;
}

if (!(date.date() in this.dayOfMonth)) {
if (!(date.date() in this.dayOfMonth) && Object.keys(this.dayOfMonth).length!=31) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some testing to ensure that this wont break months that have less than 31 days?

Copy link
Collaborator Author

@jodevsa jodevsa Aug 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ncb000gt The logic behind this condition Object.keys(this.dayOfMonth).length!=31 is to know whether the dayOfMonth is set to * in the cronString or not. This has nothing to do with the current Date date.date()

date.add(1, 'd');
date.hours(0);
date.minutes(0);
date.seconds(0);
continue;
}

if (!(date.day() in this.dayOfWeek)) {
if (!(date.day() in this.dayOfWeek) && Object.keys(this.dayOfWeek).length!=7) {
date.add(1, 'd');
date.hours(0);
date.minutes(0);
Expand All @@ -236,8 +235,7 @@
}
continue;
}

if (!(date.hours() in this.hour)) {
if (!(date.hours() in this.hour) && Object.keys(this.hour).length!=24) {
origDate = moment(date);
date.hours(date.hours() == 23 && diff > 86400000 ? 0 : date.hours() + 1);
date.minutes(0);
Expand All @@ -247,8 +245,7 @@
}
continue;
}

if (!(date.minutes() in this.minute)) {
if (!(date.minutes() in this.minute) && Object.keys(this.minute).length!=60) {
origDate = moment(date);
date.minutes(date.minutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.minutes() + 1);
date.seconds(0);
Expand All @@ -257,8 +254,7 @@
}
continue;
}

if (!(date.seconds() in this.second)) {
if (!(date.seconds() in this.second) && Object.keys(this.second).length!=60) {
origDate = moment(date);
date.seconds(date.seconds() == 59 && diff > 60 * 1000 ? 0 : date.seconds() + 1);
if (date <= origDate) {
Expand All @@ -267,6 +263,11 @@
continue;
}

if(date.valueOf() === firstDate){
date.seconds(date.seconds()+1);
continue;
}

break;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/test-crontime.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,34 @@ describe('crontime', function () {
var x = cronTime._getNextDateFrom(new Date("2018-08-10T02:19:59.999Z"));
expect(x.toISOString()).to.equal('2018-08-10T02:20:00.000Z');
});

it('should generete the right next days when cron is set to every minute', function () {
var cronTime = new cron.CronTime('* * * * *');
var min=60000;
var previousDate=new Date(Date.UTC(2018,5,3,0,0));
for(var i=0;i<25;i++){
var nextDate = cronTime._getNextDateFrom(previousDate);
expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min)
previousDate=nextDate;
}
});

it('should generete the right next days when cron is set to every 15 min', function () {
var cronTime = new cron.CronTime('*/15 * * * *');
var min=60000*15;
var previousDate=new Date(Date.UTC(2016,6,3,0,0));
for(var i=0;i<25;i++){
var nextDate = cronTime._getNextDateFrom(previousDate);
expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min)
previousDate=nextDate;
}
});

it('should generete the right next day when cron is set to every 15 min in Feb', function () {
var cronTime = new cron.CronTime('*/15 * * FEB *');
var min=60000*15;
var previousDate=new Date(Date.UTC(2018,3,0,0,0));
var nextDate = cronTime._getNextDateFrom(previousDate,"UTC");
expect(nextDate.valueOf()).to.equal(new Date(Date.UTC(2019,1,1,0,0)).valueOf());
});
});