1 | 'use strict';
|
2 |
|
3 | var moment = require('moment-timezone');
|
4 |
|
5 | CronDate.prototype.addYear = function() {
|
6 | this._date.add(1, 'year');
|
7 | };
|
8 |
|
9 | CronDate.prototype.addMonth = function() {
|
10 | this._date.add(1, 'month').startOf('month');
|
11 | };
|
12 |
|
13 | CronDate.prototype.addDay = function() {
|
14 | this._date.add(1, 'day').startOf('day');
|
15 | };
|
16 |
|
17 | CronDate.prototype.addHour = function() {
|
18 | var prev = this.getTime();
|
19 | this._date.add(1, 'hour').startOf('hour');
|
20 | if (this.getTime() <= prev) {
|
21 | this._date.add(1, 'hour');
|
22 | }
|
23 | };
|
24 |
|
25 | CronDate.prototype.addMinute = function() {
|
26 | var prev = this.getTime();
|
27 | this._date.add(1, 'minute').startOf('minute');
|
28 | if (this.getTime() < prev) {
|
29 | this._date.add(1, 'hour');
|
30 | }
|
31 | };
|
32 |
|
33 | CronDate.prototype.addSecond = function() {
|
34 | var prev = this.getTime();
|
35 | this._date.add(1, 'second').startOf('second');
|
36 | if (this.getTime() < prev) {
|
37 | this._date.add(1, 'hour');
|
38 | }
|
39 | };
|
40 |
|
41 | CronDate.prototype.subtractYear = function() {
|
42 | this._date.subtract(1, 'year');
|
43 | };
|
44 |
|
45 | CronDate.prototype.subtractMonth = function() {
|
46 | this._date.subtract(1, 'month').endOf('month');
|
47 | };
|
48 |
|
49 | CronDate.prototype.subtractDay = function() {
|
50 | this._date.subtract(1, 'day').endOf('day');
|
51 | };
|
52 |
|
53 | CronDate.prototype.subtractHour = function() {
|
54 | var prev = this.getTime();
|
55 | this._date.subtract(1, 'hour').endOf('hour');
|
56 | if (this.getTime() >= prev) {
|
57 | this._date.subtract(1, 'hour');
|
58 | }
|
59 | };
|
60 |
|
61 | CronDate.prototype.subtractMinute = function() {
|
62 | var prev = this.getTime();
|
63 | this._date.subtract(1, 'minute').endOf('minute');
|
64 | if (this.getTime() > prev) {
|
65 | this._date.subtract(1, 'hour');
|
66 | }
|
67 | };
|
68 |
|
69 | CronDate.prototype.subtractSecond = function() {
|
70 | var prev = this.getTime();
|
71 | this._date.subtract(1, 'second').startOf('second');
|
72 | if (this.getTime() > prev) {
|
73 | this._date.subtract(1, 'hour');
|
74 | }
|
75 | };
|
76 |
|
77 | CronDate.prototype.getDate = function() {
|
78 | return this._date.date();
|
79 | };
|
80 |
|
81 | CronDate.prototype.getFullYear = function() {
|
82 | return this._date.year();
|
83 | };
|
84 |
|
85 | CronDate.prototype.getDay = function() {
|
86 | return this._date.day();
|
87 | };
|
88 |
|
89 | CronDate.prototype.getMonth = function() {
|
90 | return this._date.month();
|
91 | };
|
92 |
|
93 | CronDate.prototype.getHours = function() {
|
94 | return this._date.hours();
|
95 | };
|
96 |
|
97 | CronDate.prototype.getMinutes = function() {
|
98 | return this._date.minute();
|
99 | };
|
100 |
|
101 | CronDate.prototype.getSeconds = function() {
|
102 | return this._date.second();
|
103 | };
|
104 |
|
105 | CronDate.prototype.getMilliseconds = function() {
|
106 | return this._date.millisecond();
|
107 | };
|
108 |
|
109 | CronDate.prototype.getTime = function() {
|
110 | return this._date.valueOf();
|
111 | };
|
112 |
|
113 | CronDate.prototype.getUTCDate = function() {
|
114 | return this._getUTC().date();
|
115 | };
|
116 |
|
117 | CronDate.prototype.getUTCFullYear = function() {
|
118 | return this._getUTC().year();
|
119 | };
|
120 |
|
121 | CronDate.prototype.getUTCDay = function() {
|
122 | return this._getUTC().day();
|
123 | };
|
124 |
|
125 | CronDate.prototype.getUTCMonth = function() {
|
126 | return this._getUTC().month();
|
127 | };
|
128 |
|
129 | CronDate.prototype.getUTCHours = function() {
|
130 | return this._getUTC().hours();
|
131 | };
|
132 |
|
133 | CronDate.prototype.getUTCMinutes = function() {
|
134 | return this._getUTC().minute();
|
135 | };
|
136 |
|
137 | CronDate.prototype.getUTCSeconds = function() {
|
138 | return this._getUTC().second();
|
139 | };
|
140 |
|
141 | CronDate.prototype.toISOString = function() {
|
142 | return this._date.toISOString();
|
143 | };
|
144 |
|
145 | CronDate.prototype.toJSON = function() {
|
146 | return this._date.toJSON();
|
147 | };
|
148 |
|
149 | CronDate.prototype.setDate = function(d) {
|
150 | return this._date.date(d);
|
151 | };
|
152 |
|
153 | CronDate.prototype.setFullYear = function(y) {
|
154 | return this._date.year(y);
|
155 | };
|
156 |
|
157 | CronDate.prototype.setDay = function(d) {
|
158 | return this._date.day(d);
|
159 | };
|
160 |
|
161 | CronDate.prototype.setMonth = function(m) {
|
162 | return this._date.month(m);
|
163 | };
|
164 |
|
165 | CronDate.prototype.setHours = function(h) {
|
166 | return this._date.hour(h);
|
167 | };
|
168 |
|
169 | CronDate.prototype.setMinutes = function(m) {
|
170 | return this._date.minute(m);
|
171 | };
|
172 |
|
173 | CronDate.prototype.setSeconds = function(s) {
|
174 | return this._date.second(s);
|
175 | };
|
176 |
|
177 | CronDate.prototype.setMilliseconds = function(s) {
|
178 | return this._date.millisecond(s);
|
179 | };
|
180 |
|
181 | CronDate.prototype.getTime = function() {
|
182 | return this._date.valueOf();
|
183 | };
|
184 |
|
185 | CronDate.prototype._getUTC = function() {
|
186 | return moment.utc(this._date);
|
187 | };
|
188 |
|
189 | CronDate.prototype.toString = function() {
|
190 | return this._date.toString();
|
191 | };
|
192 |
|
193 | CronDate.prototype.toDate = function() {
|
194 | return this._date.toDate();
|
195 | };
|
196 |
|
197 | function CronDate (timestamp, tz) {
|
198 | if (timestamp instanceof CronDate) {
|
199 | timestamp = timestamp._date;
|
200 | }
|
201 |
|
202 | if (!tz) {
|
203 | this._date = moment(timestamp);
|
204 | } else {
|
205 | this._date = moment.tz(timestamp, tz);
|
206 | }
|
207 | }
|
208 |
|
209 | module.exports = CronDate;
|