diff --git a/lib/cron.js b/lib/cron.js index 92823fea..32639de1 100644 --- a/lib/cron.js +++ b/lib/cron.js @@ -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; @@ -202,7 +214,7 @@ 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); @@ -210,8 +222,7 @@ 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); @@ -219,7 +230,7 @@ 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); @@ -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); @@ -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); @@ -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) { @@ -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; },