( fields: CronFields, from: Date, )
| 123 | * the second occurrence). This matches vixie-cron behavior. |
| 124 | */ |
| 125 | export function computeNextCronRun( |
| 126 | fields: CronFields, |
| 127 | from: Date, |
| 128 | ): Date | null { |
| 129 | const minuteSet = new Set(fields.minute) |
| 130 | const hourSet = new Set(fields.hour) |
| 131 | const domSet = new Set(fields.dayOfMonth) |
| 132 | const monthSet = new Set(fields.month) |
| 133 | const dowSet = new Set(fields.dayOfWeek) |
| 134 | |
| 135 | // Is the field wildcarded (full range)? |
| 136 | const domWild = fields.dayOfMonth.length === 31 |
| 137 | const dowWild = fields.dayOfWeek.length === 7 |
| 138 | |
| 139 | // Round up to the next whole minute (strictly after `from`) |
| 140 | const t = new Date(from.getTime()) |
| 141 | t.setSeconds(0, 0) |
| 142 | t.setMinutes(t.getMinutes() + 1) |
| 143 | |
| 144 | const maxIter = 366 * 24 * 60 |
| 145 | for (let i = 0; i < maxIter; i++) { |
| 146 | const month = t.getMonth() + 1 |
| 147 | if (!monthSet.has(month)) { |
| 148 | // Jump to start of next month |
| 149 | t.setMonth(t.getMonth() + 1, 1) |
| 150 | t.setHours(0, 0, 0, 0) |
| 151 | continue |
| 152 | } |
| 153 | |
| 154 | const dom = t.getDate() |
| 155 | const dow = t.getDay() |
| 156 | // When both dom/dow are constrained, either match is sufficient (OR semantics) |
| 157 | const dayMatches = |
| 158 | domWild && dowWild |
| 159 | ? true |
| 160 | : domWild |
| 161 | ? dowSet.has(dow) |
| 162 | : dowWild |
| 163 | ? domSet.has(dom) |
| 164 | : domSet.has(dom) || dowSet.has(dow) |
| 165 | |
| 166 | if (!dayMatches) { |
| 167 | // Jump to start of next day |
| 168 | t.setDate(t.getDate() + 1) |
| 169 | t.setHours(0, 0, 0, 0) |
| 170 | continue |
| 171 | } |
| 172 | |
| 173 | if (!hourSet.has(t.getHours())) { |
| 174 | t.setHours(t.getHours() + 1, 0, 0, 0) |
| 175 | continue |
| 176 | } |
| 177 | |
| 178 | if (!minuteSet.has(t.getMinutes())) { |
| 179 | t.setMinutes(t.getMinutes() + 1) |
| 180 | continue |
| 181 | } |
| 182 |
no test coverage detected