croner 8.1.2 → 9.0.0-dev.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.
@@ -0,0 +1,1174 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/croner.ts
20
+ var croner_exports = {};
21
+ __export(croner_exports, {
22
+ Cron: () => Cron,
23
+ default: () => croner_default,
24
+ scheduledJobs: () => scheduledJobs
25
+ });
26
+ module.exports = __toCommonJS(croner_exports);
27
+
28
+ // src/helpers/minitz.ts
29
+ function minitz(y, m, d, h, i, s, tz, throwOnInvalid) {
30
+ return minitz.fromTZ(minitz.tp(y, m, d, h, i, s, tz), throwOnInvalid);
31
+ }
32
+ minitz.fromTZISO = (localTimeStr, tz, throwOnInvalid) => {
33
+ return minitz.fromTZ(parseISOLocal(localTimeStr, tz), throwOnInvalid);
34
+ };
35
+ minitz.fromTZ = function(tp, throwOnInvalid) {
36
+ const inDate = new Date(Date.UTC(
37
+ tp.y,
38
+ tp.m - 1,
39
+ tp.d,
40
+ tp.h,
41
+ tp.i,
42
+ tp.s
43
+ )), offset = getTimezoneOffset(tp.tz, inDate), dateGuess = new Date(inDate.getTime() - offset), dateOffsGuess = getTimezoneOffset(tp.tz, dateGuess);
44
+ if (dateOffsGuess - offset === 0) {
45
+ return dateGuess;
46
+ } else {
47
+ const dateGuess2 = new Date(inDate.getTime() - dateOffsGuess), dateOffsGuess2 = getTimezoneOffset(tp.tz, dateGuess2);
48
+ if (dateOffsGuess2 - dateOffsGuess === 0) {
49
+ return dateGuess2;
50
+ } else if (!throwOnInvalid && dateOffsGuess2 - dateOffsGuess > 0) {
51
+ return dateGuess2;
52
+ } else if (!throwOnInvalid) {
53
+ return dateGuess;
54
+ } else {
55
+ throw new Error("Invalid date passed to fromTZ()");
56
+ }
57
+ }
58
+ };
59
+ minitz.toTZ = function(d, tzStr) {
60
+ const localDateString = d.toLocaleString("en-US", { timeZone: tzStr }).replace(/[\u202f]/, " ");
61
+ const td = new Date(localDateString);
62
+ return {
63
+ y: td.getFullYear(),
64
+ m: td.getMonth() + 1,
65
+ d: td.getDate(),
66
+ h: td.getHours(),
67
+ i: td.getMinutes(),
68
+ s: td.getSeconds(),
69
+ tz: tzStr
70
+ };
71
+ };
72
+ minitz.tp = (y, m, d, h, i, s, tz) => {
73
+ return { y, m, d, h, i, s, tz };
74
+ };
75
+ function getTimezoneOffset(timeZone, date = /* @__PURE__ */ new Date()) {
76
+ const tz = date.toLocaleString("en-US", { timeZone, timeZoneName: "shortOffset" }).split(" ").slice(-1)[0];
77
+ const dateString = date.toLocaleString("en-US").replace(/[\u202f]/, " ");
78
+ return Date.parse(`${dateString} GMT`) - Date.parse(`${dateString} ${tz}`);
79
+ }
80
+ function parseISOLocal(dtStr, tz) {
81
+ const pd = new Date(Date.parse(dtStr));
82
+ if (isNaN(pd)) {
83
+ throw new Error("minitz: Invalid ISO8601 passed to parser.");
84
+ }
85
+ const stringEnd = dtStr.substring(9);
86
+ if (dtStr.includes("Z") || stringEnd.includes("-") || stringEnd.includes("+")) {
87
+ return minitz.tp(
88
+ pd.getUTCFullYear(),
89
+ pd.getUTCMonth() + 1,
90
+ pd.getUTCDate(),
91
+ pd.getUTCHours(),
92
+ pd.getUTCMinutes(),
93
+ pd.getUTCSeconds(),
94
+ "Etc/UTC"
95
+ );
96
+ } else {
97
+ return minitz.tp(
98
+ pd.getFullYear(),
99
+ pd.getMonth() + 1,
100
+ pd.getDate(),
101
+ pd.getHours(),
102
+ pd.getMinutes(),
103
+ pd.getSeconds(),
104
+ tz
105
+ );
106
+ }
107
+ }
108
+ minitz.minitz = minitz;
109
+
110
+ // src/pattern.ts
111
+ var LAST_OCCURRENCE = 32;
112
+ var ANY_OCCURRENCE = 1 | 2 | 4 | 8 | 16 | LAST_OCCURRENCE;
113
+ var OCCURRENCE_BITMASKS = [1, 2, 4, 8, 16];
114
+ var CronPattern = class {
115
+ pattern;
116
+ timezone;
117
+ second;
118
+ minute;
119
+ hour;
120
+ day;
121
+ month;
122
+ dayOfWeek;
123
+ lastDayOfMonth;
124
+ starDOM;
125
+ starDOW;
126
+ constructor(pattern, timezone) {
127
+ this.pattern = pattern;
128
+ this.timezone = timezone;
129
+ this.second = Array(60).fill(0);
130
+ this.minute = Array(60).fill(0);
131
+ this.hour = Array(24).fill(0);
132
+ this.day = Array(31).fill(0);
133
+ this.month = Array(12).fill(0);
134
+ this.dayOfWeek = Array(7).fill(0);
135
+ this.lastDayOfMonth = false;
136
+ this.starDOM = false;
137
+ this.starDOW = false;
138
+ this.parse();
139
+ }
140
+ /**
141
+ * Parse current pattern, will throw on any type of failure
142
+ * @private
143
+ */
144
+ parse() {
145
+ if (!(typeof this.pattern === "string")) {
146
+ throw new TypeError("CronPattern: Pattern has to be of type string.");
147
+ }
148
+ if (this.pattern.indexOf("@") >= 0) this.pattern = this.handleNicknames(this.pattern).trim();
149
+ const parts = this.pattern.replace(/\s+/g, " ").split(" ");
150
+ if (parts.length < 5 || parts.length > 6) {
151
+ throw new TypeError(
152
+ "CronPattern: invalid configuration format ('" + this.pattern + "'), exactly five or six space separated parts are required."
153
+ );
154
+ }
155
+ if (parts.length === 5) {
156
+ parts.unshift("0");
157
+ }
158
+ if (parts[3].indexOf("L") >= 0) {
159
+ parts[3] = parts[3].replace("L", "");
160
+ this.lastDayOfMonth = true;
161
+ }
162
+ if (parts[3] == "*") {
163
+ this.starDOM = true;
164
+ }
165
+ if (parts[4].length >= 3) parts[4] = this.replaceAlphaMonths(parts[4]);
166
+ if (parts[5].length >= 3) parts[5] = this.replaceAlphaDays(parts[5]);
167
+ if (parts[5] == "*") {
168
+ this.starDOW = true;
169
+ }
170
+ if (this.pattern.indexOf("?") >= 0) {
171
+ const initDate = new CronDate(/* @__PURE__ */ new Date(), this.timezone).getDate(true);
172
+ parts[0] = parts[0].replace("?", initDate.getSeconds().toString());
173
+ parts[1] = parts[1].replace("?", initDate.getMinutes().toString());
174
+ parts[2] = parts[2].replace("?", initDate.getHours().toString());
175
+ if (!this.starDOM) parts[3] = parts[3].replace("?", initDate.getDate().toString());
176
+ parts[4] = parts[4].replace("?", (initDate.getMonth() + 1).toString());
177
+ if (!this.starDOW) parts[5] = parts[5].replace("?", initDate.getDay().toString());
178
+ }
179
+ this.throwAtIllegalCharacters(parts);
180
+ this.partToArray("second", parts[0], 0, 1);
181
+ this.partToArray("minute", parts[1], 0, 1);
182
+ this.partToArray("hour", parts[2], 0, 1);
183
+ this.partToArray("day", parts[3], -1, 1);
184
+ this.partToArray("month", parts[4], -1, 1);
185
+ this.partToArray("dayOfWeek", parts[5], 0, ANY_OCCURRENCE);
186
+ if (this.dayOfWeek[7]) {
187
+ this.dayOfWeek[0] = this.dayOfWeek[7];
188
+ }
189
+ }
190
+ /**
191
+ * Convert current part (seconds/minutes etc) to an array of 1 or 0 depending on if the part is about to trigger a run or not.
192
+ */
193
+ partToArray(type, conf, valueIndexOffset, defaultValue) {
194
+ const arr = this[type];
195
+ const lastDayOfMonth = type === "day" && this.lastDayOfMonth;
196
+ if (conf === "" && !lastDayOfMonth) {
197
+ throw new TypeError(
198
+ "CronPattern: configuration entry " + type + " (" + conf + ") is empty, check for trailing spaces."
199
+ );
200
+ }
201
+ if (conf === "*") return arr.fill(defaultValue);
202
+ const split = conf.split(",");
203
+ if (split.length > 1) {
204
+ for (let i = 0; i < split.length; i++) {
205
+ this.partToArray(type, split[i], valueIndexOffset, defaultValue);
206
+ }
207
+ } else if (conf.indexOf("-") !== -1 && conf.indexOf("/") !== -1) {
208
+ this.handleRangeWithStepping(conf, type, valueIndexOffset, defaultValue);
209
+ } else if (conf.indexOf("-") !== -1) {
210
+ this.handleRange(conf, type, valueIndexOffset, defaultValue);
211
+ } else if (conf.indexOf("/") !== -1) {
212
+ this.handleStepping(conf, type, valueIndexOffset, defaultValue);
213
+ } else if (conf !== "") {
214
+ this.handleNumber(conf, type, valueIndexOffset, defaultValue);
215
+ }
216
+ }
217
+ /**
218
+ * After converting JAN-DEC, SUN-SAT only 0-9 * , / - are allowed, throw if anything else pops up
219
+ * @throws On error
220
+ */
221
+ throwAtIllegalCharacters(parts) {
222
+ for (let i = 0; i < parts.length; i++) {
223
+ const reValidCron = i === 5 ? /[^/*0-9,\-#L]+/ : /[^/*0-9,-]+/;
224
+ if (reValidCron.test(parts[i])) {
225
+ throw new TypeError(
226
+ "CronPattern: configuration entry " + i + " (" + parts[i] + ") contains illegal characters."
227
+ );
228
+ }
229
+ }
230
+ }
231
+ /**
232
+ * Nothing but a number left, handle that
233
+ *
234
+ * @param conf Current part, expected to be a number, as a string
235
+ * @param type One of "seconds", "minutes" etc
236
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
237
+ */
238
+ handleNumber(conf, type, valueIndexOffset, defaultValue) {
239
+ const result = this.extractNth(conf, type);
240
+ const i = parseInt(result[0], 10) + valueIndexOffset;
241
+ if (isNaN(i)) {
242
+ throw new TypeError("CronPattern: " + type + " is not a number: '" + conf + "'");
243
+ }
244
+ this.setPart(type, i, result[1] || defaultValue);
245
+ }
246
+ /**
247
+ * Set a specific value for a specific part of the CronPattern.
248
+ *
249
+ * @param part The specific part of the CronPattern, e.g., "second", "minute", etc.
250
+ * @param index The index to modify.
251
+ * @param value The value to set, typically 0 or 1, in case of "nth weekday" it will be the weekday number used for further processing
252
+ */
253
+ setPart(part, index, value) {
254
+ if (!Object.prototype.hasOwnProperty.call(this, part)) {
255
+ throw new TypeError("CronPattern: Invalid part specified: " + part);
256
+ }
257
+ if (part === "dayOfWeek") {
258
+ if (index === 7) index = 0;
259
+ if (index < 0 || index > 6) {
260
+ throw new RangeError("CronPattern: Invalid value for dayOfWeek: " + index);
261
+ }
262
+ this.setNthWeekdayOfMonth(index, value);
263
+ return;
264
+ }
265
+ if (part === "second" || part === "minute") {
266
+ if (index < 0 || index >= 60) {
267
+ throw new RangeError("CronPattern: Invalid value for " + part + ": " + index);
268
+ }
269
+ } else if (part === "hour") {
270
+ if (index < 0 || index >= 24) {
271
+ throw new RangeError("CronPattern: Invalid value for " + part + ": " + index);
272
+ }
273
+ } else if (part === "day") {
274
+ if (index < 0 || index >= 31) {
275
+ throw new RangeError("CronPattern: Invalid value for " + part + ": " + index);
276
+ }
277
+ } else if (part === "month") {
278
+ if (index < 0 || index >= 12) {
279
+ throw new RangeError("CronPattern: Invalid value for " + part + ": " + index);
280
+ }
281
+ }
282
+ this[part][index] = value;
283
+ }
284
+ /**
285
+ * Take care of ranges with stepping (e.g. 3-23/5)
286
+ *
287
+ * @param conf Current part, expected to be a string like 3-23/5
288
+ * @param type One of "seconds", "minutes" etc
289
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
290
+ */
291
+ handleRangeWithStepping(conf, type, valueIndexOffset, defaultValue) {
292
+ const result = this.extractNth(conf, type);
293
+ const matches = result[0].match(/^(\d+)-(\d+)\/(\d+)$/);
294
+ if (matches === null) {
295
+ throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '" + conf + "'");
296
+ }
297
+ const [, lowerMatch, upperMatch, stepMatch] = matches;
298
+ const lower = parseInt(lowerMatch, 10) + valueIndexOffset;
299
+ const upper = parseInt(upperMatch, 10) + valueIndexOffset;
300
+ const steps = parseInt(stepMatch, 10);
301
+ if (isNaN(lower)) throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");
302
+ if (isNaN(upper)) throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");
303
+ if (isNaN(steps)) throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");
304
+ if (steps === 0) throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
305
+ if (steps > this[type].length) {
306
+ throw new TypeError(
307
+ "CronPattern: Syntax error, steps cannot be greater than maximum value of part (" + this[type].length + ")"
308
+ );
309
+ }
310
+ if (lower > upper) {
311
+ throw new TypeError("CronPattern: From value is larger than to value: '" + conf + "'");
312
+ }
313
+ for (let i = lower; i <= upper; i += steps) {
314
+ this.setPart(type, i, result[1] || defaultValue);
315
+ }
316
+ }
317
+ /*
318
+ * Break out nth weekday (#) if exists
319
+ * - only allow if type os dayOfWeek
320
+ */
321
+ extractNth(conf, type) {
322
+ let rest = conf;
323
+ let nth;
324
+ if (rest.includes("#")) {
325
+ if (type !== "dayOfWeek") {
326
+ throw new Error("CronPattern: nth (#) only allowed in day-of-week field");
327
+ }
328
+ nth = rest.split("#")[1];
329
+ rest = rest.split("#")[0];
330
+ }
331
+ return [rest, nth];
332
+ }
333
+ /**
334
+ * Take care of ranges (e.g. 1-20)
335
+ *
336
+ * @param conf - Current part, expected to be a string like 1-20, can contain L for last
337
+ * @param type - One of "seconds", "minutes" etc
338
+ * @param valueIndexOffset - -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
339
+ */
340
+ handleRange(conf, type, valueIndexOffset, defaultValue) {
341
+ const result = this.extractNth(conf, type);
342
+ const split = result[0].split("-");
343
+ if (split.length !== 2) {
344
+ throw new TypeError("CronPattern: Syntax error, illegal range: '" + conf + "'");
345
+ }
346
+ const lower = parseInt(split[0], 10) + valueIndexOffset, upper = parseInt(split[1], 10) + valueIndexOffset;
347
+ if (isNaN(lower)) {
348
+ throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");
349
+ } else if (isNaN(upper)) {
350
+ throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");
351
+ }
352
+ if (lower > upper) {
353
+ throw new TypeError("CronPattern: From value is larger than to value: '" + conf + "'");
354
+ }
355
+ for (let i = lower; i <= upper; i++) {
356
+ this.setPart(type, i, result[1] || defaultValue);
357
+ }
358
+ }
359
+ /**
360
+ * Handle stepping (e.g. * / 14)
361
+ *
362
+ * @param conf Current part, expected to be a string like * /20 (without the space)
363
+ * @param type One of "seconds", "minutes" etc
364
+ */
365
+ handleStepping(conf, type, valueIndexOffset, defaultValue) {
366
+ const result = this.extractNth(conf, type);
367
+ const split = result[0].split("/");
368
+ if (split.length !== 2) {
369
+ throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
370
+ }
371
+ let start = 0;
372
+ if (split[0] !== "*") {
373
+ start = parseInt(split[0], 10) + valueIndexOffset;
374
+ }
375
+ const steps = parseInt(split[1], 10);
376
+ if (isNaN(steps)) throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");
377
+ if (steps === 0) throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
378
+ if (steps > this[type].length) {
379
+ throw new TypeError(
380
+ "CronPattern: Syntax error, max steps for part is (" + this[type].length + ")"
381
+ );
382
+ }
383
+ for (let i = start; i < this[type].length; i += steps) {
384
+ this.setPart(type, i, result[1] || defaultValue);
385
+ }
386
+ }
387
+ /**
388
+ * Replace day name with day numbers
389
+ *
390
+ * @param conf Current part, expected to be a string that might contain sun,mon etc.
391
+ *
392
+ * @returns Conf with 0 instead of sun etc.
393
+ */
394
+ replaceAlphaDays(conf) {
395
+ return conf.replace(/-sun/gi, "-7").replace(/sun/gi, "0").replace(/mon/gi, "1").replace(/tue/gi, "2").replace(/wed/gi, "3").replace(/thu/gi, "4").replace(/fri/gi, "5").replace(/sat/gi, "6");
396
+ }
397
+ /**
398
+ * Replace month name with month numbers
399
+ *
400
+ * @param conf Current part, expected to be a string that might contain jan,feb etc.
401
+ *
402
+ * @returns conf with 0 instead of sun etc.
403
+ */
404
+ replaceAlphaMonths(conf) {
405
+ return conf.replace(/jan/gi, "1").replace(/feb/gi, "2").replace(/mar/gi, "3").replace(/apr/gi, "4").replace(/may/gi, "5").replace(/jun/gi, "6").replace(/jul/gi, "7").replace(/aug/gi, "8").replace(/sep/gi, "9").replace(/oct/gi, "10").replace(/nov/gi, "11").replace(/dec/gi, "12");
406
+ }
407
+ /**
408
+ * Replace nicknames with actual cron patterns
409
+ *
410
+ * @param pattern Pattern, may contain nicknames, or not
411
+ *
412
+ * @returns Pattern, with cron expression insted of nicknames
413
+ */
414
+ handleNicknames(pattern) {
415
+ const cleanPattern = pattern.trim().toLowerCase();
416
+ if (cleanPattern === "@yearly" || cleanPattern === "@annually") {
417
+ return "0 0 1 1 *";
418
+ } else if (cleanPattern === "@monthly") {
419
+ return "0 0 1 * *";
420
+ } else if (cleanPattern === "@weekly") {
421
+ return "0 0 * * 0";
422
+ } else if (cleanPattern === "@daily") {
423
+ return "0 0 * * *";
424
+ } else if (cleanPattern === "@hourly") {
425
+ return "0 * * * *";
426
+ } else {
427
+ return pattern;
428
+ }
429
+ }
430
+ /**
431
+ * Handle the nth weekday of the month logic using hash sign (e.g. FRI#2 for the second Friday of the month)
432
+ *
433
+ * @param index Weekday, example: 5 for friday
434
+ * @param nthWeekday bitmask, 2 (0x00010) for 2nd friday, 31 (ANY_OCCURRENCE, 0b100000) for any day
435
+ */
436
+ setNthWeekdayOfMonth(index, nthWeekday) {
437
+ if (typeof nthWeekday !== "number" && nthWeekday === "L") {
438
+ this["dayOfWeek"][index] = this["dayOfWeek"][index] | LAST_OCCURRENCE;
439
+ } else if (typeof nthWeekday === "number" && nthWeekday < 6 && nthWeekday > 0) {
440
+ this["dayOfWeek"][index] = this["dayOfWeek"][index] | OCCURRENCE_BITMASKS[nthWeekday - 1];
441
+ } else if (typeof nthWeekday === "number" && nthWeekday === ANY_OCCURRENCE) {
442
+ this["dayOfWeek"][index] = ANY_OCCURRENCE;
443
+ } else {
444
+ throw new TypeError(
445
+ `CronPattern: nth weekday of of range, should be 1-5 or L. Value: ${nthWeekday}`
446
+ );
447
+ }
448
+ }
449
+ };
450
+
451
+ // src/date.ts
452
+ var DaysOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
453
+ var RecursionSteps = [
454
+ ["month", "year", 0],
455
+ ["day", "month", -1],
456
+ ["hour", "day", 0],
457
+ ["minute", "hour", 0],
458
+ ["second", "minute", 0]
459
+ ];
460
+ var CronDate = class _CronDate {
461
+ tz;
462
+ /**
463
+ * Current milliseconds
464
+ * @type {number}
465
+ */
466
+ ms;
467
+ /**
468
+ * Current second (0-59), in local time or target timezone specified by `this.tz`
469
+ * @type {number}
470
+ */
471
+ second;
472
+ /**
473
+ * Current minute (0-59), in local time or target timezone specified by `this.tz`
474
+ * @type {number}
475
+ */
476
+ minute;
477
+ /**
478
+ * Current hour (0-23), in local time or target timezone specified by `this.tz`
479
+ * @type {number}
480
+ */
481
+ hour;
482
+ /**
483
+ * Current day (1-31), in local time or target timezone specified by `this.tz`
484
+ * @type {number}
485
+ */
486
+ day;
487
+ /**
488
+ * Current month (1-12), in local time or target timezone specified by `this.tz`
489
+ * @type {number}
490
+ */
491
+ month;
492
+ /**
493
+ * Current full year, in local time or target timezone specified by `this.tz`
494
+ */
495
+ year;
496
+ constructor(d, tz) {
497
+ this.tz = tz;
498
+ if (d && d instanceof Date) {
499
+ if (!isNaN(d)) {
500
+ this.fromDate(d);
501
+ } else {
502
+ throw new TypeError("CronDate: Invalid date passed to CronDate constructor");
503
+ }
504
+ } else if (d === void 0) {
505
+ this.fromDate(/* @__PURE__ */ new Date());
506
+ } else if (d && typeof d === "string") {
507
+ this.fromString(d);
508
+ } else if (d instanceof _CronDate) {
509
+ this.fromCronDate(d);
510
+ } else {
511
+ throw new TypeError(
512
+ "CronDate: Invalid type (" + typeof d + ") passed to CronDate constructor"
513
+ );
514
+ }
515
+ }
516
+ /**
517
+ * Check if the given date is the nth occurrence of a weekday in its month.
518
+ *
519
+ * @param year The year.
520
+ * @param month The month (0 for January, 11 for December).
521
+ * @param day The day of the month.
522
+ * @param nth The nth occurrence (bitmask).
523
+ *
524
+ * @return True if the date is the nth occurrence of its weekday, false otherwise.
525
+ */
526
+ isNthWeekdayOfMonth(year, month, day, nth) {
527
+ const date = new Date(Date.UTC(year, month, day));
528
+ const weekday = date.getUTCDay();
529
+ let count = 0;
530
+ for (let d = 1; d <= day; d++) {
531
+ if (new Date(Date.UTC(year, month, d)).getUTCDay() === weekday) {
532
+ count++;
533
+ }
534
+ }
535
+ if (nth & ANY_OCCURRENCE && OCCURRENCE_BITMASKS[count - 1] & nth) {
536
+ return true;
537
+ }
538
+ if (nth & LAST_OCCURRENCE) {
539
+ const daysInMonth = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
540
+ for (let d = day + 1; d <= daysInMonth; d++) {
541
+ if (new Date(Date.UTC(year, month, d)).getUTCDay() === weekday) {
542
+ return false;
543
+ }
544
+ }
545
+ return true;
546
+ }
547
+ return false;
548
+ }
549
+ /**
550
+ * Sets internals using a Date
551
+ */
552
+ fromDate(inDate) {
553
+ if (this.tz !== void 0) {
554
+ if (typeof this.tz === "number") {
555
+ this.ms = inDate.getUTCMilliseconds();
556
+ this.second = inDate.getUTCSeconds();
557
+ this.minute = inDate.getUTCMinutes() + this.tz;
558
+ this.hour = inDate.getUTCHours();
559
+ this.day = inDate.getUTCDate();
560
+ this.month = inDate.getUTCMonth();
561
+ this.year = inDate.getUTCFullYear();
562
+ this.apply();
563
+ } else {
564
+ const d = minitz.toTZ(inDate, this.tz);
565
+ this.ms = inDate.getMilliseconds();
566
+ this.second = d.s;
567
+ this.minute = d.i;
568
+ this.hour = d.h;
569
+ this.day = d.d;
570
+ this.month = d.m - 1;
571
+ this.year = d.y;
572
+ }
573
+ } else {
574
+ this.ms = inDate.getMilliseconds();
575
+ this.second = inDate.getSeconds();
576
+ this.minute = inDate.getMinutes();
577
+ this.hour = inDate.getHours();
578
+ this.day = inDate.getDate();
579
+ this.month = inDate.getMonth();
580
+ this.year = inDate.getFullYear();
581
+ }
582
+ }
583
+ /**
584
+ * Sets internals by deep copying another CronDate
585
+ *
586
+ * @param {CronDate} d - Input date
587
+ */
588
+ fromCronDate(d) {
589
+ this.tz = d.tz;
590
+ this.year = d.year;
591
+ this.month = d.month;
592
+ this.day = d.day;
593
+ this.hour = d.hour;
594
+ this.minute = d.minute;
595
+ this.second = d.second;
596
+ this.ms = d.ms;
597
+ }
598
+ /**
599
+ * Reset internal parameters (seconds, minutes, hours) if any of them have exceeded (or could have exceeded) their normal ranges
600
+ *
601
+ * Will alway return true on february 29th, as that is a date that _could_ be out of bounds
602
+ */
603
+ apply() {
604
+ if (this.month > 11 || this.day > DaysOfMonth[this.month] || this.hour > 59 || this.minute > 59 || this.second > 59 || this.hour < 0 || this.minute < 0 || this.second < 0) {
605
+ const d = new Date(
606
+ Date.UTC(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms)
607
+ );
608
+ this.ms = d.getUTCMilliseconds();
609
+ this.second = d.getUTCSeconds();
610
+ this.minute = d.getUTCMinutes();
611
+ this.hour = d.getUTCHours();
612
+ this.day = d.getUTCDate();
613
+ this.month = d.getUTCMonth();
614
+ this.year = d.getUTCFullYear();
615
+ return true;
616
+ } else {
617
+ return false;
618
+ }
619
+ }
620
+ /**
621
+ * Sets internals by parsing a string
622
+ */
623
+ fromString(str) {
624
+ if (typeof this.tz === "number") {
625
+ const inDate = minitz.fromTZISO(str, "UTC");
626
+ this.ms = inDate.getUTCMilliseconds();
627
+ this.second = inDate.getUTCSeconds();
628
+ this.minute = inDate.getUTCMinutes();
629
+ this.hour = inDate.getUTCHours();
630
+ this.day = inDate.getUTCDate();
631
+ this.month = inDate.getUTCMonth();
632
+ this.year = inDate.getUTCFullYear();
633
+ this.apply();
634
+ } else if (this.tz === void 0) {
635
+ return this.fromDate(minitz.fromTZISO(str, this.tz === void 0 ? "UTC" : this.tz));
636
+ }
637
+ }
638
+ /**
639
+ * Find next match of current part
640
+ */
641
+ findNext(options, target, pattern, offset) {
642
+ const originalTarget = this[target];
643
+ let lastDayOfMonth;
644
+ if (pattern.lastDayOfMonth) {
645
+ if (this.month !== 1) {
646
+ lastDayOfMonth = DaysOfMonth[this.month];
647
+ } else {
648
+ lastDayOfMonth = new Date(Date.UTC(this.year, this.month + 1, 0, 0, 0, 0, 0)).getUTCDate();
649
+ }
650
+ }
651
+ const fDomWeekDay = !pattern.starDOW && target == "day" ? new Date(Date.UTC(this.year, this.month, 1, 0, 0, 0, 0)).getUTCDay() : void 0;
652
+ for (let i = this[target] + offset; i < pattern[target].length; i++) {
653
+ let match = pattern[target][i];
654
+ if (target === "day" && pattern.lastDayOfMonth && i - offset == lastDayOfMonth) {
655
+ match = 1;
656
+ }
657
+ if (target === "day" && !pattern.starDOW) {
658
+ let dowMatch = pattern.dayOfWeek[(fDomWeekDay + (i - offset - 1)) % 7];
659
+ if (dowMatch && dowMatch & ANY_OCCURRENCE) {
660
+ dowMatch = this.isNthWeekdayOfMonth(this.year, this.month, i - offset, dowMatch) ? 1 : 0;
661
+ } else if (dowMatch) {
662
+ throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${dowMatch}`);
663
+ }
664
+ if (options.legacyMode && !pattern.starDOM) {
665
+ match = match || dowMatch;
666
+ } else {
667
+ match = match && dowMatch;
668
+ }
669
+ }
670
+ if (match) {
671
+ this[target] = i - offset;
672
+ return originalTarget !== this[target] ? 2 : 1;
673
+ }
674
+ }
675
+ return 3;
676
+ }
677
+ /**
678
+ * Increment to next run time recursively
679
+ *
680
+ * This function is currently capped at year 3000. Do you have a reason to go further? Open an issue on GitHub!
681
+ *
682
+ * @param pattern The pattern used to increment current state
683
+ * @param options Cron options used for incrementing
684
+ * @param doing Which part to increment, 0 represent first item of RecursionSteps-array etc.
685
+ * @return Returns itthis for chaining, or null if increment wasnt possible
686
+ */
687
+ recurse(pattern, options, doing) {
688
+ const res = this.findNext(options, RecursionSteps[doing][0], pattern, RecursionSteps[doing][2]);
689
+ if (res > 1) {
690
+ let resetLevel = doing + 1;
691
+ while (resetLevel < RecursionSteps.length) {
692
+ this[RecursionSteps[resetLevel][0]] = -RecursionSteps[resetLevel][2];
693
+ resetLevel++;
694
+ }
695
+ if (res === 3) {
696
+ this[RecursionSteps[doing][1]]++;
697
+ this[RecursionSteps[doing][0]] = -RecursionSteps[doing][2];
698
+ this.apply();
699
+ return this.recurse(pattern, options, 0);
700
+ } else if (this.apply()) {
701
+ return this.recurse(pattern, options, doing - 1);
702
+ }
703
+ }
704
+ doing += 1;
705
+ if (doing >= RecursionSteps.length) {
706
+ return this;
707
+ } else if (this.year >= 3e3) {
708
+ return null;
709
+ } else {
710
+ return this.recurse(pattern, options, doing);
711
+ }
712
+ }
713
+ /**
714
+ * Increment to next run time
715
+ *
716
+ * @param pattern The pattern used to increment current state
717
+ * @param options Cron options used for incrementing
718
+ * @param hasPreviousRun If this run should adhere to minimum interval
719
+ * @return Returns itthis for chaining, or null if increment wasnt possible
720
+ */
721
+ increment(pattern, options, hasPreviousRun) {
722
+ this.second += options.interval !== void 0 && options.interval > 1 && hasPreviousRun ? options.interval : 1;
723
+ this.ms = 0;
724
+ this.apply();
725
+ return this.recurse(pattern, options, 0);
726
+ }
727
+ /**
728
+ * Convert current state back to a javascript Date()
729
+ *
730
+ * @param internal If this is an internal call
731
+ */
732
+ getDate(internal) {
733
+ if (internal || this.tz === void 0) {
734
+ return new Date(
735
+ this.year,
736
+ this.month,
737
+ this.day,
738
+ this.hour,
739
+ this.minute,
740
+ this.second,
741
+ this.ms
742
+ );
743
+ } else {
744
+ if (typeof this.tz === "number") {
745
+ return new Date(
746
+ Date.UTC(
747
+ this.year,
748
+ this.month,
749
+ this.day,
750
+ this.hour,
751
+ this.minute - this.tz,
752
+ this.second,
753
+ this.ms
754
+ )
755
+ );
756
+ } else {
757
+ return minitz.fromTZ(
758
+ minitz.tp(
759
+ this.year,
760
+ this.month + 1,
761
+ this.day,
762
+ this.hour,
763
+ this.minute,
764
+ this.second,
765
+ this.tz
766
+ ),
767
+ false
768
+ );
769
+ }
770
+ }
771
+ }
772
+ /**
773
+ * Convert current state back to a javascript Date() and return UTC milliseconds
774
+ */
775
+ getTime() {
776
+ return this.getDate(false).getTime();
777
+ }
778
+ };
779
+
780
+ // src/options.ts
781
+ function CronOptions(options) {
782
+ if (options === void 0) {
783
+ options = {};
784
+ }
785
+ delete options.name;
786
+ options.legacyMode = options.legacyMode === void 0 ? true : options.legacyMode;
787
+ options.paused = options.paused === void 0 ? false : options.paused;
788
+ options.maxRuns = options.maxRuns === void 0 ? Infinity : options.maxRuns;
789
+ options.catch = options.catch === void 0 ? false : options.catch;
790
+ options.interval = options.interval === void 0 ? 0 : parseInt(options.interval.toString(), 10);
791
+ options.utcOffset = options.utcOffset === void 0 ? void 0 : parseInt(options.utcOffset.toString(), 10);
792
+ options.unref = options.unref === void 0 ? false : options.unref;
793
+ if (options.startAt) {
794
+ options.startAt = new CronDate(options.startAt, options.timezone);
795
+ }
796
+ if (options.stopAt) {
797
+ options.stopAt = new CronDate(options.stopAt, options.timezone);
798
+ }
799
+ if (options.interval !== null) {
800
+ if (isNaN(options.interval)) {
801
+ throw new Error("CronOptions: Supplied value for interval is not a number");
802
+ } else if (options.interval < 0) {
803
+ throw new Error("CronOptions: Supplied value for interval can not be negative");
804
+ }
805
+ }
806
+ if (options.utcOffset !== void 0) {
807
+ if (isNaN(options.utcOffset)) {
808
+ throw new Error(
809
+ "CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC."
810
+ );
811
+ } else if (options.utcOffset < -870 || options.utcOffset > 870) {
812
+ throw new Error("CronOptions: utcOffset out of bounds.");
813
+ }
814
+ if (options.utcOffset !== void 0 && options.timezone) {
815
+ throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.");
816
+ }
817
+ }
818
+ if (options.unref !== true && options.unref !== false) {
819
+ throw new Error("CronOptions: Unref should be either true, false or undefined(false).");
820
+ }
821
+ return options;
822
+ }
823
+
824
+ // src/utils.ts
825
+ function isFunction(v) {
826
+ return Object.prototype.toString.call(v) === "[object Function]" || "function" === typeof v || v instanceof Function;
827
+ }
828
+ function unrefTimer(timer) {
829
+ if (typeof Deno !== "undefined" && typeof Deno.unrefTimer !== "undefined") {
830
+ Deno.unrefTimer(timer);
831
+ } else if (timer && typeof timer.unref !== "undefined") {
832
+ timer.unref();
833
+ }
834
+ }
835
+
836
+ // src/croner.ts
837
+ var maxDelay = 30 * 1e3;
838
+ var scheduledJobs = [];
839
+ var Cron = class {
840
+ name;
841
+ options;
842
+ _states;
843
+ fn;
844
+ constructor(pattern, fnOrOptions1, fnOrOptions2) {
845
+ let options, func;
846
+ if (isFunction(fnOrOptions1)) {
847
+ func = fnOrOptions1;
848
+ } else if (typeof fnOrOptions1 === "object") {
849
+ options = fnOrOptions1;
850
+ } else if (fnOrOptions1 !== void 0) {
851
+ throw new Error(
852
+ "Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options)."
853
+ );
854
+ }
855
+ if (isFunction(fnOrOptions2)) {
856
+ func = fnOrOptions2;
857
+ } else if (typeof fnOrOptions2 === "object") {
858
+ options = fnOrOptions2;
859
+ } else if (fnOrOptions2 !== void 0) {
860
+ throw new Error(
861
+ "Cron: Invalid argument passed for funcIn. Should be one of function, or object (options)."
862
+ );
863
+ }
864
+ this.name = options ? options.name : void 0;
865
+ this.options = CronOptions(options);
866
+ this._states = {
867
+ kill: false,
868
+ blocking: false,
869
+ previousRun: void 0,
870
+ currentRun: void 0,
871
+ once: void 0,
872
+ currentTimeout: void 0,
873
+ maxRuns: options ? options.maxRuns : void 0,
874
+ paused: options ? options.paused : false,
875
+ pattern: new CronPattern("* * * * *")
876
+ };
877
+ if (pattern && (pattern instanceof Date || typeof pattern === "string" && pattern.indexOf(":") > 0)) {
878
+ this._states.once = new CronDate(pattern, this.options.timezone || this.options.utcOffset);
879
+ } else {
880
+ this._states.pattern = new CronPattern(pattern, this.options.timezone);
881
+ }
882
+ if (this.name) {
883
+ const existing = scheduledJobs.find((j) => j.name === this.name);
884
+ if (existing) {
885
+ throw new Error(
886
+ "Cron: Tried to initialize new named job '" + this.name + "', but name already taken."
887
+ );
888
+ } else {
889
+ scheduledJobs.push(this);
890
+ }
891
+ }
892
+ if (func !== void 0 && isFunction(func)) {
893
+ this.fn = func;
894
+ this.schedule();
895
+ }
896
+ return this;
897
+ }
898
+ /**
899
+ * Find next runtime, based on supplied date. Strips milliseconds.
900
+ *
901
+ * @param prev - Date to start from
902
+ * @returns Next run time
903
+ */
904
+ nextRun(prev) {
905
+ const next = this._next(prev);
906
+ return next ? next.getDate(false) : null;
907
+ }
908
+ /**
909
+ * Find next n runs, based on supplied date. Strips milliseconds.
910
+ *
911
+ * @param n - Number of runs to enumerate
912
+ * @param previous - Date to start from
913
+ * @returns - Next n run times
914
+ */
915
+ nextRuns(n, previous) {
916
+ if (this._states.maxRuns !== void 0 && n > this._states.maxRuns) {
917
+ n = this._states.maxRuns;
918
+ }
919
+ const enumeration = [];
920
+ let prev = previous || this._states.currentRun || void 0;
921
+ while (n-- && (prev = this.nextRun(prev))) {
922
+ enumeration.push(prev);
923
+ }
924
+ return enumeration;
925
+ }
926
+ /**
927
+ * Return the original pattern, if there was one
928
+ *
929
+ * @returns Original pattern
930
+ */
931
+ getPattern() {
932
+ return this._states.pattern ? this._states.pattern.pattern : void 0;
933
+ }
934
+ /**
935
+ * Indicates whether or not the cron job is scheduled and running, e.g. awaiting next trigger
936
+ *
937
+ * @returns Running or not
938
+ */
939
+ isRunning() {
940
+ const nextRunTime = this.nextRun(this._states.currentRun);
941
+ const isRunning = !this._states.paused;
942
+ const isScheduled = this.fn !== void 0;
943
+ const notIsKilled = !this._states.kill;
944
+ return isRunning && isScheduled && notIsKilled && nextRunTime !== null;
945
+ }
946
+ /**
947
+ * Indicates whether or not the cron job is permanently stopped
948
+ *
949
+ * @returns Running or not
950
+ */
951
+ isStopped() {
952
+ return this._states.kill;
953
+ }
954
+ /**
955
+ * Indicates whether or not the cron job is currently working
956
+ *
957
+ * @returns Running or not
958
+ */
959
+ isBusy() {
960
+ return this._states.blocking;
961
+ }
962
+ /**
963
+ * Return current/previous run start time
964
+ *
965
+ * @returns Current (if running) or previous run time
966
+ */
967
+ currentRun() {
968
+ return this._states.currentRun ? this._states.currentRun.getDate() : null;
969
+ }
970
+ /**
971
+ * Return previous run start time
972
+ *
973
+ * @returns Previous run time
974
+ */
975
+ previousRun() {
976
+ return this._states.previousRun ? this._states.previousRun.getDate() : null;
977
+ }
978
+ /**
979
+ * Returns number of milliseconds to next run
980
+ *
981
+ * @param prev Starting date, defaults to now - minimum interval
982
+ */
983
+ msToNext(prev) {
984
+ prev = prev || /* @__PURE__ */ new Date();
985
+ const next = this._next(prev);
986
+ if (next) {
987
+ if (prev instanceof CronDate) {
988
+ return next.getTime() - prev.getTime();
989
+ } else {
990
+ return next.getTime() - new CronDate(prev).getTime();
991
+ }
992
+ } else {
993
+ return null;
994
+ }
995
+ }
996
+ /**
997
+ * Stop execution
998
+ *
999
+ * Running this will forcefully stop the job, and prevent furter exection. `.resume()` will not work after stopping.
1000
+ * It will also be removed from the scheduledJobs array if it were named.
1001
+ */
1002
+ stop() {
1003
+ this._states.kill = true;
1004
+ if (this._states.currentTimeout) {
1005
+ clearTimeout(this._states.currentTimeout);
1006
+ }
1007
+ const jobIndex = scheduledJobs.indexOf(this);
1008
+ if (jobIndex >= 0) {
1009
+ scheduledJobs.splice(jobIndex, 1);
1010
+ }
1011
+ }
1012
+ /**
1013
+ * Pause execution
1014
+ *
1015
+ * @returns Wether pause was successful
1016
+ */
1017
+ pause() {
1018
+ this._states.paused = true;
1019
+ return !this._states.kill;
1020
+ }
1021
+ /**
1022
+ * Resume execution
1023
+ *
1024
+ * @returns Wether resume was successful
1025
+ */
1026
+ resume() {
1027
+ this._states.paused = false;
1028
+ return !this._states.kill;
1029
+ }
1030
+ /**
1031
+ * Schedule a new job
1032
+ *
1033
+ * @param func - Function to be run each iteration of pattern
1034
+ */
1035
+ schedule(func) {
1036
+ if (func && this.fn) {
1037
+ throw new Error(
1038
+ "Cron: It is not allowed to schedule two functions using the same Croner instance."
1039
+ );
1040
+ } else if (func) {
1041
+ this.fn = func;
1042
+ }
1043
+ let waitMs = this.msToNext();
1044
+ const target = this.nextRun(this._states.currentRun);
1045
+ if (waitMs === null || waitMs === void 0 || isNaN(waitMs) || target === null) return this;
1046
+ if (waitMs > maxDelay) {
1047
+ waitMs = maxDelay;
1048
+ }
1049
+ this._states.currentTimeout = setTimeout(() => this._checkTrigger(target), waitMs);
1050
+ if (this._states.currentTimeout && this.options.unref) {
1051
+ unrefTimer(this._states.currentTimeout);
1052
+ }
1053
+ return this;
1054
+ }
1055
+ /**
1056
+ * Internal function to trigger a run, used by both scheduled and manual trigger
1057
+ */
1058
+ async _trigger(initiationDate) {
1059
+ this._states.blocking = true;
1060
+ this._states.currentRun = new CronDate(
1061
+ void 0,
1062
+ // We should use initiationDate, but that does not play well with fake timers in third party tests. In real world there is not much difference though */
1063
+ this.options.timezone || this.options.utcOffset
1064
+ );
1065
+ if (this.options.catch) {
1066
+ try {
1067
+ if (this.fn !== void 0) {
1068
+ await this.fn(this, this.options.context);
1069
+ }
1070
+ } catch (_e) {
1071
+ if (isFunction(this.options.catch)) {
1072
+ this.options.catch(_e, this);
1073
+ }
1074
+ }
1075
+ } else {
1076
+ if (this.fn !== void 0) {
1077
+ await this.fn(this, this.options.context);
1078
+ }
1079
+ }
1080
+ this._states.previousRun = new CronDate(
1081
+ initiationDate,
1082
+ this.options.timezone || this.options.utcOffset
1083
+ );
1084
+ this._states.blocking = false;
1085
+ }
1086
+ /**
1087
+ * Trigger a run manually
1088
+ */
1089
+ async trigger() {
1090
+ await this._trigger();
1091
+ }
1092
+ /**
1093
+ * Called when it's time to trigger.
1094
+ * Checks if all conditions are currently met,
1095
+ * then instantly triggers the scheduled function.
1096
+ */
1097
+ _checkTrigger(target) {
1098
+ const now = /* @__PURE__ */ new Date(), shouldRun = !this._states.paused && now.getTime() >= target.getTime(), isBlocked = this._states.blocking && this.options.protect;
1099
+ if (shouldRun && !isBlocked) {
1100
+ if (this._states.maxRuns !== void 0) {
1101
+ this._states.maxRuns--;
1102
+ }
1103
+ this._trigger();
1104
+ } else {
1105
+ if (shouldRun && isBlocked && isFunction(this.options.protect)) {
1106
+ setTimeout(() => this.options.protect(this), 0);
1107
+ }
1108
+ }
1109
+ this.schedule();
1110
+ }
1111
+ /**
1112
+ * Internal version of next. Cron needs millseconds internally, hence _next.
1113
+ */
1114
+ _next(previousRun) {
1115
+ let hasPreviousRun = previousRun || this._states.currentRun ? true : false;
1116
+ let startAtInFutureWithInterval = false;
1117
+ if (!previousRun && this.options.startAt && this.options.interval) {
1118
+ [previousRun, hasPreviousRun] = this._calculatePreviousRun(previousRun, hasPreviousRun);
1119
+ startAtInFutureWithInterval = !previousRun ? true : false;
1120
+ }
1121
+ previousRun = new CronDate(previousRun, this.options.timezone || this.options.utcOffset);
1122
+ if (this.options.startAt && previousRun && previousRun.getTime() < this.options.startAt.getTime()) {
1123
+ previousRun = this.options.startAt;
1124
+ }
1125
+ let nextRun = this._states.once || new CronDate(previousRun, this.options.timezone || this.options.utcOffset);
1126
+ if (!startAtInFutureWithInterval && nextRun !== this._states.once) {
1127
+ nextRun = nextRun.increment(
1128
+ this._states.pattern,
1129
+ this.options,
1130
+ hasPreviousRun
1131
+ // hasPreviousRun is used to allow
1132
+ );
1133
+ }
1134
+ if (this._states.once && this._states.once.getTime() <= previousRun.getTime()) {
1135
+ return null;
1136
+ } else if (nextRun === null || this._states.maxRuns !== void 0 && this._states.maxRuns <= 0 || this._states.kill || this.options.stopAt && nextRun.getTime() >= this.options.stopAt.getTime()) {
1137
+ return null;
1138
+ } else {
1139
+ return nextRun;
1140
+ }
1141
+ }
1142
+ /**
1143
+ * Calculate the previous run if no previous run is supplied, but startAt and interval are set.
1144
+ * This calculation is only necessary if the startAt time is before the current time.
1145
+ * Should only be called from the _next function.
1146
+ */
1147
+ _calculatePreviousRun(prev, hasPreviousRun) {
1148
+ const now = new CronDate(void 0, this.options.timezone || this.options.utcOffset);
1149
+ let newPrev = prev;
1150
+ if (this.options.startAt.getTime() <= now.getTime()) {
1151
+ newPrev = this.options.startAt;
1152
+ let prevTimePlusInterval = prev.getTime() + this.options.interval * 1e3;
1153
+ while (prevTimePlusInterval <= now.getTime()) {
1154
+ newPrev = new CronDate(prev, this.options.timezone || this.options.utcOffset).increment(
1155
+ this._states.pattern,
1156
+ this.options,
1157
+ true
1158
+ );
1159
+ prevTimePlusInterval = prev.getTime() + this.options.interval * 1e3;
1160
+ }
1161
+ hasPreviousRun = true;
1162
+ }
1163
+ if (newPrev === null) {
1164
+ newPrev = void 0;
1165
+ }
1166
+ return [newPrev, hasPreviousRun];
1167
+ }
1168
+ };
1169
+ var croner_default = Cron;
1170
+ // Annotate the CommonJS export names for ESM import in node:
1171
+ 0 && (module.exports = {
1172
+ Cron,
1173
+ scheduledJobs
1174
+ });