Skip to content

Commit

Permalink
Couple modifications to "next date" retrieval.
Browse files Browse the repository at this point in the history
In requesting the next date, we try to figure out which date that is.
There were a couple issues with the current implementation.

When you
start with a real date, using that as a "pattern" to find the next date
from is next to useless...no no, it is useless. So, instead of finding
the next date we just check the value and warn the user if its in the
past, otherwise we return it as a new date object (wrapped in a time
Date in case the user has setup timezone info).

When the source is actually a cron pattern, we want to make sure that we
are getting the next appropriate date to actually trigger on. The
original implementation of this code used while(1). This was changed in
8c158dc but I'm not sure what you really get out of it other than it
artificially limits a potential inf. loop (what I don't get is the
potential for it...seems very unlikely).

Added some test cases to check these cases.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed Apr 2, 2013
1 parent 22e8984 commit 567e344
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/cron.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ CronTime.prototype = {
*/ */
_getNextDateFrom: function(start) { _getNextDateFrom: function(start) {
var date = new CronDate(start); var date = new CronDate(start);
//console.log("d: " + date);
if (this.zone && date.setTimezone) if (this.zone && date.setTimezone)
date.setTimezone(start.getTimezone()); date.setTimezone(start.getTimezone());
if (this.realDate && start < new Date())
console.log("WARNING: Date in past. Will never be fired.");
if (this.realDate) return date;


//sanity check //sanity check
var i = 1000; //var i = 1000;
while(--i) { while(1) {
var diff = date - start; var diff = date - start;



if (!(date.getMonth() in this.month)) { if (!(date.getMonth() in this.month)) {
date.setMonth(date.getMonth()+1); date.setMonth(date.getMonth()+1);
Expand Down
24 changes: 23 additions & 1 deletion tests/test-crontime.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -154,5 +154,27 @@ module.exports = testCase({
new cron.CronTime('* * /4 * * *'); new cron.CronTime('* * /4 * * *');
}); });
assert.done(); assert.done();
} },
'test next date': function(assert) {
assert.expect(2);
var ct = new cron.CronTime('0 0 */4 * * *');

var nextDate = new Date();
nextDate.setHours(23);
var nextdt = ct._getNextDateFrom(nextDate);
assert.ok(nextdt > nextDate);
assert.ok(nextdt.getHours() % 4 === 0);
assert.done();
},
'test next real date': function(assert) {
assert.expect(2);
var ct = new cron.CronTime(new Date());

var nextDate = new Date();
nextDate.setMonth(nextDate.getMonth()+1);
assert.ok(nextDate > ct.source);
var nextdt = ct._getNextDateFrom(nextDate);
assert.deepEqual(nextdt, nextDate);
assert.done();
}
}); });

0 comments on commit 567e344

Please sign in to comment.