Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/toots/node-cron
Browse files Browse the repository at this point in the history
Conflicts:
	tests/test-cron.js

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed May 25, 2012
2 parents 378eb39 + de1bb1a commit 3285774
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
21 changes: 10 additions & 11 deletions lib/cron.js
@@ -1,10 +1,8 @@
var timeModuleFound = false;
var CronDate = Date;
try {
var time = require("time");
Date = time.Date;
timeModuleFound = true;
CronDate = require("time").Date;
} catch(e) {
//no time module...leave Date alone. :)
//no time module...leave CronDate alone. :)
}


Expand All @@ -19,8 +17,8 @@ function CronTime(source, zone) {
this.dayOfMonth = {};
this.month = {};

if (this.source instanceof Date) {
this.source = new Date(this.source);
if ((this.source instanceof Date) || (this.source instanceof CronDate)) {
this.source = new CronDate(this.source);
this.realDate = true;
} else {
this._parse();
Expand All @@ -40,7 +38,7 @@ CronTime.prototype = {
* calculates the next send time
*/
sendAt: function() {
var date = (this.source instanceof Date) ? this.source : new Date();
var date = (this.source instanceof CronDate) ? this.source : new CronDate();
if (this.zone && date.setTimezone)
date.setTimezone(this.zone);

Expand All @@ -57,7 +55,7 @@ CronTime.prototype = {
* Get the number of seconds in the future at which to fire our callbacks.
*/
getTimeout: function() {
return Math.max(-1, this.sendAt().getTime() - Date.now());
return Math.max(-1, this.sendAt().getTime() - CronDate.now());
},

/**
Expand Down Expand Up @@ -85,7 +83,7 @@ CronTime.prototype = {
* get next date that matches parsed cron time
*/
_getNextDateFrom: function(start) {
var date = new Date(start);
var date = new CronDate(start);
if (this.zone && date.setTimezone)
date.setTimezone(start.getTimezone());

Expand Down Expand Up @@ -237,9 +235,10 @@ function CronJob(cronTime, onTick, onComplete, start, timeZone) {
start = cronTime.start;
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.');
}

if (timeZone && !(CronDate.prototype.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.');

this._callbacks = [];
this.onComplete = onComplete;
this.cronTime = new CronTime(cronTime, timeZone);
Expand Down
33 changes: 19 additions & 14 deletions tests/test-cron.js
Expand Up @@ -211,23 +211,28 @@ module.exports = testCase({
var zone = "America/Chicago";

// New Orleans time
var d = new time.Date();
d.setTimezone(zone);
var t = new time.Date();
t.setTimezone(zone);

// Current time
t = new time.Date();
t.setTimezone("America/New_York");
d = new Date();

// If current time is New Orleans time, switch to Los Angeles..
if (t.getHours() === d.getHours()) {
zone = "America/Los_Angeles";
d.setTimezone(zone);
t.setTimezone(zone);
}
assert.notEqual(d.getHours(), t.getHours());
assert.notEqual(d.getTimezone(), t.getTimezone());
assert.ok(!(Date instanceof time.Date));

var seconds = d.getSeconds() + 1;
var c = new cron.CronJob(seconds + ' ' + d.getMinutes() + ' ' + d.getHours() + ' * * *', function(){
// If t = 59s12m then t.setSeconds(60)
// becones 00s13m so we're fine just doing
// this and no testRun callback.
t.setSeconds(t.getSeconds()+1);
// Run a job designed to be executed at a given
// time in `zone`, making sure that it is a different
// hour than local time.
var c = new cron.CronJob(t.getSeconds() + ' ' + t.getMinutes() + ' ' + t.getHours() + ' * * *', function(){
assert.ok(true);
}, undefined, true, zone);

Expand All @@ -237,25 +242,25 @@ module.exports = testCase({
}, 1250);
},
'test a job with a date and a given time zone': function (assert) {
assert.expect(2);
assert.expect(3);

var time = require("time");
var zone = "America/Chicago";

// New Orleans time
var d = new time.Date();
d.setTimezone(zone);
var t = new time.Date();
t.setTimezone(zone);

// Current time
t = new time.Date();
t.setTimezone("America/New_York");
d = new Date();

// If current time is New Orleans time, switch to Los Angeles..
if (t.getHours() === d.getHours()) {
zone = "America/Los_Angeles";
d.setTimezone(zone);
t.setTimezone(zone);
}
assert.notEqual(d.getHours(), t.getHours());
assert.ok(!(Date instanceof time.Date));

if ((58 - t.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000);
Expand Down

0 comments on commit 3285774

Please sign in to comment.