Comparing version 0.2.0 to 0.3.0
146
lib/cron.js
@@ -1,4 +0,15 @@ | ||
function CronTime(time) { | ||
this.source = time; | ||
var timeModuleFound = false; | ||
try { | ||
var time = require("time"); | ||
Date = time.Date; | ||
timeModuleFound = true; | ||
} catch(e) { | ||
//no time module...leave Date alone. :) | ||
} | ||
function CronTime(source, zone) { | ||
this.source = source; | ||
this.zone = zone; | ||
this.second = {}; | ||
@@ -11,6 +22,7 @@ this.minute = {}; | ||
if (!(this.source instanceof Date)) { | ||
if (this.source instanceof Date) { | ||
this.source = new Date(this.source); | ||
this.realDate = true; | ||
} else { | ||
this._parse(); | ||
} else { | ||
this.realDate = true; | ||
} | ||
@@ -33,55 +45,12 @@ }; | ||
var date = (this.source instanceof Date) ? this.source : new Date(); | ||
if (this.zone && date.setTimezone) | ||
date.setTimezone(this.zone); | ||
//add 1 second so next time isn't now (can cause timeout to be 0) | ||
date.setSeconds(date.getSeconds() + 1); | ||
if (!(this.realDate)) { | ||
var i = 1000; | ||
//sanity check | ||
while(--i) { | ||
if (!(date.getMonth() in this.month)) { | ||
date.setMonth(date.getMonth()+1); | ||
date.setDate(1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getDate() in this.dayOfMonth)) { | ||
date.setDate(date.getDate()+1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getDay()+1 in this.dayOfWeek)) { | ||
date.setDate(date.getDate()+1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getHours() in this.hour)) { | ||
date.setHours(date.getHours()+1); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getMinutes() in this.minute)) { | ||
date.setMinutes(date.getMinutes()+1); | ||
date.setSeconds(0); | ||
continue; | ||
} | ||
if(!(date.getSeconds() in this.second)) { | ||
date.setSeconds(date.getSeconds()+1); | ||
continue; | ||
} | ||
break; | ||
} | ||
if (this.realDate) { | ||
return date; | ||
} | ||
return date; | ||
return this._getNextDateFrom(date); | ||
}, | ||
@@ -118,5 +87,62 @@ | ||
/** | ||
* get next date that matches parsed cron time | ||
*/ | ||
_getNextDateFrom: function(start) { | ||
var date = new Date(start); | ||
if (this.zone && date.setTimezone) | ||
date.setTimezone(start.getTimezone()); | ||
//sanity check | ||
var i = 1000; | ||
while(--i) { | ||
var diff = date - start; | ||
if (!(date.getMonth() in this.month)) { | ||
date.setMonth(date.getMonth()+1); | ||
date.setDate(1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getDate() in this.dayOfMonth)) { | ||
date.setDate(date.getDate()+1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getDay()+1 in this.dayOfWeek)) { | ||
date.setDate(date.getDate()+1); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getHours() in this.hour)) { | ||
date.setHours(date.getHours() == 23 && diff > 24*60*60*1000 ? 0 : date.getHours()+1); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getMinutes() in this.minute)) { | ||
date.setMinutes(date.getMinutes() == 59 && diff > 60*60*1000 ? 0 : date.getMinutes()+1); | ||
date.setSeconds(0); | ||
continue; | ||
} | ||
if (!(date.getSeconds() in this.second)) { | ||
date.setSeconds(date.getSeconds() == 59 && diff > 60*1000 ? 0 : date.getSeconds()+1); | ||
continue; | ||
} | ||
break; | ||
} | ||
return date; | ||
}, | ||
/** | ||
* wildcard, or all params in array (for to string) | ||
*/ | ||
_wcOrAll: function(type) { | ||
@@ -209,3 +235,3 @@ if(this._hasAll(type)) return '*'; | ||
function CronJob(cronTime, onTick, onComplete, start) { | ||
function CronJob(cronTime, onTick, onComplete, start, timeZone) { | ||
if (typeof cronTime != "string" && arguments.length == 1) { | ||
@@ -217,2 +243,4 @@ //crontime is an object... | ||
cronTime = cronTime.cronTime; | ||
timeZone = cronTime.timeZone; | ||
if (timeZone && !(Date.setTimezone)) console.log('You specified a Timezone but have not included the `time` module. Timezone functionality is disabled. Please install the `time` module to use Timezones in your application.'); | ||
} | ||
@@ -222,3 +250,3 @@ | ||
this.onComplete = onComplete; | ||
this.cronTime = new CronTime(cronTime); | ||
this.cronTime = new CronTime(cronTime, timeZone); | ||
@@ -292,4 +320,4 @@ this.addCallback(onTick); | ||
exports.time = function(cronTime) { | ||
return new CronTime(cronTime); | ||
exports.time = function(cronTime, timeZone) { | ||
return new CronTime(cronTime, timeZone); | ||
} | ||
@@ -296,0 +324,0 @@ |
{ | ||
"name": "cron", | ||
"description": "CronJob's for your node", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)", | ||
@@ -18,3 +18,4 @@ "bugs" : { | ||
"devDependencies": { | ||
"nodeunit": ">=0.5.4" | ||
"nodeunit": ">=0.5.4", | ||
"time" : "0.8.0" | ||
}, | ||
@@ -28,2 +29,3 @@ "licenses": [ | ||
"contributors": [ | ||
"Romain Beauxis <toots@rastageeks.org> (https://github.com/toots)", | ||
"James Padolsey <> (http://github.com/jamespadolsey)", | ||
@@ -34,4 +36,5 @@ "Finn Herpich <fh@three-heads.de> (http://github.com/ErrorProne)", | ||
"humanchimp <morphcham@gmail.com> (http://github.com/humanchimp)", | ||
"Craig Condon <craig@spiceapps.com> (http://github.com/spiceapps)" | ||
"Craig Condon <craig@spiceapps.com> (http://github.com/spiceapps)", | ||
"Dan Bear <daniel@hulu.com> (http://github.com/danhbear)" | ||
] | ||
} |
@@ -12,9 +12,9 @@ node-cron | ||
Usage: | ||
Usage (basic cron usage): | ||
========== | ||
var cronJob = require('cron').CronJob; | ||
cronJob('* * * * * *', function(){ | ||
new cronJob('* * * * * *', function(){ | ||
console.log('You will see this message every second'); | ||
}, null, true); | ||
}, null, true, "America/Los_Angeles"); | ||
@@ -31,13 +31,30 @@ | ||
Another example | ||
Another cron example | ||
========== | ||
var cronJob = require('cron').CronJob; | ||
var job = cronJob('00 30 11 * * 2-6', function(){ | ||
var job = new cronJob('00 30 11 * * 2-6', function(){ | ||
// Runs every weekday (Monday through Friday) | ||
// at 11:30:00 AM. It does not run on Saturday | ||
// or Sunday. | ||
}); | ||
job.start(); | ||
}, function () { | ||
// This function is executed when the job stops | ||
}, | ||
true /* Start the job right now */, | ||
timeZone /* Time zone of this job. */ | ||
); | ||
Another example with Date | ||
========== | ||
var cronJob = require('cron').CronJob; | ||
var job = new cronJob(new Date(), function(){ | ||
//runs once at the specified date. | ||
}, function () { | ||
// This function is executed when the job stops | ||
}, | ||
true /* Start the job right now */, | ||
timeZone /* Time zone of this job. */ | ||
); | ||
For good measure | ||
@@ -47,3 +64,3 @@ ========== | ||
var cronJob = require('cron').CronJob; | ||
var job = cronJob({ | ||
var job = new cronJob({ | ||
cronTime: '00 30 11 * * 2-6', | ||
@@ -55,3 +72,4 @@ onTick: function() { | ||
}, | ||
start: true | ||
start: false, | ||
timeZone: "America/Los_Angeles" | ||
}); | ||
@@ -65,3 +83,3 @@ job.start(); | ||
try { | ||
cronJob('invalid cron pattern', function() { | ||
new cronJob('invalid cron pattern', function() { | ||
console.log('this should not be printed'); | ||
@@ -73,7 +91,14 @@ }) | ||
Install | ||
========== | ||
From source: `npm install` | ||
From npm: `npm install cron` | ||
If you want to specify timezones, you'll need to install the [time](https://github.com/TooTallNate/node-time) module or place an entry for it in your package.json file. | ||
`npm install time` | ||
API | ||
@@ -102,2 +127,3 @@ ========== | ||
* [Romain Beauxis][toots] | ||
* [James Padolsey][jamespadolsey] | ||
@@ -109,2 +135,3 @@ * [Craig Condon][crcn] | ||
* [humanchimp][humanchimp] | ||
* [danhbear][danhbear] | ||
@@ -117,2 +144,3 @@ License | ||
[toots]:http://github.com/toots | ||
[jamespadolsey]:http://github.com/padolsey | ||
@@ -124,1 +152,2 @@ [crcn]:http://github.com/crcn | ||
[errorprone]:http://github.com/ErrorProne | ||
[danhbear]:http://github.com/danhbear |
@@ -207,2 +207,73 @@ var testCase = require('nodeunit').testCase, | ||
}, | ||
'test a job with a string and a given time zone': function (assert) { | ||
assert.expect(3); | ||
var time = require("time"); | ||
var zone = "America/Chicago"; | ||
// New Orleans time | ||
var d = new time.Date(); | ||
d.setTimezone(zone); | ||
// Current time | ||
t = new time.Date(); | ||
t.setTimezone("America/New_York"); | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.getHours() === d.getHours()) { | ||
zone = "America/Los_Angeles"; | ||
d.setTimezone(zone); | ||
} | ||
assert.notEqual(d.getHours(), t.getHours()); | ||
assert.notEqual(d.getTimezone(), t.getTimezone()); | ||
var seconds = d.getSeconds() + 1; | ||
var c = new cron.CronJob(seconds + ' ' + d.getMinutes() + ' ' + d.getHours() + ' * * *', function(){ | ||
assert.ok(true); | ||
}, undefined, true, zone); | ||
setTimeout(function() { | ||
c.stop(); | ||
assert.done(); | ||
}, 1250); | ||
}, | ||
'test a job with a date and a given time zone': function (assert) { | ||
assert.expect(2); | ||
var time = require("time"); | ||
var zone = "America/Chicago"; | ||
// New Orleans time | ||
var d = new time.Date(); | ||
d.setTimezone(zone); | ||
// Current time | ||
t = new time.Date(); | ||
t.setTimezone("America/New_York"); | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.getHours() === d.getHours()) { | ||
zone = "America/Los_Angeles"; | ||
d.setTimezone(zone); | ||
} | ||
assert.notEqual(d.getHours(), t.getHours()); | ||
if ((58 - t.getSeconds()) <= 0) { | ||
setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000); | ||
} else { | ||
testRun(); | ||
} | ||
function testRun() { | ||
var s = d.getSeconds()+1; | ||
d.setSeconds(s); | ||
var c = new cron.CronJob(d, function() { | ||
assert.ok(true); | ||
}, null, true, zone); | ||
setTimeout(function() { | ||
c.stop(); | ||
assert.done(); | ||
}, 2250); | ||
} | ||
} | ||
}); |
@@ -123,3 +123,16 @@ var testCase = require('nodeunit').testCase, | ||
assert.done(); | ||
}, | ||
'test day roll-over': function(assert) { | ||
var numHours = 24; | ||
assert.expect(numHours * 2); | ||
var ct = new cron.CronTime('0 0 17 * * *'); | ||
for (var hr = 0; hr < numHours; hr++) { | ||
var start = new Date(2012, 3, 16, hr, 30, 30); | ||
var next = ct._getNextDateFrom(start); | ||
assert.ok(next - start < 24*60*60*1000); | ||
assert.ok(next > start); | ||
} | ||
assert.done(); | ||
} | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
43393
10
688
144
0
2