Permalink
Browse files

Couple modifications to "next date" retrieval.

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...
1 parent 22e8984 commit 567e34449a8fd835c340fd89b421afd9e5d2fad7 @ncb000gt ncb000gt committed Apr 2, 2013
Showing with 30 additions and 3 deletions.
  1. +7 −2 lib/cron.js
  2. +23 −1 tests/test-crontime.js
View
@@ -85,13 +85,18 @@ CronTime.prototype = {
*/
_getNextDateFrom: function(start) {
var date = new CronDate(start);
+ //console.log("d: " + date);
if (this.zone && date.setTimezone)
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
- var i = 1000;
- while(--i) {
+ //var i = 1000;
+ while(1) {
var diff = date - start;
+
if (!(date.getMonth() in this.month)) {
date.setMonth(date.getMonth()+1);
View
@@ -154,5 +154,27 @@ module.exports = testCase({
new cron.CronTime('* * /4 * * *');
});
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.