1 | cron-parser
|
2 | ================
|
3 |
|
4 | [![Build Status](https://travis-ci.org/harrisiirak/cron-parser.svg?branch=master)](https://travis-ci.org/harrisiirak/cron-parser)
|
5 | [![NPM version](https://badge.fury.io/js/cron-parser.png)](http://badge.fury.io/js/cron-parser)
|
6 |
|
7 | Node.js library for parsing crontab instructions. It includes support for timezones and DST transitions.
|
8 |
|
9 | Setup
|
10 | ========
|
11 | ```bash
|
12 | npm install cron-parser
|
13 | ```
|
14 |
|
15 | Supported format
|
16 | ========
|
17 |
|
18 | ```
|
19 | * * * * * *
|
20 | ┬ ┬ ┬ ┬ ┬ ┬
|
21 | │ │ │ │ │ |
|
22 | │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
|
23 | │ │ │ │ └───── month (1 - 12)
|
24 | │ │ │ └────────── day of month (1 - 31)
|
25 | │ │ └─────────────── hour (0 - 23)
|
26 | │ └──────────────────── minute (0 - 59)
|
27 | └───────────────────────── second (0 - 59, optional)
|
28 | ```
|
29 |
|
30 | Supports mixed use of ranges and range increments (L, W and # characters are not supported currently). See tests for examples.
|
31 |
|
32 | Usage
|
33 | ========
|
34 |
|
35 | Simple expression.
|
36 |
|
37 | ```javascript
|
38 | var parser = require('cron-parser');
|
39 |
|
40 | try {
|
41 | var interval = parser.parseExpression('*/2 * * * *');
|
42 |
|
43 | console.log('Date: ', interval.next().toString()); // Sat Dec 29 2012 00:42:00 GMT+0200 (EET)
|
44 | console.log('Date: ', interval.next().toString()); // Sat Dec 29 2012 00:44:00 GMT+0200 (EET)
|
45 |
|
46 | console.log('Date: ', interval.prev().toString()); // Sat Dec 29 2012 00:42:00 GMT+0200 (EET)
|
47 | console.log('Date: ', interval.prev().toString()); // Sat Dec 29 2012 00:40:00 GMT+0200 (EET)
|
48 | } catch (err) {
|
49 | console.log('Error: ' + err.message);
|
50 | }
|
51 |
|
52 | ```
|
53 |
|
54 | Iteration with limited timespan. Also returns ES6 compatible iterator (when iterator flag is set to true).
|
55 |
|
56 | ```javascript
|
57 | var parser = require('cron-parser');
|
58 |
|
59 | var options = {
|
60 | currentDate: new Date('Wed, 26 Dec 2012 12:38:53 UTC'),
|
61 | endDate: new Date('Wed, 26 Dec 2012 14:40:00 UTC'),
|
62 | iterator: true
|
63 | };
|
64 |
|
65 | try {
|
66 | var interval = parser.parseExpression('*/22 * * * *', options);
|
67 |
|
68 | while (true) {
|
69 | try {
|
70 | var obj = interval.next();
|
71 | console.log('value:', obj.value.toString(), 'done:', obj.done);
|
72 | } catch (e) {
|
73 | break;
|
74 | }
|
75 | }
|
76 |
|
77 | // value: Wed Dec 26 2012 14:44:00 GMT+0200 (EET) done: false
|
78 | // value: Wed Dec 26 2012 15:00:00 GMT+0200 (EET) done: false
|
79 | // value: Wed Dec 26 2012 15:22:00 GMT+0200 (EET) done: false
|
80 | // value: Wed Dec 26 2012 15:44:00 GMT+0200 (EET) done: false
|
81 | // value: Wed Dec 26 2012 16:00:00 GMT+0200 (EET) done: false
|
82 | // value: Wed Dec 26 2012 16:22:00 GMT+0200 (EET) done: true
|
83 | } catch (err) {
|
84 | console.log('Error: ' + err.message);
|
85 | }
|
86 |
|
87 | ```
|
88 |
|
89 | Timezone support
|
90 |
|
91 | ```javascript
|
92 | var parser = require('cron-parser');
|
93 |
|
94 | var options = {
|
95 | currentDate: '2016-03-27 00:00:01',
|
96 | tz: 'Europe/Athens'
|
97 | };
|
98 |
|
99 | try {
|
100 | var interval = parser.parseExpression('0 * * * *', options);
|
101 |
|
102 | console.log('Date: ', interval.next().toString()); // Date: Sun Mar 27 2016 01:00:00 GMT+0200
|
103 | console.log('Date: ', interval.next().toString()); // Date: Sun Mar 27 2016 02:00:00 GMT+0200
|
104 | console.log('Date: ', interval.next().toString()); // Date: Sun Mar 27 2016 04:00:00 GMT+0300 (Notice DST transition)
|
105 | } catch (err) {
|
106 | console.log('Error: ' + err.message);
|
107 | }
|
108 | ```
|
109 |
|
110 | Options
|
111 | ========
|
112 |
|
113 | * *currentDate* - Start date of the iteration
|
114 | * *endDate* - End date of the iteration
|
115 |
|
116 | `currentDate` and `endDate` accept `string`, `integer` and `Date` as input.
|
117 |
|
118 | In case of using `string` as input, not every string format accepted
|
119 | by the `Date` constructor will work correctly. The supported formats are: [`ISO8601`](http://momentjs.com/docs/#/parsing/string/) and the older
|
120 | [`ASP.NET JSON Date`](http://momentjs.com/docs/#/parsing/asp-net-json-date/) format. The reason being that those are the formats accepted by the
|
121 | [`moment`](http://momentjs.com) library which is being used to handle dates.
|
122 |
|
123 | Using `Date` as an input can be problematic specially when using the `tz` option. The issue being that, when creating a new `Date` object without
|
124 | any timezone information, it will be created in the timezone of the system that is running the code. This (most of times) won't be what the user
|
125 | will be expecting. Using one of the supported `string` formats will solve the issue(see timezone example).
|
126 |
|
127 | * *iterator* - Return ES6 compatible iterator object
|
128 | * *utc* - Enable UTC
|
129 | * *tz* - Timezone string. It won't be used in case `utc` is enabled
|