include/pq_cron.h

filter: Statementstmt 84.9% 203 / 239 Branchbran 73.0% 184 / 252 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.8% 387 / 491 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_super.h ↑ Summary include/pq_time.h »
1 #ifndef PQ_CRON_H
2 #define PQ_CRON_H
3  
4 /* pq_cron.h - the cron expression parser and the next-occurrence walk.
5 * Pure C over epoch seconds; no database, no Perl values in the hot parts.
6 *
7 * Five fields - minute hour dom month dow - compiled once into bitmasks.
8 * Supported: `*`, ranges a-b, steps *\/n and a-b\/n, lists a,b,c,
9 * case-insensitive three-letter month and day names, the @ aliases, and
10 * `@every <n>(s|m|h|d)`. Deliberately excluded, and rejected BY NAME
11 * rather than mis-parsed: L W # ? and second/year fields.
12 *
13 * The vixie dom/dow OR rule is implemented and separately flagged: when
14 * BOTH day fields are restricted, a day matching either fires; when only
15 * one is restricted, it alone gates. This is the single most common cron
16 * bug and it gets its own test block.
17 *
18 * A spec that can never match ("0 0 30 2 *") is rejected at parse time -
19 * the 5-year walk cap doubles as the detector - so the failure is a boot
20 * croak naming the spec, not a cron that silently never fires. */
21  
22 /* The minute field needs sixty bits, so it needs a real 64-bit type.
23 *
24 * Not perl's U64: handy.h only typedefs it under QUADKIND, and perls
25 * around 5.20/5.22 do not expose it to an extension at all - which is a
26 * hard compile error ("unknown type name 'U64'") on a smoker running
27 * one, even on a 64-bit box. This dist declares MIN_PERL_VERSION
28 * 5.010000, so that is a portability break rather than an old-perl
29 * technicality. Reported by CPAN Testers against 0.01 on 5.20.0,
30 * 5.22.2, 5.22.3 and 5.22.4.
31 *
32 * Not UV either: UV is 32 bits on a 32-bit perl built without
33 * use64bitint, and a 32-bit minute mask is silently wrong rather than
34 * loudly broken. uint64_t is C99, is what the type actually has to be,
35 * and says so. */
36 #include <stdint.h>
37 typedef uint64_t pq_u64;
38  
39 typedef struct pq_cron {
40 pq_u64 minute; /* bits 0-59 */
41 U32 hour; /* bits 0-23 */
42 U32 dom; /* bits 1-31 */
43 U16 mon; /* bits 1-12 */
44 U8 dow; /* bits 0-6, 0 = Sunday (7 folds to 0) */
45 U8 dom_restricted; /* the vixie OR flags */
46 U8 dow_restricted;
47 IV every; /* @every interval seconds; 0 = field mode */
48 const char *src; /* the expression's text, for a named zone's walk:
49 * it points into the caller's SV, valid for the call */
50 STRLEN srclen;
51 } pq_cron;
52  
53 #define PQ_CRON_ALL_MIN ((pq_u64)0xFFFFFFFFFFFFFFFULL) /* 60 bits */
54 #define PQ_CRON_ALL_HOUR ((U32)0x00FFFFFF) /* 24 bits */
55 #define PQ_CRON_ALL_DOM ((U32)0xFFFFFFFE) /* bits 1-31 */
56 #define PQ_CRON_ALL_MON ((U16)0x1FFE) /* bits 1-12 */
57 #define PQ_CRON_ALL_DOW ((U8)0x7F) /* bits 0-6 */
58  
59 /* ---- field parsing --------------------------------------------------------- */
60  
61 static const char *const PQ_CRON_MONTHS[] =
62 { "jan","feb","mar","apr","may","jun",
63 "jul","aug","sep","oct","nov","dec" };
64 static const char *const PQ_CRON_DAYS[] =
65 { "sun","mon","tue","wed","thu","fri","sat" };
66  
67 /* One name or number token out of a field chunk. Returns the numeric
68 * value, or -1 with *errp set. */
69 841 static int pq_cron_atom(pTHX_ const char **pp, const char *end,
70 const char *const *names, int nnames,
71 int base, const char **errp) {
72 841 const char *p = *pp;
73 841 ▶ if (p < end && isALPHA((U8)*p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && isALPHA((U8)*p)) { 841 0 841
if (p < end && isALPHA((U8)*p)) { 0 841 841
74 char buf[4];
75 int i;
76 0 ▶ if (end - p < 3) { *errp = p; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 3) { *errp = p; return -1; } 0 0 0
77 0 ▶ for (i = 0; i < 3; i++) buf[i] = (char)toLOWER((U8)p[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 3; i++) buf[i] = (char)toLOWER((U8)p[i]); 0 0 0
for (i = 0; i < 3; i++) buf[i] = (char)toLOWER((U8)p[i]); 0 0 0
78 0 buf[3] = 0;
79 0 ▶ for (i = 0; i < nnames; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nnames; i++) 0 0 0
80 0 ▶ if (strEQ(buf, names[i])) { *pp = p + 3; return i + base; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(buf, names[i])) { *pp = p + 3; return i + base; } 0 0 0
81 0 *errp = p;
82 0 return -1;
83 }
84 841 ▶ if (p < end && isDIGIT((U8)*p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && isDIGIT((U8)*p)) { 841 0 841
if (p < end && isDIGIT((U8)*p)) { 841 0 841
85 841 int v = 0;
86 2109 ▶ while (p < end && isDIGIT((U8)*p)) v = v * 10 + (*p++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isDIGIT((U8)*p)) v = v * 10 + (*p++ - '0'); 1270 839 2109
while (p < end && isDIGIT((U8)*p)) v = v * 10 + (*p++ - '0'); 1268 2 1270
87 841 *pp = p;
88 841 return v;
89 }
90 0 *errp = p;
91 0 return -1;
92 }
93  
94 /* Parse one field into a mask. lo/hi is the legal range; names may be
95 * NULL. Returns 0 on success, or the offending position via *errp. */
96 2548 static int pq_cron_field(pTHX_ const char *s, const char *end,
97 int lo, int hi,
98 const char *const *names, int nnames,
99 pq_u64 *mask, const char **errp) {
100 2548 pq_u64 out = 0;
101 2548 const char *p = s;
102  
103 2549 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2549 0 2549
104 2549 int a, b, step = 1;
105  
106 2549 ▶ if (*p == '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '*') { 1722 827 2549
107 1722 p++;
108 1722 a = lo; b = hi;
109 }
110 else {
111 827 a = pq_cron_atom(aTHX_ &p, end, names, nnames, lo, errp);
112 827 ▶ if (a < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (a < 0) return -1; 0 827 827
113 827 ▶ if (p < end && *p == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '-') { 2 825 827
if (p < end && *p == '-') { 1 1 2
114 1 p++;
115 1 b = pq_cron_atom(aTHX_ &p, end, names, nnames, lo, errp);
116 1 ▶ if (b < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (b < 0) return -1; 0 1 1
117 }
118 826 else b = a;
119 }
120 2549 ▶ if (p < end && *p == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '/') { 14 2535 2549
if (p < end && *p == '/') { 13 1 14
121 13 p++;
122 13 step = pq_cron_atom(aTHX_ &p, end, NULL, 0, 0, errp);
123 13 ▶ if (step <= 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (step <= 0) return -1; 0 13 13
124 13 ▶ if (b == a) b = hi; /* a/n means a-hi/n; star-slash-n arrives
Branch: 1 branch
BranchTrueFalseTotal
if (b == a) b = hi; /* a/n means a-hi/n; star-slash-n arrives 0 13 13
125 * here with a=lo, b=hi already */
126 }
127  
128 2549 ▶ if (a < lo || a > hi || b > hi) { *errp = s; return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 2548 1 2549
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 2543 5 2548
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 0 2543 2543
129  
130 2543 ▶ if (a <= b) {
Branch: 1 branch
BranchTrueFalseTotal
if (a <= b) { 2543 0 2543
131 int v;
132 39987 ▶ for (v = a; v <= b; v += step) out |= ((pq_u64)1) << v;
Branch: 1 branch
BranchTrueFalseTotal
for (v = a; v <= b; v += step) out |= ((pq_u64)1) << v; 37444 2543 39987
133 }
134 else {
135 /* a wrapped range (fri-mon): both arcs */
136 int v;
137 0 ▶ for (v = a; v <= hi; v += step) out |= ((pq_u64)1) << v;
Branch: 1 branch
BranchTrueFalseTotal
for (v = a; v <= hi; v += step) out |= ((pq_u64)1) << v; 0 0 0
138 0 ▶ for (v = lo; v <= b; v += step) out |= ((pq_u64)1) << v;
Branch: 1 branch
BranchTrueFalseTotal
for (v = lo; v <= b; v += step) out |= ((pq_u64)1) << v; 0 0 0
139 }
140  
141 2543 ▶ if (p < end && *p == ',') { p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == ',') { p++; continue; } 1 2542 2543
if (p < end && *p == ',') { p++; continue; } 1 0 1
142 2542 break;
143 }
144 2542 ▶ if (p != end) { *errp = p; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) { *errp = p; return -1; } 0 2542 2542
145 2542 *mask = out;
146 2542 return 0;
147 }
148  
149 /* The whole expression. Croaks on anything unsupported, naming the token
150 * - a mis-parsed cron that fires at the wrong time is far worse than a
151 * loud rejection. */
152 537 static void pq_cron_parse(pTHX_ const char *expr, STRLEN len, pq_cron *c) {
153 537 const char *p = expr, *end = expr + len;
154 const char *fs[5], *fe[5];
155 int i;
156  
157 537 memset(c, 0, sizeof *c);
158 537 c->src = expr;
159 537 c->srclen = len;
160  
161 537 ▶ while (p < end && isSPACE((U8)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE((U8)*p)) p++; 536 1 537
while (p < end && isSPACE((U8)*p)) p++; 0 536 536
162 537 ▶ while (end > p && isSPACE((U8)end[-1])) end--;
Branch: 2 branches
BranchTrueFalseTotal
while (end > p && isSPACE((U8)end[-1])) end--; 536 1 537
while (end > p && isSPACE((U8)end[-1])) end--; 0 536 536
163 537 ▶ if (p >= end)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) 1 536 537
164 1 croak("Punk::Queue: empty cron expression");
165  
166 /* the deliberate exclusions, rejected by name */
167 {
168 const char *q;
169 5787 ▶ for (q = p; q < end; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q < end; q++) { 5255 532 5787
170 5255 ▶ if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?')
Branch: 4 branches
BranchTrueFalseTotal
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') 5254 1 5255
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') 5253 1 5254
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') 5252 1 5253
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') 1 5251 5252
171 4 croak("Punk::Queue: cron token '%c' is not supported "
172 "(L, W, # and ? are excluded in this release)", *q);
173 }
174 }
175  
176 532 ▶ if (*p == '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '@') { 16 516 532
177 static const struct { const char *name; const char *expr; }
178 aliases[] = {
179 { "@yearly", "0 0 1 1 *" }, { "@annually", "0 0 1 1 *" },
180 { "@monthly", "0 0 1 * *" }, { "@weekly", "0 0 * * 0" },
181 { "@daily", "0 0 * * *" }, { "@midnight", "0 0 * * *" },
182 { "@hourly", "0 * * * *" },
183 };
184 16 size_t n = (size_t)(end - p);
185 unsigned int k;
186 97 ▶ for (k = 0; k < sizeof aliases / sizeof *aliases; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < sizeof aliases / sizeof *aliases; k++) { 90 7 97
187 90 ▶ if (n == strlen(aliases[k].name)
Branch: 1 branch
BranchTrueFalseTotal
if (n == strlen(aliases[k].name) 21 69 90
188 21 ▶ && memEQ(p, aliases[k].name, n)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(p, aliases[k].name, n)) { 9 12 21
189 9 pq_cron_parse(aTHX_ aliases[k].expr,
190 9 strlen(aliases[k].expr), c);
191 14 return;
192 }
193 }
194 7 ▶ if (n > 7 && memEQ(p, "@every ", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n > 7 && memEQ(p, "@every ", 7)) { 7 0 7
if (n > 7 && memEQ(p, "@every ", 7)) { 6 1 7
195 6 const char *q = p + 7;
196 6 IV v = 0;
197 14 ▶ while (q < end && isDIGIT((U8)*q)) v = v * 10 + (*q++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (q < end && isDIGIT((U8)*q)) v = v * 10 + (*q++ - '0'); 14 0 14
while (q < end && isDIGIT((U8)*q)) v = v * 10 + (*q++ - '0'); 8 6 14
198 6 ▶ if (v > 0 && q + 1 == end) {
Branch: 2 branches
BranchTrueFalseTotal
if (v > 0 && q + 1 == end) { 6 0 6
if (v > 0 && q + 1 == end) { 6 0 6
199 10 ▶ IV mult = *q == 's' ? 1 : *q == 'm' ? 60
Branch: 2 branches
BranchTrueFalseTotal
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 5 1 6
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 4 1 5
200 4 ▶ : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0;
Branch: 2 branches
BranchTrueFalseTotal
: *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 2 2 4
: *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 1 1 2
201 6 ▶ if (mult) {
Branch: 1 branch
BranchTrueFalseTotal
if (mult) { 5 1 6
202 5 c->every = v * mult;
203 5 return;
204 }
205 }
206 1 croak("Punk::Queue: @every wants '<n>(s|m|h|d)', not '%.*s'",
207 (int)(end - p), p);
208 }
209 1 croak("Punk::Queue: unknown cron alias '%.*s'", (int)(end - p), p);
210 }
211  
212 /* split five whitespace-separated fields */
213 3089 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 2576 513 3089
214 4633 ▶ while (p < end && isSPACE((U8)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE((U8)*p)) p++; 4630 3 4633
while (p < end && isSPACE((U8)*p)) p++; 2057 2573 4630
215 2576 ▶ if (p >= end)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) 3 2573 2576
216 3 croak("Punk::Queue: cron expression has %d field(s), needs 5 "
217 "(minute hour day-of-month month day-of-week)", i);
218 2573 fs[i] = p;
219 5612 ▶ while (p < end && !isSPACE((U8)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && !isSPACE((U8)*p)) p++; 5097 515 5612
while (p < end && !isSPACE((U8)*p)) p++; 3039 2058 5097
220 2573 fe[i] = p;
221 }
222 514 ▶ while (p < end && isSPACE((U8)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE((U8)*p)) p++; 2 512 514
while (p < end && isSPACE((U8)*p)) p++; 1 1 2
223 513 ▶ if (p < end)
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) 1 512 513
224 1 croak("Punk::Queue: cron expression has more than 5 fields - "
225 "second and year fields are not supported");
226  
227 {
228 512 const char *err = NULL;
229 pq_u64 m;
230 512 ▶ if (pq_cron_field(aTHX_ fs[0], fe[0], 0, 59, NULL, 0, &m, &err))
Branch: 1 branch
BranchTrueFalseTotal
if (pq_cron_field(aTHX_ fs[0], fe[0], 0, 59, NULL, 0, &m, &err)) 1 511 512
231 1 croak("Punk::Queue: bad cron minute field near '%.8s'", err);
232 511 c->minute = m & PQ_CRON_ALL_MIN;
233 511 ▶ if (pq_cron_field(aTHX_ fs[1], fe[1], 0, 23, NULL, 0, &m, &err))
Branch: 1 branch
BranchTrueFalseTotal
if (pq_cron_field(aTHX_ fs[1], fe[1], 0, 23, NULL, 0, &m, &err)) 1 510 511
234 1 croak("Punk::Queue: bad cron hour field near '%.8s'", err);
235 510 c->hour = (U32)m & PQ_CRON_ALL_HOUR;
236 510 ▶ if (pq_cron_field(aTHX_ fs[2], fe[2], 1, 31, NULL, 0, &m, &err))
Branch: 1 branch
BranchTrueFalseTotal
if (pq_cron_field(aTHX_ fs[2], fe[2], 1, 31, NULL, 0, &m, &err)) 2 508 510
237 2 croak("Punk::Queue: bad cron day-of-month field near '%.8s'",
238 err);
239 508 c->dom = (U32)m & PQ_CRON_ALL_DOM;
240 508 ▶ if (pq_cron_field(aTHX_ fs[3], fe[3], 1, 12,
Branch: 1 branch
BranchTrueFalseTotal
if (pq_cron_field(aTHX_ fs[3], fe[3], 1, 12, 1 507 508
241 PQ_CRON_MONTHS, 12, &m, &err))
242 1 croak("Punk::Queue: bad cron month field near '%.8s'", err);
243 507 c->mon = (U16)m & PQ_CRON_ALL_MON;
244 507 ▶ if (pq_cron_field(aTHX_ fs[4], fe[4], 0, 7,
Branch: 1 branch
BranchTrueFalseTotal
if (pq_cron_field(aTHX_ fs[4], fe[4], 0, 7, 1 506 507
245 PQ_CRON_DAYS, 7, &m, &err))
246 1 croak("Punk::Queue: bad cron day-of-week field near '%.8s'",
247 err);
248 /* dow 7 is Sunday in half the world's crontabs: fold bit 7 into
249 * bit 0 AFTER the mask is built. (Folding per-atom was the bug
250 * that turned `*` - which expands to the range 0-7 - into
251 * Sunday-only, quietly restricting every cron to one weekday.) */
252 506 m |= (m >> 7) & 1;
253 506 c->dow = (U8)m & PQ_CRON_ALL_DOW;
254 }
255  
256 506 ▶ if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow)
Branch: 5 branches
BranchTrueFalseTotal
if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow) 506 0 506
if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow) 506 0 506
if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow) 506 0 506
if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow) 506 0 506
if (!c->minute || !c->hour || !c->dom || !c->mon || !c->dow) 0 506 506
257 0 croak("Punk::Queue: a cron field matches nothing");
258  
259 506 c->dom_restricted = (c->dom != PQ_CRON_ALL_DOM);
260 506 c->dow_restricted = (c->dow != PQ_CRON_ALL_DOW);
261 }
262  
263 /* ---- timezone-aware civil time ---------------------------------------------
264 *
265 * tz is 'UTC' (default), 'local' (POSIX mktime/localtime_r, honouring the
266 * process TZ), or a fixed '+HHMM'/'-HHMM'. No timezone database: UTC and
267 * fixed offsets use our own civil-date arithmetic; only 'local' touches
268 * libc, because only 'local' has DST. With Time::Keeper::Recurrence
269 * installed (pq_tkr.h), any other tz is tried as an IANA zone name, and a
270 * cron in one is walked by it. */
271  
272 typedef struct pq_civil {
273 int year, mon, mday, hour, min, wday; /* mon 1-12, wday 0=Sun */
274 } pq_civil;
275  
276 enum { PQ_TZ_UTC, PQ_TZ_LOCAL, PQ_TZ_FIXED, PQ_TZ_NAMED };
277  
278 typedef struct pq_tz {
279 int kind;
280 IV offset; /* seconds east of UTC, PQ_TZ_FIXED only */
281 const char *name; /* PQ_TZ_NAMED: the zone name, in the caller's SV */
282 STRLEN nlen;
283 } pq_tz;
284  
285 511 static void pq_tz_parse(pTHX_ const char *tz, STRLEN len, pq_tz *out) {
286 511 out->kind = PQ_TZ_UTC;
287 511 out->offset = 0;
288 511 out->name = NULL;
289 511 out->nlen = 0;
290 511 ▶ if (!tz || !len || (len == 3 && memEQ(tz, "UTC", 3))) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!tz || !len || (len == 3 && memEQ(tz, "UTC", 3))) return; 476 35 511
if (!tz || !len || (len == 3 && memEQ(tz, "UTC", 3))) return; 476 0 476
if (!tz || !len || (len == 3 && memEQ(tz, "UTC", 3))) return; 66 410 476
if (!tz || !len || (len == 3 && memEQ(tz, "UTC", 3))) return; 66 0 66
291 410 ▶ if (len == 5 && memEQ(tz, "local", 5)) { out->kind = PQ_TZ_LOCAL; return; }
Branch: 2 branches
BranchTrueFalseTotal
if (len == 5 && memEQ(tz, "local", 5)) { out->kind = PQ_TZ_LOCAL; return; } 3 407 410
if (len == 5 && memEQ(tz, "local", 5)) { out->kind = PQ_TZ_LOCAL; return; } 0 3 3
292 410 ▶ if (len == 5 && (tz[0] == '+' || tz[0] == '-')
Branch: 3 branches
BranchTrueFalseTotal
if (len == 5 && (tz[0] == '+' || tz[0] == '-') 3 407 410
if (len == 5 && (tz[0] == '+' || tz[0] == '-') 1 2 3
if (len == 5 && (tz[0] == '+' || tz[0] == '-') 1 0 1
293 3 ▶ && isDIGIT((U8)tz[1]) && isDIGIT((U8)tz[2])
Branch: 2 branches
BranchTrueFalseTotal
&& isDIGIT((U8)tz[1]) && isDIGIT((U8)tz[2]) 3 0 3
&& isDIGIT((U8)tz[1]) && isDIGIT((U8)tz[2]) 3 0 3
294 3 ▶ && isDIGIT((U8)tz[3]) && isDIGIT((U8)tz[4])) {
Branch: 2 branches
BranchTrueFalseTotal
&& isDIGIT((U8)tz[3]) && isDIGIT((U8)tz[4])) { 3 0 3
&& isDIGIT((U8)tz[3]) && isDIGIT((U8)tz[4])) { 3 0 3
295 3 IV hh = (tz[1] - '0') * 10 + (tz[2] - '0');
296 3 IV mm = (tz[3] - '0') * 10 + (tz[4] - '0');
297 3 ▶ if (hh <= 14 && mm <= 59) {
Branch: 2 branches
BranchTrueFalseTotal
if (hh <= 14 && mm <= 59) { 3 0 3
if (hh <= 14 && mm <= 59) { 3 0 3
298 3 out->kind = PQ_TZ_FIXED;
299 3 ▶ out->offset = (hh * 3600 + mm * 60) * (tz[0] == '-' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
out->offset = (hh * 3600 + mm * 60) * (tz[0] == '-' ? -1 : 1); 1 2 3
300 3 return;
301 }
302 }
303 {
304 407 const time_keeper_recurrence_abi *T = pq_tkr(aTHX);
305 407 ▶ if (T) {
Branch: 1 branch
BranchTrueFalseTotal
if (T) { 405 2 407
306 double d;
307 char err[160];
308 405 ▶ if ((T->cron_next)(aTHX_ "* * * * *", 9, tz, len, 0.0, &d, err, sizeof err) == TIME_KEEPER_RECURRENCE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ((T->cron_next)(aTHX_ "* * * * *", 9, tz, len, 0.0, &d, err, sizeof err) == TIME_KEEPER_RECURRENCE_OK) { 403 2 405
309 403 out->kind = PQ_TZ_NAMED;
310 403 out->name = tz;
311 403 out->nlen = len;
312 403 return;
313 }
314 2 croak("Punk::Queue: cron tz must be 'UTC', 'local', '+HHMM' or a "
315 "time zone name, not '%.*s'", (int)len, tz);
316 }
317 }
318 2 croak("Punk::Queue: cron tz must be 'UTC', 'local' or '+HHMM', "
319 "not '%.*s'", (int)len, tz);
320 }
321  
322 /* days-from-civil (Howard Hinnant's algorithm), and back */
323 1151 static IV pq_days_from_civil(int y, int m, int d) {
324 IV era, yoe, doy, doe;
325 1151 y -= m <= 2;
326 1151 ▶ era = (y >= 0 ? y : y - 399) / 400;
Branch: 1 branch
BranchTrueFalseTotal
era = (y >= 0 ? y : y - 399) / 400; 0 1151 1151
327 1151 yoe = y - era * 400;
328 1151 ▶ doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
Branch: 1 branch
BranchTrueFalseTotal
doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; 505 646 1151
329 1151 doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
330 1151 return era * 146097 + doe - 719468;
331 }
332  
333 2132 static void pq_civil_from_days(IV z, int *y, int *m, int *d) {
334 IV era, doe, yoe, doy, mp;
335 2132 z += 719468;
336 2132 ▶ era = (z >= 0 ? z : z - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
era = (z >= 0 ? z : z - 146096) / 146097; 0 2132 2132
337 2132 doe = z - era * 146097;
338 2132 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
339 2132 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
340 2132 mp = (5 * doy + 2) / 153;
341 2132 *d = (int)(doy - (153 * mp + 2) / 5 + 1);
342 2132 ▶ *m = (int)(mp + (mp < 10 ? 3 : -9));
Branch: 1 branch
BranchTrueFalseTotal
*m = (int)(mp + (mp < 10 ? 3 : -9)); 1397 735 2132
343 2132 *y = (int)(yoe + era * 400 + (*m <= 2));
344 2132 }
345  
346 /* epoch -> civil in tz */
347 2132 static void pq_civil_of(pTHX_ double epoch, const pq_tz *tz, pq_civil *cv) {
348 2132 ▶ if (tz->kind == PQ_TZ_LOCAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->kind == PQ_TZ_LOCAL) { 0 2132 2132
349 0 time_t t = (time_t)epoch;
350 struct tm tmv;
351 #ifdef WIN32
352 /* no localtime_r in the CRT, and its localtime is per-thread */
353 struct tm *p = localtime(&t);
354 if (p) tmv = *p; else memset(&tmv, 0, sizeof tmv);
355 #else
356 0 (void)localtime_r(&t, &tmv);
357 #endif
358 0 cv->year = tmv.tm_year + 1900;
359 0 cv->mon = tmv.tm_mon + 1;
360 0 cv->mday = tmv.tm_mday;
361 0 cv->hour = tmv.tm_hour;
362 0 cv->min = tmv.tm_min;
363 0 cv->wday = tmv.tm_wday;
364 0 return;
365 }
366 {
367 2132 IV t = (IV)epoch + tz->offset;
368 2132 IV days = t / 86400, secs = t % 86400;
369 2132 ▶ if (secs < 0) { secs += 86400; days--; }
Branch: 1 branch
BranchTrueFalseTotal
if (secs < 0) { secs += 86400; days--; } 0 2132 2132
370 2132 pq_civil_from_days(days, &cv->year, &cv->mon, &cv->mday);
371 2132 cv->hour = (int)(secs / 3600);
372 2132 cv->min = (int)((secs % 3600) / 60);
373 2132 cv->wday = (int)(((days % 7) + 11) % 7); /* epoch day 0 = Thu */
374 }
375 }
376  
377 /* civil midnight-of -> epoch, non-local frames only */
378 1151 static double pq_epoch_of_civil(const pq_tz *tz, int y, int m, int d,
379 int hh, int mi) {
380 1151 IV days = pq_days_from_civil(y, m, d);
381 1151 return (double)(days * 86400 + hh * 3600 + mi * 60) - (double)tz->offset;
382 }
383  
384 /* does this civil day satisfy the day fields, vixie OR included */
385 1717 static int pq_cron_day_ok(const pq_cron *c, const pq_civil *cv) {
386 1717 int dom_hit = (c->dom >> cv->mday) & 1;
387 1717 int dow_hit = (c->dow >> cv->wday) & 1;
388 1717 ▶ if (c->dom_restricted && c->dow_restricted) return dom_hit || dow_hit;
Branch: 4 branches
BranchTrueFalseTotal
if (c->dom_restricted && c->dow_restricted) return dom_hit || dow_hit; 1065 652 1717
if (c->dom_restricted && c->dow_restricted) return dom_hit || dow_hit; 29 1036 1065
if (c->dom_restricted && c->dow_restricted) return dom_hit || dow_hit; 16 13 29
if (c->dom_restricted && c->dow_restricted) return dom_hit || dow_hit; 13 3 16
389 1688 ▶ if (c->dom_restricted) return dom_hit;
Branch: 1 branch
BranchTrueFalseTotal
if (c->dom_restricted) return dom_hit; 1036 652 1688
390 652 ▶ if (c->dow_restricted) return dow_hit;
Branch: 1 branch
BranchTrueFalseTotal
if (c->dow_restricted) return dow_hit; 64 588 652
391 588 return 1;
392 }
393  
394 #define PQ_CRON_HORIZON (5.0 * 366.0 * 86400.0)
395  
396 /* The next occurrence strictly after `from`, or -1 when none exists
397 * within five years (which the parser uses to reject unmatchable specs).
398 *
399 * The walk is structured - skip non-matching months by month, days by
400 * day, hours by hour - so the worst case is thousands of steps, not the
401 * millions a minute-by-minute scan would take.
402 *
403 * DST, for tz 'local': the walk is over epoch seconds, which never
404 * produce a wall time inside the spring-forward gap, so an occurrence in
405 * the skipped hour simply does not fire. In the fall-back doubled hour
406 * the same wall minute exists at two epochs; a candidate whose wall
407 * minute equals `from`'s is skipped, so a pinned time fires once while
408 * a sweeping every-minute spec keeps firing every real minute - both the behaviours the
409 * plan promises. */
410 507 static double pq_cron_next(pTHX_ const pq_cron *c, double from,
411 const pq_tz *tz) {
412 double t;
413 pq_civil fromcv;
414 507 double limit = from + PQ_CRON_HORIZON;
415  
416 507 ▶ if (c->every > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->every > 0) 5 502 507
417 5 return from + (double)c->every;
418  
419 502 ▶ if (tz->kind == PQ_TZ_NAMED) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->kind == PQ_TZ_NAMED) { 402 100 502
420 /* a zone name: Time::Keeper::Recurrence walks the same grammar
421 * through the zone database */
422 402 const time_keeper_recurrence_abi *T = pq_tkr(aTHX);
423 double out;
424 char err[200];
425 int rc;
426 402 ▶ if (!T)
Branch: 1 branch
BranchTrueFalseTotal
if (!T) 0 402 402
427 0 croak("Punk::Queue: cron tz '%.*s' needs Time::Keeper::Recurrence",
428 (int)tz->nlen, tz->name);
429 402 rc = (T->cron_next)(aTHX_ c->src, c->srclen, tz->name, tz->nlen, from, &out, err, sizeof err);
430 402 ▶ if (rc == TIME_KEEPER_RECURRENCE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TIME_KEEPER_RECURRENCE_OK) 401 1 402
431 401 return out;
432 1 ▶ if (rc == TIME_KEEPER_RECURRENCE_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TIME_KEEPER_RECURRENCE_NONE) 1 0 1
433 1 return -1;
434 0 croak("Punk::Queue: cron '%.*s' in '%.*s': %s", (int)c->srclen, c->src,
435 (int)tz->nlen, tz->name, err);
436 }
437  
438 100 pq_civil_of(aTHX_ from, tz, &fromcv);
439  
440 /* first whole minute strictly after from */
441 100 t = (double)((IV)(from / 60) * 60 + 60);
442  
443 2036 ▶ while (t <= limit) {
Branch: 1 branch
BranchTrueFalseTotal
while (t <= limit) { 2032 4 2036
444 pq_civil cv;
445 2032 pq_civil_of(aTHX_ t, tz, &cv);
446  
447 2032 ▶ if (!((c->mon >> cv.mon) & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!((c->mon >> cv.mon) & 1)) { 315 1717 2032
448 /* first minute of the next month */
449 315 int y = cv.year, m = cv.mon + 1;
450 315 ▶ if (m > 12) { m = 1; y++; }
Branch: 1 branch
BranchTrueFalseTotal
if (m > 12) { m = 1; y++; } 28 287 315
451 315 ▶ if (tz->kind == PQ_TZ_LOCAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->kind == PQ_TZ_LOCAL) { 0 315 315
452 struct tm tmv;
453 0 memset(&tmv, 0, sizeof tmv);
454 0 tmv.tm_year = y - 1900; tmv.tm_mon = m - 1; tmv.tm_mday = 1;
455 0 tmv.tm_isdst = -1;
456 0 t = (double)mktime(&tmv);
457 }
458 315 else t = pq_epoch_of_civil(tz, y, m, 1, 0, 0);
459 1936 continue;
460 }
461 1717 ▶ if (!pq_cron_day_ok(c, &cv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_cron_day_ok(c, &cv)) { 836 881 1717
462 836 ▶ if (tz->kind == PQ_TZ_LOCAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->kind == PQ_TZ_LOCAL) { 0 836 836
463 struct tm tmv;
464 0 memset(&tmv, 0, sizeof tmv);
465 0 tmv.tm_year = cv.year - 1900; tmv.tm_mon = cv.mon - 1;
466 0 tmv.tm_mday = cv.mday + 1; tmv.tm_isdst = -1;
467 0 t = (double)mktime(&tmv);
468 }
469 836 else t = pq_epoch_of_civil(tz, cv.year, cv.mon, cv.mday, 0, 0)
470 + 86400.0;
471 836 continue;
472 }
473 881 ▶ if (!((c->hour >> cv.hour) & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!((c->hour >> cv.hour) & 1)) { 449 432 881
474 449 t += (double)((59 - cv.min) * 60 + 60);
475 449 continue;
476 }
477 432 ▶ if (!((c->minute >> cv.min) & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!((c->minute >> cv.min) & 1)) { 336 96 432
478 336 t += 60.0;
479 336 continue;
480 }
481  
482 /* the fall-back dedupe: same wall minute as `from` fires once */
483 96 ▶ if (tz->kind == PQ_TZ_LOCAL
Branch: 1 branch
BranchTrueFalseTotal
if (tz->kind == PQ_TZ_LOCAL 0 96 96
484 0 ▶ && cv.year == fromcv.year && cv.mon == fromcv.mon
Branch: 2 branches
BranchTrueFalseTotal
&& cv.year == fromcv.year && cv.mon == fromcv.mon 0 0 0
&& cv.year == fromcv.year && cv.mon == fromcv.mon 0 0 0
485 0 ▶ && cv.mday == fromcv.mday && cv.hour == fromcv.hour
Branch: 2 branches
BranchTrueFalseTotal
&& cv.mday == fromcv.mday && cv.hour == fromcv.hour 0 0 0
&& cv.mday == fromcv.mday && cv.hour == fromcv.hour 0 0 0
486 0 ▶ && cv.min == fromcv.min) {
Branch: 1 branch
BranchTrueFalseTotal
&& cv.min == fromcv.min) { 0 0 0
487 0 t += 60.0;
488 0 continue;
489 }
490 96 return t;
491 }
492 4 return -1.0;
493 }
494  
495 /* Parse + verify: the boot-time entry. Croaks on syntax AND on a spec
496 * that can never fire. */
497 55 static void pq_cron_compile(pTHX_ const char *expr, STRLEN elen,
498 const char *tz, STRLEN tlen,
499 pq_cron *c, pq_tz *z) {
500 55 pq_cron_parse(aTHX_ expr, elen, c);
501 39 pq_tz_parse(aTHX_ tz, tlen, z);
502 36 ▶ if (c->every == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->every == 0) { 36 0 36
503 36 double n = pq_cron_next(aTHX_ c, (double)time(NULL), z);
504 36 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 5 31 36
505 5 croak("Punk::Queue: cron '%.*s' can never fire "
506 "(no occurrence within five years)", (int)elen, expr);
507 }
508 31 }
509  
510 #endif /* PQ_CRON_H */
« include/pq_super.h ↑ Summary include/pq_time.h »