Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing _getNextDateFrom function logic
  • Loading branch information
jodevsa committed Aug 13, 2018
1 parent 4087ea0 commit 2fdc168
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions lib/cron.js
Expand Up @@ -181,9 +181,21 @@
/**
* get next date that matches parsed cron time
*/
_getNextDateFrom: function(start) {
var date = moment(start);

_getNextDateFrom: function(start,zone) {
var date;
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);
}
}
if (date.toString() == 'Invalid date') {
console.log('ERROR: You specified an invalid date.');
return date;
Expand All @@ -202,24 +214,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) {
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 @@ -229,8 +240,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 @@ -240,8 +250,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 @@ -250,8 +259,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 @@ -260,9 +268,15 @@
continue;
}

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

break;
}

if(origDate.valueOf()==start){
date.seconds(date.seconds()-1);
}
return date;
},

Expand Down

0 comments on commit 2fdc168

Please sign in to comment.