Skip to content

Commit

Permalink
fixed custom time period example
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Scheidel committed Jun 22, 2013
1 parent 08ed92c commit aa8a33c
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 116 deletions.
2 changes: 1 addition & 1 deletion example/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var later = require('../index');
var later = require('../index'); // require('later') if installed via npm


// create the desired schedule
Expand Down
172 changes: 99 additions & 73 deletions example/timeperiod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,105 @@

var later = require('../index');

// create the new time period
later.partOfDay = later.pd = {

name: 'part of day',

range: later.D.range * 6,

val: function(d) {
return later.h.val(d) < 12 ? 0 :
later.h.val(d) < 18 ? 1 :
2;
},

isValid: function(d, val) {
return later.pd.val(d) === val;
},

extent: function(d) { return [0, 2]; },

start: function(d) {
var hour = later.pd.val(d) === 0 ? 0 :
later.pd.val(d) === 1 ? 12 :
18;

return later.date.next(
later.Y.val(d),
later.M.val(d),
later.D.val(d),
hour
);
},

end: function(d) {
var hour = later.pd.val(d) === 0 ? 11 :
later.pd.val(d) === 1 ? 5 :
23;

return later.date.prev(
later.Y.val(d),
later.M.val(d),
later.D.val(d),
hour
);
},

next: function(d, val) {
var hour = val === 0 ? 0 : val === 1 ? 12 : 18;

return later.date.next(
later.Y.val(d),
later.M.val(d),
// increment the day if we already passed the desired time period
later.D.val(d) + (hour < later.h.val(d) ? 1 : 0),
hour
);
},

prev: function(d, val) {
var hour = val === 0 ? 11 : val === 1 ? 5 : 23;

return later.date.prev(
later.Y.val(d),
later.M.val(d),
// decrement the day if we already passed the desired time period
later.D.val(d) + (hour > later.h.val(d) ? -1 : 0),
hour
);
}
};


var sched = {schedules: [{m: [0, 15, 30, 45], pd: [2]}]},
// PartOfDay time period
// 0 = before noon (morning)
// 1 = after noon, before 6pm (afternoon)
// 2 = after 6pm (evening)
later.partOfDay = later.pd = {

// the name of this time period
name: 'part of day',

// the minimum amount of seconds that moving from one value to the next
// value will cover. in this case, the minimum is roughly 6 hours
range: later.h.range * 6,

// return the appropriate val based on the current hour
val: function(d) {
return later.h.val(d) < 12 ? 0 :
later.h.val(d) < 18 ? 1 :
2;
},

// use val(d) to determine if a particular value is valid
isValid: function(d, val) {
return later.pd.val(d) === val;
},

// ours is constant since every day will have the same number of ranges
extent: function(d) { return [0, 2]; },

// start is the first date that has the same val(d) as the d. in our case
// this is either hour 0, 12, or 18 depending on what part of the day we
// are in
start: function(d) {
var hour = later.pd.val(d) === 0 ? 0 :
later.pd.val(d) === 1 ? 12 :
18;

// next is a helper that automatically creates a day in the right timezone
return later.date.next(
later.Y.val(d),
later.M.val(d),
later.D.val(d),
hour
);
},

// end is the last date that has the same val(d) as the d. in our case this
// is the last second of the part of the day we are in
end: function(d) {
var hour = later.pd.val(d) === 0 ? 11 :
later.pd.val(d) === 1 ? 5 :
23;

// prev is a helper that automatically creates a day in the right timezone
// with unspecified date parts set to the maximum (this case will set
// minutes and seconds to 59 for us)
return later.date.prev(
later.Y.val(d),
later.M.val(d),
later.D.val(d),
hour
);
},

// move to the next instance of the specified time of day, noting that it
// may occur on the following day
next: function(d, val) {
var hour = val === 0 ? 0 : val === 1 ? 12 : 18;

return later.date.next(
later.Y.val(d),
later.M.val(d),
// increment the day if we already passed the desired time period
later.D.val(d) + (hour < later.h.val(d) ? 1 : 0),
hour
);
},

// move to the prev instance of the specified time of day, noting that it
// may occur on the previous day
prev: function(d, val) {
var hour = val === 0 ? 11 : val === 1 ? 5 : 23;

return later.date.prev(
later.Y.val(d),
later.M.val(d),
// decrement the day if we already passed the desired time period
later.D.val(d) + (hour > later.h.val(d) ? -1 : 0),
hour
);
}
};

// use our new time period in a schedule
var sched = later.parse.recur().every(15).minute().on(2).customPeriod('pd'),
next = later.schedule(sched).next(5, new Date(2013, 3, 21));

console.log(next);
// Sat, 01 Mar 2014 00:00:00 GMT
//[ Sun Apr 21 2013 18:00:00 GMT,
// Sun Apr 21 2013 18:15:00 GMT,
// Sun Apr 21 2013 18:30:00 GMT,
// Sun Apr 21 2013 18:45:00 GMT,
// Sun Apr 21 2013 19:00:00 GMT ]
21 changes: 13 additions & 8 deletions later-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,7 @@ later = function() {
return this.val(d) === val;
},
extent: constraint.extent,
start: function(d) {
if (constraint.val(d) === value) return d;
return constraint.start(constraint.prev(d, value));
},
start: constraint.start,
end: constraint.end,
next: function(startDate, val) {
if (val > value) val = constraint.extent(startDate)[0];
Expand All @@ -525,10 +522,7 @@ later = function() {
},
extent: constraint.extent,
start: constraint.start,
end: function(d) {
if (constraint.val(d) === value) return d;
return constraint.next(d, value);
},
end: constraint.end,
next: function(startDate, val) {
if (val <= value) val = constraint.extent(startDate)[0];
return constraint.next(startDate, val);
Expand Down Expand Up @@ -556,17 +550,25 @@ later = function() {
return {
start: function(dir, startDate) {
var next = startDate, nextVal = later.array[dir], done;
console.log("start ---------------------");
while (!done && next) {
done = true;
for (var i = 0; i < constraintsLen; i++) {
var constraint = constraints[i].constraint, curVal = constraint.val(next), extent = constraint.extent(next), newVal = nextVal(curVal, constraints[i].vals, extent);
console.log("constraint = " + constraint.name);
console.log("next = " + next.toUTCString());
console.log("curVal = " + curVal);
console.log("extent = " + extent);
console.log("newVal = " + newVal);
console.log("is valid = " + constraint.isValid(next, newVal));
if (!constraint.isValid(next, newVal)) {
next = constraint[dir](next, newVal);
done = false;
break;
}
}
}
console.log("return = " + (next ? tickConstraint.start(next) : undefined));
return next ? tickConstraint.start(next) : undefined;
},
end: function(startDate) {
Expand All @@ -586,6 +588,9 @@ later = function() {
return result;
},
tick: function(dir, date) {
console.log("TICK");
console.log("date=" + date);
console.log("next=" + new Date(tickConstraint.end(date).getTime() + later.SEC));
return new Date(dir === "next" ? tickConstraint.end(date).getTime() + later.SEC : tickConstraint.start(date).getTime() - later.SEC);
}
};
Expand Down
Loading

0 comments on commit aa8a33c

Please sign in to comment.