About
Dateselect is a time-based job scheduler (like cron). The syntax is built on CSS-like selectors, so it's easy to use while still being powerfull.
Dateselect is a David Björklund production.
.
Usage
First, install dateselect with the help of npm:
npm install dateselect
Include dateselect in your module:
var dateselect = require('dateselect'); dateselect("second", callback); var next = dateselect.next("second")
The main method dateselect let's you schedule tasks, while dateselect.next return a Date representing the next occurence.
Syntax
Dateselect uses CSS-like selectors to select time. Dateselect assumes that you describe time like this:
<year class="2011"> <month class="1 january"> <day class="1"> <hour class="1 1am"> <minute class="1"> <second class="1"/> <second class="2"/> etc. <second class="59"/> </minute> <minute class="2"> etc. </minute> etc.
This means that if we want a task to be run once every minute in february we can describe it like this:
dateselect("month.2 minute", function(){ doSomethingFunky(); });
Worth mentioning is that the standard time for dateselect to execute is at january 1 00:00:00 GMT, whick means that if you specify "month.2 minute" is equivalent with "month.2 day.1 minute.1 second.0"
Dateselect supports a non-standard meta-class for recurring events, :nth(). However, be aware that dateselect don't support this month or year. Then you can use day:nth() instead. If we want an event to be run every fifth second, we write
dateselect("second:nth(3)", callback);.
Dateselect also include som special classes:
.xam or .xpm for hour .weekend, .weekday, .monday, .tuesday etc. for days
Think that the names are to long? Dateselect includes some shortcuts
y === year M === month d === day h === hour m === minute s === second
That's all. Look at the examples below, or try it for yourself.
Examples
Let's schedule an alam clock. Notice the use of am for the hour, dateselect supports both 12h clock and 24h clock.
// also availabe is .weekend, .monday, .tuesday etc. dateselect("day.weekday hour.7am minute.30", function(){ console.log("Wake up!"); startTheRadio(); });
We can also use '>' in the above example:
dateselect("day.weekday > .7am > .30", function(){ console.log("Wake up!"); startTheRadio(); });
Set the alarm to ring on saturday and sunday also:
dateselect("day.weekday > .7am > .30, day.weekend > .10 > .15", function(){ console.log("Wake up!"); startTheRadio(); });
dateselect.next return the Date when the next occurence is happening, e.g.
var date = dateselect.next("minute");
Aliases is also availabe:
dateselect("year.2012 month day.4 hour.8 minute.13 second.4", callback); // equivalent with dateselect("y.2012 m d.4 h.8 M.13 s.4", callback);
Try it out
Contribute
Dateselect have a pretty good test coverage, but there could of course be some bugs unnoticed. If you find a bug, please file it and I'll do my best in fixing it.
Dateselect is open source and availabe on github. Feel free to fork the repository and do something funky.
Thanks
I use jison to parse the CSS-selectors and underscore.js for some array-magic. jquery and jsonselect was great for inspiration. This site about dateselect is built with Node.js, socket.io, node-static, 1140 CSS grid, jquery and Google Web Fonts.