MCPcopy
hub / github.com/node-cron/node-cron / matchNext

Method matchNext

src/time/matcher-walker.ts:53–82  ·  view source on GitHub ↗

* Finds the next instant that satisfies the expression, strictly after the * base date. * * Instead of scanning time tick by tick, it infers candidates directly from * the cron fields: it walks the calendar day by day (a Y/M/D is the same * weekday in every timezone), and on each day

()

Source from the content-addressed store, hash-verified

51 * non-existent local times (the DST spring-forward gap) by construction.
52 */
53 matchNext(): LocalizedTime {
54 const months = this.months;
55 const days = this.days;
56
57 const baseMs = Math.floor(this.baseDate.getTime() / 1000) * 1000;
58 const baseParts = new LocalizedTime(new Date(baseMs), this.timezone).getParts();
59
60 let { year, month, day } = baseParts;
61
62 for (let i = 0; i < MAX_DAYS; i++) {
63 // Pre-check month, day-of-month and weekday before the (potentially
64 // 86,400-wide) time-of-day scan. The weekday check mirrors match()
65 // exactly, so it only skips days that would be rejected anyway, and it
66 // avoids scanning every time on a day whose weekday can't match (e.g.
67 // `* * * 15 * 1`, the 15th only when it is a Monday).
68 if (months.includes(month) && matchesDayOfMonth(days, year, month, day) && this.matchesWeekday(year, month, day)) {
69 // On the base day the result must be strictly after the base instant;
70 // on later days any matching time of day qualifies.
71 const lowerBound = i === 0 ? baseParts : null;
72 const found = this.firstTimeOnDay(year, month, day, lowerBound, baseMs);
73 if (found !== null) {
74 return new LocalizedTime(new Date(found), this.timezone);
75 }
76 }
77
78 ({ year, month, day } = nextDay(year, month, day));
79 }
80
81 throw new Error('Could not find next matching date within reasonable time range');
82 }
83
84 /**
85 * Smallest matching (hour, minute, second) on the given calendar day that

Callers 2

getNextMatchMethod · 0.95

Calls 5

matchesWeekdayMethod · 0.95
firstTimeOnDayMethod · 0.95
matchesDayOfMonthFunction · 0.90
nextDayFunction · 0.85
getPartsMethod · 0.80

Tested by

no test coverage detected