UNPKG

4.42 kBJavaScriptView Raw
1'use strict';
2
3var moment = require('moment-timezone');
4
5CronDate.prototype.addYear = function() {
6 this._date.add(1, 'year');
7};
8
9CronDate.prototype.addMonth = function() {
10 this._date.add(1, 'month').startOf('month');
11};
12
13CronDate.prototype.addDay = function() {
14 this._date.add(1, 'day').startOf('day');
15};
16
17CronDate.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
25CronDate.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
33CronDate.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
41CronDate.prototype.subtractYear = function() {
42 this._date.subtract(1, 'year');
43};
44
45CronDate.prototype.subtractMonth = function() {
46 this._date.subtract(1, 'month').endOf('month');
47};
48
49CronDate.prototype.subtractDay = function() {
50 this._date.subtract(1, 'day').endOf('day');
51};
52
53CronDate.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
61CronDate.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
69CronDate.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
77CronDate.prototype.getDate = function() {
78 return this._date.date();
79};
80
81CronDate.prototype.getFullYear = function() {
82 return this._date.year();
83};
84
85CronDate.prototype.getDay = function() {
86 return this._date.day();
87};
88
89CronDate.prototype.getMonth = function() {
90 return this._date.month();
91};
92
93CronDate.prototype.getHours = function() {
94 return this._date.hours();
95};
96
97CronDate.prototype.getMinutes = function() {
98 return this._date.minute();
99};
100
101CronDate.prototype.getSeconds = function() {
102 return this._date.second();
103};
104
105CronDate.prototype.getMilliseconds = function() {
106 return this._date.millisecond();
107};
108
109CronDate.prototype.getTime = function() {
110 return this._date.valueOf();
111};
112
113CronDate.prototype.getUTCDate = function() {
114 return this._getUTC().date();
115};
116
117CronDate.prototype.getUTCFullYear = function() {
118 return this._getUTC().year();
119};
120
121CronDate.prototype.getUTCDay = function() {
122 return this._getUTC().day();
123};
124
125CronDate.prototype.getUTCMonth = function() {
126 return this._getUTC().month();
127};
128
129CronDate.prototype.getUTCHours = function() {
130 return this._getUTC().hours();
131};
132
133CronDate.prototype.getUTCMinutes = function() {
134 return this._getUTC().minute();
135};
136
137CronDate.prototype.getUTCSeconds = function() {
138 return this._getUTC().second();
139};
140
141CronDate.prototype.toISOString = function() {
142 return this._date.toISOString();
143};
144
145CronDate.prototype.toJSON = function() {
146 return this._date.toJSON();
147};
148
149CronDate.prototype.setDate = function(d) {
150 return this._date.date(d);
151};
152
153CronDate.prototype.setFullYear = function(y) {
154 return this._date.year(y);
155};
156
157CronDate.prototype.setDay = function(d) {
158 return this._date.day(d);
159};
160
161CronDate.prototype.setMonth = function(m) {
162 return this._date.month(m);
163};
164
165CronDate.prototype.setHours = function(h) {
166 return this._date.hour(h);
167};
168
169CronDate.prototype.setMinutes = function(m) {
170 return this._date.minute(m);
171};
172
173CronDate.prototype.setSeconds = function(s) {
174 return this._date.second(s);
175};
176
177CronDate.prototype.setMilliseconds = function(s) {
178 return this._date.millisecond(s);
179};
180
181CronDate.prototype.getTime = function() {
182 return this._date.valueOf();
183};
184
185CronDate.prototype._getUTC = function() {
186 return moment.utc(this._date);
187};
188
189CronDate.prototype.toString = function() {
190 return this._date.toString();
191};
192
193CronDate.prototype.toDate = function() {
194 return this._date.toDate();
195};
196
197function 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
209module.exports = CronDate;