Skip to content

Commit

Permalink
added sendAt method for CronTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Condon committed Sep 8, 2011
1 parent 6e92a8b commit 4ed1c94
Showing 1 changed file with 110 additions and 20 deletions.
130 changes: 110 additions & 20 deletions lib/cron.js
Expand Up @@ -10,32 +10,109 @@
* - http://www.gnu.org/copyleft/gpl.html
*/



function CronTime(time) {

this.source = time;

this.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
this.constraints = [[0,59],[0,59],[0,23],[1,31],[0,11],[1,7]];
this.aliases = {
jan:0,feb:1,mar:2,apr:3,may:4,jun:5,jul:6,aug:7,sep:8,oct:9,nov:10,dec:11,
sun:1,mon:2,tue:3,wed:4,thu:5,fri:6,sat:7
};

this.second = {};
this.minute = {};
this.hour = {};
this.second = {};
this.minute = {};
this.hour = {};
this.dayOfWeek = {};
this.dayOfMonth = {};
this.month = {};
this.dayOfWeek = {};
this.month = {};

this._parse();

};

CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [1, 7] ];
CronTime.aliases = {
jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11,
sun:1, mon:2, tue:3, wed:4, thu:5, fri:6, sat:7
};


CronTime.prototype = {

/**
* calculates the next send time
*/

sendAt: function(start) {

var date = start ? start : new Date(),
now = {
second: date.getSeconds(),
minute: date.getMinutes(),
hour: date.getHours(),
dayOfWeek: date.getDay() + 1,
dayOfMonth: date.getDate(),
month: date.getMonth(),
year: date.getFullYear()
};


while(1)
{
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);
continue;
}

if(!(date.getSeconds() in this.second)) {
date.setSeconds(date.getSeconds()+1);
continue;
}

break;
}

return date;
},

/**
*/


'timeout': function()
{
return Math.max(0, this.sendAt().getTime() - Date.now());
},


/**
*/

_parse: function() {

var aliases = this.aliases,
var aliases = CronTime.aliases,
source = this.source.replace(/[a-z]{1,3}/ig, function(alias){

alias = alias.toLowerCase();
Expand All @@ -52,10 +129,14 @@ CronTime.prototype = {

while (len--) {
cur = split[len] || '*';
this._parseField(cur, this.map[len], this.constraints[len]);
this._parseField(cur, CronTime.map[len], CronTime.constraints[len]);
}

},

/**
*/

_parseField: function(field, type, constraints) {

var rangePattern = /(\d+?)(?:-(\d+?))?(?:\/(\d+?))?(?:,|$)/g,
Expand Down Expand Up @@ -99,6 +180,15 @@ CronTime.prototype = {
};


var ct = new CronTime('*/2 * * * * *');

var now = new Date();

// now.setDay(6);

console.log(now)
console.log(ct.timeout());

function CronJob(cronTime, event, oncomplete) {

if (!(this instanceof CronJob)) {
Expand All @@ -122,7 +212,7 @@ CronJob.prototype = {
},

runEvents: function() {
for (var i = -1, l = this.events.length; ++i < l; ) {
for (var i = this.events.length; i--;) {
if (typeof this.events[i] === 'function') {
this.events[i](this.oncomplete);
}
Expand All @@ -148,12 +238,12 @@ CronJob.prototype = {

this.timer = this.timer || setInterval(function(){self.clock();}, 1000);

now.second = date.getSeconds();
now.minute = date.getMinutes();
now.hour = date.getHours();
now.second = date.getSeconds();
now.minute = date.getMinutes();
now.hour = date.getHours();
now.dayOfWeek = date.getDay() + 1;
now.dayOfMonth = date.getDate();
now.month = date.getMonth();
now.dayOfWeek = date.getDay() + 1;
now.month = date.getMonth();

for (i in now) {
if (!(now[i] in cronTime[i])) {
Expand Down

0 comments on commit 4ed1c94

Please sign in to comment.