ts-time-utils 1.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +567 -12
- package/dist/calculate.d.ts +7 -2
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +13 -3
- package/dist/calendar.d.ts +103 -0
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +224 -0
- package/dist/chain.d.ts +269 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +422 -0
- package/dist/compare.d.ts +217 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +417 -0
- package/dist/cron.d.ts +82 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +294 -0
- package/dist/esm/calculate.d.ts +7 -2
- package/dist/esm/calculate.d.ts.map +1 -1
- package/dist/esm/calculate.js +13 -3
- package/dist/esm/calendar.d.ts +103 -0
- package/dist/esm/calendar.d.ts.map +1 -1
- package/dist/esm/calendar.js +224 -0
- package/dist/esm/chain.d.ts +269 -0
- package/dist/esm/chain.d.ts.map +1 -0
- package/dist/esm/chain.js +422 -0
- package/dist/esm/compare.d.ts +217 -0
- package/dist/esm/compare.d.ts.map +1 -0
- package/dist/esm/compare.js +417 -0
- package/dist/esm/cron.d.ts +82 -0
- package/dist/esm/cron.d.ts.map +1 -0
- package/dist/esm/cron.js +294 -0
- package/dist/esm/fiscal.d.ts +195 -0
- package/dist/esm/fiscal.d.ts.map +1 -0
- package/dist/esm/fiscal.js +295 -0
- package/dist/esm/format.d.ts +65 -0
- package/dist/esm/format.d.ts.map +1 -1
- package/dist/esm/format.js +202 -0
- package/dist/esm/holidays.d.ts +62 -0
- package/dist/esm/holidays.d.ts.map +1 -0
- package/dist/esm/holidays.js +793 -0
- package/dist/esm/index.d.ts +18 -6
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +20 -6
- package/dist/esm/iterate.d.ts +212 -0
- package/dist/esm/iterate.d.ts.map +1 -0
- package/dist/esm/iterate.js +409 -0
- package/dist/esm/parse.d.ts +45 -0
- package/dist/esm/parse.d.ts.map +1 -1
- package/dist/esm/parse.js +207 -0
- package/dist/esm/plugins.d.ts +129 -0
- package/dist/esm/plugins.d.ts.map +1 -0
- package/dist/esm/plugins.js +173 -0
- package/dist/esm/timezone.d.ts +52 -0
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +171 -0
- package/dist/esm/validate.d.ts +51 -0
- package/dist/esm/validate.d.ts.map +1 -1
- package/dist/esm/validate.js +92 -0
- package/dist/esm/workingHours.d.ts +70 -0
- package/dist/esm/workingHours.d.ts.map +1 -1
- package/dist/esm/workingHours.js +161 -0
- package/dist/fiscal.d.ts +195 -0
- package/dist/fiscal.d.ts.map +1 -0
- package/dist/fiscal.js +295 -0
- package/dist/format.d.ts +65 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +202 -0
- package/dist/holidays.d.ts +62 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +793 -0
- package/dist/index.d.ts +18 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -6
- package/dist/iterate.d.ts +212 -0
- package/dist/iterate.d.ts.map +1 -0
- package/dist/iterate.js +409 -0
- package/dist/parse.d.ts +45 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +207 -0
- package/dist/plugins.d.ts +129 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +173 -0
- package/dist/timezone.d.ts +52 -0
- package/dist/timezone.d.ts.map +1 -1
- package/dist/timezone.js +171 -0
- package/dist/validate.d.ts +51 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +92 -0
- package/dist/workingHours.d.ts +70 -0
- package/dist/workingHours.d.ts.map +1 -1
- package/dist/workingHours.js +161 -0
- package/package.json +40 -1
package/dist/cron.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cron expression utilities for scheduling
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Parse a cron expression into its parts
|
|
6
|
+
* @param expression - cron expression (5 fields: minute hour dayOfMonth month dayOfWeek)
|
|
7
|
+
*/
|
|
8
|
+
export function parseCronExpression(expression) {
|
|
9
|
+
const parts = expression.trim().split(/\s+/);
|
|
10
|
+
if (parts.length !== 5) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
minute: parts[0],
|
|
15
|
+
hour: parts[1],
|
|
16
|
+
dayOfMonth: parts[2],
|
|
17
|
+
month: parts[3],
|
|
18
|
+
dayOfWeek: parts[4],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a cron field into its numeric values
|
|
23
|
+
* @param field - cron field string (e.g., "star/5", "1-5", "1,2,3", "*")
|
|
24
|
+
* @param min - minimum valid value
|
|
25
|
+
* @param max - maximum valid value
|
|
26
|
+
*/
|
|
27
|
+
export function parseCronField(field, min, max) {
|
|
28
|
+
const values = [];
|
|
29
|
+
// Handle wildcard
|
|
30
|
+
if (field === '*') {
|
|
31
|
+
for (let i = min; i <= max; i++) {
|
|
32
|
+
values.push(i);
|
|
33
|
+
}
|
|
34
|
+
return { type: 'all', values };
|
|
35
|
+
}
|
|
36
|
+
// Handle step values (*/n or range/n)
|
|
37
|
+
if (field.includes('/')) {
|
|
38
|
+
const [range, stepStr] = field.split('/');
|
|
39
|
+
const step = parseInt(stepStr, 10);
|
|
40
|
+
if (isNaN(step) || step <= 0)
|
|
41
|
+
return null;
|
|
42
|
+
let start = min;
|
|
43
|
+
let end = max;
|
|
44
|
+
if (range !== '*') {
|
|
45
|
+
if (range.includes('-')) {
|
|
46
|
+
const [s, e] = range.split('-').map(Number);
|
|
47
|
+
start = s;
|
|
48
|
+
end = e;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
start = parseInt(range, 10);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (let i = start; i <= end; i += step) {
|
|
55
|
+
values.push(i);
|
|
56
|
+
}
|
|
57
|
+
return { type: 'step', values };
|
|
58
|
+
}
|
|
59
|
+
// Handle range (n-m)
|
|
60
|
+
if (field.includes('-')) {
|
|
61
|
+
const [start, end] = field.split('-').map(Number);
|
|
62
|
+
if (isNaN(start) || isNaN(end) || start > end)
|
|
63
|
+
return null;
|
|
64
|
+
for (let i = start; i <= end; i++) {
|
|
65
|
+
values.push(i);
|
|
66
|
+
}
|
|
67
|
+
return { type: 'range', values };
|
|
68
|
+
}
|
|
69
|
+
// Handle list (n,m,o)
|
|
70
|
+
if (field.includes(',')) {
|
|
71
|
+
const items = field.split(',').map(Number);
|
|
72
|
+
if (items.some(isNaN))
|
|
73
|
+
return null;
|
|
74
|
+
return { type: 'list', values: items.sort((a, b) => a - b) };
|
|
75
|
+
}
|
|
76
|
+
// Handle specific value
|
|
77
|
+
const value = parseInt(field, 10);
|
|
78
|
+
if (isNaN(value) || value < min || value > max)
|
|
79
|
+
return null;
|
|
80
|
+
return { type: 'specific', values: [value] };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if a date matches a cron expression
|
|
84
|
+
* @param date - date to check
|
|
85
|
+
* @param expression - cron expression
|
|
86
|
+
*/
|
|
87
|
+
export function matchesCron(date, expression) {
|
|
88
|
+
const parts = parseCronExpression(expression);
|
|
89
|
+
if (!parts)
|
|
90
|
+
return false;
|
|
91
|
+
const minute = parseCronField(parts.minute, 0, 59);
|
|
92
|
+
const hour = parseCronField(parts.hour, 0, 23);
|
|
93
|
+
const dayOfMonth = parseCronField(parts.dayOfMonth, 1, 31);
|
|
94
|
+
const month = parseCronField(parts.month, 1, 12);
|
|
95
|
+
const dayOfWeek = parseCronField(parts.dayOfWeek, 0, 6);
|
|
96
|
+
if (!minute || !hour || !dayOfMonth || !month || !dayOfWeek) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return (minute.values.includes(date.getMinutes()) &&
|
|
100
|
+
hour.values.includes(date.getHours()) &&
|
|
101
|
+
dayOfMonth.values.includes(date.getDate()) &&
|
|
102
|
+
month.values.includes(date.getMonth() + 1) &&
|
|
103
|
+
dayOfWeek.values.includes(date.getDay()));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the next date that matches a cron expression
|
|
107
|
+
* @param expression - cron expression
|
|
108
|
+
* @param after - start searching after this date (default: now)
|
|
109
|
+
* @param maxIterations - maximum iterations to prevent infinite loops
|
|
110
|
+
*/
|
|
111
|
+
export function getNextCronDate(expression, after = new Date(), maxIterations = 525600 // Max 1 year in minutes
|
|
112
|
+
) {
|
|
113
|
+
const parts = parseCronExpression(expression);
|
|
114
|
+
if (!parts)
|
|
115
|
+
return null;
|
|
116
|
+
const minute = parseCronField(parts.minute, 0, 59);
|
|
117
|
+
const hour = parseCronField(parts.hour, 0, 23);
|
|
118
|
+
const dayOfMonth = parseCronField(parts.dayOfMonth, 1, 31);
|
|
119
|
+
const month = parseCronField(parts.month, 1, 12);
|
|
120
|
+
const dayOfWeek = parseCronField(parts.dayOfWeek, 0, 6);
|
|
121
|
+
if (!minute || !hour || !dayOfMonth || !month || !dayOfWeek) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
const candidate = new Date(after);
|
|
125
|
+
candidate.setSeconds(0, 0);
|
|
126
|
+
candidate.setMinutes(candidate.getMinutes() + 1);
|
|
127
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
128
|
+
if (minute.values.includes(candidate.getMinutes()) &&
|
|
129
|
+
hour.values.includes(candidate.getHours()) &&
|
|
130
|
+
dayOfMonth.values.includes(candidate.getDate()) &&
|
|
131
|
+
month.values.includes(candidate.getMonth() + 1) &&
|
|
132
|
+
dayOfWeek.values.includes(candidate.getDay())) {
|
|
133
|
+
return candidate;
|
|
134
|
+
}
|
|
135
|
+
candidate.setMinutes(candidate.getMinutes() + 1);
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get the N next dates that match a cron expression
|
|
141
|
+
* @param expression - cron expression
|
|
142
|
+
* @param count - number of dates to get
|
|
143
|
+
* @param after - start searching after this date
|
|
144
|
+
*/
|
|
145
|
+
export function getNextCronDates(expression, count, after = new Date()) {
|
|
146
|
+
const dates = [];
|
|
147
|
+
let currentAfter = after;
|
|
148
|
+
for (let i = 0; i < count; i++) {
|
|
149
|
+
const next = getNextCronDate(expression, currentAfter);
|
|
150
|
+
if (!next)
|
|
151
|
+
break;
|
|
152
|
+
dates.push(next);
|
|
153
|
+
currentAfter = next;
|
|
154
|
+
}
|
|
155
|
+
return dates;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Get the previous date that matched a cron expression
|
|
159
|
+
* @param expression - cron expression
|
|
160
|
+
* @param before - start searching before this date
|
|
161
|
+
* @param maxIterations - maximum iterations to prevent infinite loops
|
|
162
|
+
*/
|
|
163
|
+
export function getPreviousCronDate(expression, before = new Date(), maxIterations = 525600) {
|
|
164
|
+
const parts = parseCronExpression(expression);
|
|
165
|
+
if (!parts)
|
|
166
|
+
return null;
|
|
167
|
+
const minute = parseCronField(parts.minute, 0, 59);
|
|
168
|
+
const hour = parseCronField(parts.hour, 0, 23);
|
|
169
|
+
const dayOfMonth = parseCronField(parts.dayOfMonth, 1, 31);
|
|
170
|
+
const month = parseCronField(parts.month, 1, 12);
|
|
171
|
+
const dayOfWeek = parseCronField(parts.dayOfWeek, 0, 6);
|
|
172
|
+
if (!minute || !hour || !dayOfMonth || !month || !dayOfWeek) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
const candidate = new Date(before);
|
|
176
|
+
candidate.setSeconds(0, 0);
|
|
177
|
+
candidate.setMinutes(candidate.getMinutes() - 1);
|
|
178
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
179
|
+
if (minute.values.includes(candidate.getMinutes()) &&
|
|
180
|
+
hour.values.includes(candidate.getHours()) &&
|
|
181
|
+
dayOfMonth.values.includes(candidate.getDate()) &&
|
|
182
|
+
month.values.includes(candidate.getMonth() + 1) &&
|
|
183
|
+
dayOfWeek.values.includes(candidate.getDay())) {
|
|
184
|
+
return candidate;
|
|
185
|
+
}
|
|
186
|
+
candidate.setMinutes(candidate.getMinutes() - 1);
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Validate a cron expression
|
|
192
|
+
* @param expression - cron expression to validate
|
|
193
|
+
*/
|
|
194
|
+
export function isValidCron(expression) {
|
|
195
|
+
const parts = parseCronExpression(expression);
|
|
196
|
+
if (!parts)
|
|
197
|
+
return false;
|
|
198
|
+
const minute = parseCronField(parts.minute, 0, 59);
|
|
199
|
+
const hour = parseCronField(parts.hour, 0, 23);
|
|
200
|
+
const dayOfMonth = parseCronField(parts.dayOfMonth, 1, 31);
|
|
201
|
+
const month = parseCronField(parts.month, 1, 12);
|
|
202
|
+
const dayOfWeek = parseCronField(parts.dayOfWeek, 0, 6);
|
|
203
|
+
return !!(minute && hour && dayOfMonth && month && dayOfWeek);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Convert a cron expression to a human-readable description
|
|
207
|
+
* @param expression - cron expression
|
|
208
|
+
*/
|
|
209
|
+
export function describeCron(expression) {
|
|
210
|
+
const parts = parseCronExpression(expression);
|
|
211
|
+
if (!parts)
|
|
212
|
+
return null;
|
|
213
|
+
const descriptions = [];
|
|
214
|
+
// Handle common patterns
|
|
215
|
+
if (expression === '* * * * *') {
|
|
216
|
+
return 'Every minute';
|
|
217
|
+
}
|
|
218
|
+
if (expression === '0 * * * *') {
|
|
219
|
+
return 'Every hour';
|
|
220
|
+
}
|
|
221
|
+
if (expression === '0 0 * * *') {
|
|
222
|
+
return 'Every day at midnight';
|
|
223
|
+
}
|
|
224
|
+
if (expression === '0 0 * * 0') {
|
|
225
|
+
return 'Every Sunday at midnight';
|
|
226
|
+
}
|
|
227
|
+
if (expression === '0 0 1 * *') {
|
|
228
|
+
return 'First day of every month at midnight';
|
|
229
|
+
}
|
|
230
|
+
// Build description
|
|
231
|
+
const minute = parts.minute;
|
|
232
|
+
const hour = parts.hour;
|
|
233
|
+
if (minute === '0' && hour !== '*') {
|
|
234
|
+
if (hour.includes('/')) {
|
|
235
|
+
const step = hour.split('/')[1];
|
|
236
|
+
descriptions.push(`Every ${step} hours`);
|
|
237
|
+
}
|
|
238
|
+
else if (hour.includes('-')) {
|
|
239
|
+
descriptions.push(`Every hour from ${hour} at minute 0`);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
descriptions.push(`At ${hour}:00`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (minute !== '*' && hour === '*') {
|
|
246
|
+
descriptions.push(`At minute ${minute} of every hour`);
|
|
247
|
+
}
|
|
248
|
+
else if (minute.includes('/')) {
|
|
249
|
+
const step = minute.split('/')[1];
|
|
250
|
+
descriptions.push(`Every ${step} minutes`);
|
|
251
|
+
}
|
|
252
|
+
if (parts.dayOfMonth !== '*') {
|
|
253
|
+
descriptions.push(`on day ${parts.dayOfMonth} of the month`);
|
|
254
|
+
}
|
|
255
|
+
if (parts.month !== '*') {
|
|
256
|
+
const monthNames = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
257
|
+
const monthNum = parseInt(parts.month, 10);
|
|
258
|
+
if (!isNaN(monthNum) && monthNum >= 1 && monthNum <= 12) {
|
|
259
|
+
descriptions.push(`in ${monthNames[monthNum]}`);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
descriptions.push(`in month ${parts.month}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (parts.dayOfWeek !== '*') {
|
|
266
|
+
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
267
|
+
const dayNum = parseInt(parts.dayOfWeek, 10);
|
|
268
|
+
if (!isNaN(dayNum) && dayNum >= 0 && dayNum <= 6) {
|
|
269
|
+
descriptions.push(`on ${dayNames[dayNum]}`);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
descriptions.push(`on day of week ${parts.dayOfWeek}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return descriptions.join(' ') || expression;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Common cron expressions
|
|
279
|
+
*/
|
|
280
|
+
export const CRON_PRESETS = {
|
|
281
|
+
everyMinute: '* * * * *',
|
|
282
|
+
everyHour: '0 * * * *',
|
|
283
|
+
everyDay: '0 0 * * *',
|
|
284
|
+
everyDayAt9am: '0 9 * * *',
|
|
285
|
+
everyDayAt6pm: '0 18 * * *',
|
|
286
|
+
everyWeek: '0 0 * * 0',
|
|
287
|
+
everyMonth: '0 0 1 * *',
|
|
288
|
+
everyYear: '0 0 1 1 *',
|
|
289
|
+
weekdays: '0 0 * * 1-5',
|
|
290
|
+
weekends: '0 0 * * 0,6',
|
|
291
|
+
every5Minutes: '*/5 * * * *',
|
|
292
|
+
every15Minutes: '*/15 * * * *',
|
|
293
|
+
every30Minutes: '*/30 * * * *',
|
|
294
|
+
};
|
package/dist/esm/calculate.d.ts
CHANGED
|
@@ -37,9 +37,14 @@ export declare function endOf(date: Date, unit: 'day' | 'week' | 'month' | 'year
|
|
|
37
37
|
* Check if a date is between two other dates
|
|
38
38
|
* @param date - date to check
|
|
39
39
|
* @param start - start date (inclusive)
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Check if a date falls between two dates
|
|
42
|
+
* @param date - date to check
|
|
43
|
+
* @param start - start date
|
|
44
|
+
* @param end - end date
|
|
45
|
+
* @param inclusive - whether boundaries are inclusive (default: true)
|
|
41
46
|
*/
|
|
42
|
-
export declare function isBetween(date: Date, start: Date, end: Date): boolean;
|
|
47
|
+
export declare function isBetween(date: Date, start: Date, end: Date, inclusive?: boolean): boolean;
|
|
43
48
|
/**
|
|
44
49
|
* Get the number of business days between two dates (excludes weekends)
|
|
45
50
|
* @param startDate - start date
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculate.d.ts","sourceRoot":"","sources":["../../src/calculate.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,IAAI,EACX,KAAK,EAAE,IAAI,EACX,IAAI,GAAE,QAAyB,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAgDxE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BrG;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BnG;AAED
|
|
1
|
+
{"version":3,"file":"calculate.d.ts","sourceRoot":"","sources":["../../src/calculate.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,IAAI,EACX,KAAK,EAAE,IAAI,EACX,IAAI,GAAE,QAAyB,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAgDxE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BrG;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BnG;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,GAAE,OAAc,GAAG,OAAO,CAShG;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,MAAM,CAa1E"}
|
package/dist/esm/calculate.js
CHANGED
|
@@ -168,11 +168,21 @@ export function endOf(date, unit) {
|
|
|
168
168
|
* Check if a date is between two other dates
|
|
169
169
|
* @param date - date to check
|
|
170
170
|
* @param start - start date (inclusive)
|
|
171
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Check if a date falls between two dates
|
|
173
|
+
* @param date - date to check
|
|
174
|
+
* @param start - start date
|
|
175
|
+
* @param end - end date
|
|
176
|
+
* @param inclusive - whether boundaries are inclusive (default: true)
|
|
172
177
|
*/
|
|
173
|
-
export function isBetween(date, start, end) {
|
|
178
|
+
export function isBetween(date, start, end, inclusive = true) {
|
|
174
179
|
const time = date.getTime();
|
|
175
|
-
|
|
180
|
+
const startTime = start.getTime();
|
|
181
|
+
const endTime = end.getTime();
|
|
182
|
+
if (inclusive) {
|
|
183
|
+
return time >= startTime && time <= endTime;
|
|
184
|
+
}
|
|
185
|
+
return time > startTime && time < endTime;
|
|
176
186
|
}
|
|
177
187
|
/**
|
|
178
188
|
* Get the number of business days between two dates (excludes weekends)
|
package/dist/esm/calendar.d.ts
CHANGED
|
@@ -79,4 +79,107 @@ export declare function getFirstDayOfYear(year: number): Date;
|
|
|
79
79
|
* @param year - year
|
|
80
80
|
*/
|
|
81
81
|
export declare function getLastDayOfYear(year: number): Date;
|
|
82
|
+
/**
|
|
83
|
+
* Get the nth occurrence of a day in a month (e.g., 2nd Monday)
|
|
84
|
+
* @param year - year
|
|
85
|
+
* @param month - month (0-11)
|
|
86
|
+
* @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
|
|
87
|
+
* @param n - occurrence (1-5, or -1 for last)
|
|
88
|
+
*/
|
|
89
|
+
export declare function getNthDayOfMonth(year: number, month: number, dayOfWeek: number, n: number): Date | null;
|
|
90
|
+
/**
|
|
91
|
+
* US Holiday type
|
|
92
|
+
*/
|
|
93
|
+
export interface USHoliday {
|
|
94
|
+
name: string;
|
|
95
|
+
date: Date;
|
|
96
|
+
type: 'federal' | 'observance';
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get New Year's Day
|
|
100
|
+
* @param year - year
|
|
101
|
+
*/
|
|
102
|
+
export declare function getNewYearsDay(year: number): Date;
|
|
103
|
+
/**
|
|
104
|
+
* Get Martin Luther King Jr. Day (3rd Monday of January)
|
|
105
|
+
* @param year - year
|
|
106
|
+
*/
|
|
107
|
+
export declare function getMLKDay(year: number): Date | null;
|
|
108
|
+
/**
|
|
109
|
+
* Get Presidents' Day (3rd Monday of February)
|
|
110
|
+
* @param year - year
|
|
111
|
+
*/
|
|
112
|
+
export declare function getPresidentsDay(year: number): Date | null;
|
|
113
|
+
/**
|
|
114
|
+
* Get Memorial Day (last Monday of May)
|
|
115
|
+
* @param year - year
|
|
116
|
+
*/
|
|
117
|
+
export declare function getMemorialDay(year: number): Date | null;
|
|
118
|
+
/**
|
|
119
|
+
* Get Independence Day (July 4th)
|
|
120
|
+
* @param year - year
|
|
121
|
+
*/
|
|
122
|
+
export declare function getIndependenceDay(year: number): Date;
|
|
123
|
+
/**
|
|
124
|
+
* Get Labor Day (1st Monday of September)
|
|
125
|
+
* @param year - year
|
|
126
|
+
*/
|
|
127
|
+
export declare function getLaborDay(year: number): Date | null;
|
|
128
|
+
/**
|
|
129
|
+
* Get Columbus Day (2nd Monday of October)
|
|
130
|
+
* @param year - year
|
|
131
|
+
*/
|
|
132
|
+
export declare function getColumbusDay(year: number): Date | null;
|
|
133
|
+
/**
|
|
134
|
+
* Get Veterans Day (November 11th)
|
|
135
|
+
* @param year - year
|
|
136
|
+
*/
|
|
137
|
+
export declare function getVeteransDay(year: number): Date;
|
|
138
|
+
/**
|
|
139
|
+
* Get Thanksgiving Day (4th Thursday of November)
|
|
140
|
+
* @param year - year
|
|
141
|
+
*/
|
|
142
|
+
export declare function getThanksgivingDay(year: number): Date | null;
|
|
143
|
+
/**
|
|
144
|
+
* Get Christmas Day (December 25th)
|
|
145
|
+
* @param year - year
|
|
146
|
+
*/
|
|
147
|
+
export declare function getChristmasDay(year: number): Date;
|
|
148
|
+
/**
|
|
149
|
+
* Get Good Friday (Friday before Easter)
|
|
150
|
+
* @param year - year
|
|
151
|
+
*/
|
|
152
|
+
export declare function getGoodFriday(year: number): Date;
|
|
153
|
+
/**
|
|
154
|
+
* Get all US federal holidays for a year
|
|
155
|
+
* @param year - year
|
|
156
|
+
*/
|
|
157
|
+
export declare function getUSHolidays(year: number): USHoliday[];
|
|
158
|
+
/**
|
|
159
|
+
* Check if a date is a US federal holiday
|
|
160
|
+
* @param date - date to check
|
|
161
|
+
*/
|
|
162
|
+
export declare function isUSHoliday(date: Date): boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Get the name of a US holiday for a given date
|
|
165
|
+
* @param date - date to check
|
|
166
|
+
* @returns holiday name or null if not a holiday
|
|
167
|
+
*/
|
|
168
|
+
export declare function getUSHolidayName(date: Date): string | null;
|
|
169
|
+
/**
|
|
170
|
+
* Get the start of the week for a date (Monday)
|
|
171
|
+
* @param date - any date
|
|
172
|
+
*/
|
|
173
|
+
export declare function getStartOfWeek(date: Date): Date;
|
|
174
|
+
/**
|
|
175
|
+
* Get the end of the week for a date (Sunday)
|
|
176
|
+
* @param date - any date
|
|
177
|
+
*/
|
|
178
|
+
export declare function getEndOfWeek(date: Date): Date;
|
|
179
|
+
/**
|
|
180
|
+
* Get all weeks in a month as arrays of dates
|
|
181
|
+
* @param year - year
|
|
182
|
+
* @param month - month (0-11)
|
|
183
|
+
*/
|
|
184
|
+
export declare function getWeeksInMonth(year: number, month: number): Date[][];
|
|
82
185
|
//# sourceMappingURL=calendar.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../../src/calendar.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAI/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,a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AED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,EAAE,CAkCrE"}
|
package/dist/esm/calendar.js
CHANGED
|
@@ -152,3 +152,227 @@ export function getFirstDayOfYear(year) {
|
|
|
152
152
|
export function getLastDayOfYear(year) {
|
|
153
153
|
return new Date(year, 11, 31);
|
|
154
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Get the nth occurrence of a day in a month (e.g., 2nd Monday)
|
|
157
|
+
* @param year - year
|
|
158
|
+
* @param month - month (0-11)
|
|
159
|
+
* @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
|
|
160
|
+
* @param n - occurrence (1-5, or -1 for last)
|
|
161
|
+
*/
|
|
162
|
+
export function getNthDayOfMonth(year, month, dayOfWeek, n) {
|
|
163
|
+
if (n === 0 || n < -1 || n > 5)
|
|
164
|
+
return null;
|
|
165
|
+
const firstDay = new Date(year, month, 1);
|
|
166
|
+
const lastDay = new Date(year, month + 1, 0);
|
|
167
|
+
if (n === -1) {
|
|
168
|
+
// Last occurrence
|
|
169
|
+
let date = lastDay.getDate();
|
|
170
|
+
while (date > 0) {
|
|
171
|
+
const d = new Date(year, month, date);
|
|
172
|
+
if (d.getDay() === dayOfWeek)
|
|
173
|
+
return d;
|
|
174
|
+
date--;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
// Find nth occurrence
|
|
179
|
+
let count = 0;
|
|
180
|
+
for (let date = 1; date <= lastDay.getDate(); date++) {
|
|
181
|
+
const d = new Date(year, month, date);
|
|
182
|
+
if (d.getDay() === dayOfWeek) {
|
|
183
|
+
count++;
|
|
184
|
+
if (count === n)
|
|
185
|
+
return d;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get New Year's Day
|
|
192
|
+
* @param year - year
|
|
193
|
+
*/
|
|
194
|
+
export function getNewYearsDay(year) {
|
|
195
|
+
return new Date(year, 0, 1);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get Martin Luther King Jr. Day (3rd Monday of January)
|
|
199
|
+
* @param year - year
|
|
200
|
+
*/
|
|
201
|
+
export function getMLKDay(year) {
|
|
202
|
+
return getNthDayOfMonth(year, 0, 1, 3);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Get Presidents' Day (3rd Monday of February)
|
|
206
|
+
* @param year - year
|
|
207
|
+
*/
|
|
208
|
+
export function getPresidentsDay(year) {
|
|
209
|
+
return getNthDayOfMonth(year, 1, 1, 3);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Get Memorial Day (last Monday of May)
|
|
213
|
+
* @param year - year
|
|
214
|
+
*/
|
|
215
|
+
export function getMemorialDay(year) {
|
|
216
|
+
return getNthDayOfMonth(year, 4, 1, -1);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Get Independence Day (July 4th)
|
|
220
|
+
* @param year - year
|
|
221
|
+
*/
|
|
222
|
+
export function getIndependenceDay(year) {
|
|
223
|
+
return new Date(year, 6, 4);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get Labor Day (1st Monday of September)
|
|
227
|
+
* @param year - year
|
|
228
|
+
*/
|
|
229
|
+
export function getLaborDay(year) {
|
|
230
|
+
return getNthDayOfMonth(year, 8, 1, 1);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get Columbus Day (2nd Monday of October)
|
|
234
|
+
* @param year - year
|
|
235
|
+
*/
|
|
236
|
+
export function getColumbusDay(year) {
|
|
237
|
+
return getNthDayOfMonth(year, 9, 1, 2);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Get Veterans Day (November 11th)
|
|
241
|
+
* @param year - year
|
|
242
|
+
*/
|
|
243
|
+
export function getVeteransDay(year) {
|
|
244
|
+
return new Date(year, 10, 11);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Get Thanksgiving Day (4th Thursday of November)
|
|
248
|
+
* @param year - year
|
|
249
|
+
*/
|
|
250
|
+
export function getThanksgivingDay(year) {
|
|
251
|
+
return getNthDayOfMonth(year, 10, 4, 4);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Get Christmas Day (December 25th)
|
|
255
|
+
* @param year - year
|
|
256
|
+
*/
|
|
257
|
+
export function getChristmasDay(year) {
|
|
258
|
+
return new Date(year, 11, 25);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Get Good Friday (Friday before Easter)
|
|
262
|
+
* @param year - year
|
|
263
|
+
*/
|
|
264
|
+
export function getGoodFriday(year) {
|
|
265
|
+
const easter = getEaster(year);
|
|
266
|
+
const goodFriday = new Date(easter);
|
|
267
|
+
goodFriday.setDate(easter.getDate() - 2);
|
|
268
|
+
return goodFriday;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Get all US federal holidays for a year
|
|
272
|
+
* @param year - year
|
|
273
|
+
*/
|
|
274
|
+
export function getUSHolidays(year) {
|
|
275
|
+
const holidays = [];
|
|
276
|
+
holidays.push({ name: "New Year's Day", date: getNewYearsDay(year), type: 'federal' });
|
|
277
|
+
const mlk = getMLKDay(year);
|
|
278
|
+
if (mlk)
|
|
279
|
+
holidays.push({ name: "Martin Luther King Jr. Day", date: mlk, type: 'federal' });
|
|
280
|
+
const presidents = getPresidentsDay(year);
|
|
281
|
+
if (presidents)
|
|
282
|
+
holidays.push({ name: "Presidents' Day", date: presidents, type: 'federal' });
|
|
283
|
+
const memorial = getMemorialDay(year);
|
|
284
|
+
if (memorial)
|
|
285
|
+
holidays.push({ name: "Memorial Day", date: memorial, type: 'federal' });
|
|
286
|
+
holidays.push({ name: "Independence Day", date: getIndependenceDay(year), type: 'federal' });
|
|
287
|
+
const labor = getLaborDay(year);
|
|
288
|
+
if (labor)
|
|
289
|
+
holidays.push({ name: "Labor Day", date: labor, type: 'federal' });
|
|
290
|
+
const columbus = getColumbusDay(year);
|
|
291
|
+
if (columbus)
|
|
292
|
+
holidays.push({ name: "Columbus Day", date: columbus, type: 'federal' });
|
|
293
|
+
holidays.push({ name: "Veterans Day", date: getVeteransDay(year), type: 'federal' });
|
|
294
|
+
const thanksgiving = getThanksgivingDay(year);
|
|
295
|
+
if (thanksgiving)
|
|
296
|
+
holidays.push({ name: "Thanksgiving Day", date: thanksgiving, type: 'federal' });
|
|
297
|
+
holidays.push({ name: "Christmas Day", date: getChristmasDay(year), type: 'federal' });
|
|
298
|
+
return holidays.sort((a, b) => a.date.getTime() - b.date.getTime());
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Check if a date is a US federal holiday
|
|
302
|
+
* @param date - date to check
|
|
303
|
+
*/
|
|
304
|
+
export function isUSHoliday(date) {
|
|
305
|
+
const holidays = getUSHolidays(date.getFullYear());
|
|
306
|
+
return holidays.some(h => h.date.getFullYear() === date.getFullYear() &&
|
|
307
|
+
h.date.getMonth() === date.getMonth() &&
|
|
308
|
+
h.date.getDate() === date.getDate());
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Get the name of a US holiday for a given date
|
|
312
|
+
* @param date - date to check
|
|
313
|
+
* @returns holiday name or null if not a holiday
|
|
314
|
+
*/
|
|
315
|
+
export function getUSHolidayName(date) {
|
|
316
|
+
const holidays = getUSHolidays(date.getFullYear());
|
|
317
|
+
const holiday = holidays.find(h => h.date.getFullYear() === date.getFullYear() &&
|
|
318
|
+
h.date.getMonth() === date.getMonth() &&
|
|
319
|
+
h.date.getDate() === date.getDate());
|
|
320
|
+
return holiday ? holiday.name : null;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Get the start of the week for a date (Monday)
|
|
324
|
+
* @param date - any date
|
|
325
|
+
*/
|
|
326
|
+
export function getStartOfWeek(date) {
|
|
327
|
+
const d = new Date(date);
|
|
328
|
+
const day = d.getDay();
|
|
329
|
+
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
|
|
330
|
+
d.setDate(diff);
|
|
331
|
+
d.setHours(0, 0, 0, 0);
|
|
332
|
+
return d;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Get the end of the week for a date (Sunday)
|
|
336
|
+
* @param date - any date
|
|
337
|
+
*/
|
|
338
|
+
export function getEndOfWeek(date) {
|
|
339
|
+
const start = getStartOfWeek(date);
|
|
340
|
+
const end = new Date(start);
|
|
341
|
+
end.setDate(start.getDate() + 6);
|
|
342
|
+
end.setHours(23, 59, 59, 999);
|
|
343
|
+
return end;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get all weeks in a month as arrays of dates
|
|
347
|
+
* @param year - year
|
|
348
|
+
* @param month - month (0-11)
|
|
349
|
+
*/
|
|
350
|
+
export function getWeeksInMonth(year, month) {
|
|
351
|
+
const weeks = [];
|
|
352
|
+
const firstDay = new Date(year, month, 1);
|
|
353
|
+
const lastDay = new Date(year, month + 1, 0);
|
|
354
|
+
let currentWeek = [];
|
|
355
|
+
const startDayOfWeek = firstDay.getDay() || 7; // Convert Sunday from 0 to 7 for ISO
|
|
356
|
+
// Add days from previous month to fill the first week
|
|
357
|
+
for (let i = 1; i < startDayOfWeek; i++) {
|
|
358
|
+
const prevDate = new Date(year, month, 1 - (startDayOfWeek - i));
|
|
359
|
+
currentWeek.push(prevDate);
|
|
360
|
+
}
|
|
361
|
+
// Add days of the current month
|
|
362
|
+
for (let day = 1; day <= lastDay.getDate(); day++) {
|
|
363
|
+
currentWeek.push(new Date(year, month, day));
|
|
364
|
+
if (currentWeek.length === 7) {
|
|
365
|
+
weeks.push(currentWeek);
|
|
366
|
+
currentWeek = [];
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// Add days from next month to fill the last week
|
|
370
|
+
if (currentWeek.length > 0) {
|
|
371
|
+
let nextDay = 1;
|
|
372
|
+
while (currentWeek.length < 7) {
|
|
373
|
+
currentWeek.push(new Date(year, month + 1, nextDay++));
|
|
374
|
+
}
|
|
375
|
+
weeks.push(currentWeek);
|
|
376
|
+
}
|
|
377
|
+
return weeks;
|
|
378
|
+
}
|