include/tkr/tkr_walk.h

filter: Statementstmt 95.5% 532 / 557 Branchbran 88.2% 561 / 636 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.6% 1093 / 1193 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/tkr/tkr_civil.h ↑ Summary include/tkr/tkr_cron.h »
1 #ifndef TKR_WALK_H
2 #define TKR_WALK_H
3  
4 /* The walk: RFC 5545's recurrence as a two-step machine. A period is a
5 * second, minute, hour, day, week, month or year, INTERVAL of them apart,
6 * counted from the start's period; inside a period the BY parts expand
7 * (a list of hours gives every one) or limit (a list of months keeps only
8 * days in them), in the RFC's table, and BYSETPOS keeps the nth of what is
9 * left. A day that does not exist (31 April, 30 February) is dropped, never
10 * moved. The start counts as the first occurrence whether or not it fits
11 * the rule, as the RFC says of COUNT.
12 *
13 * The expansion is an AND of filters over the days of the period (month,
14 * week number, day of year, day of month, weekday with its ordinal), with
15 * the start's own month, day or weekday standing in where a rule gives
16 * none, and a product of the hour, minute and second lists over each day
17 * kept. A cron reads its two day fields as either (TKR_F_VIXIE). Wall
18 * times are then made instants by tkr_resolve. */
19  
20 #define TKR_MAX_CAND 100000 /* candidates in one period */
21 #define TKR_EMPTY_DAYS_CRON 1830 /* days with nothing in them before "can never fire": a cron's five years, Punk::Queue's contract */
22 #define TKR_EMPTY_DAYS_RULE 18263 /* a rule's fifty years: a daily rule on a Wednesday the 14th of May waits six */
23 #define TKR_BETWEEN_MAX 10000000
24  
25 #define TKR_FILL_SKIP 2 /* tkr_fill: nothing until the period in *skip_to, the same day or the next */
26  
27 #define TKR_DD_NONE 0
28 #define TKR_DD_MONTHDAY 1 /* the start's day of the month (and its month, when no BYMONTH) */
29 #define TKR_DD_WEEKDAY 2 /* the start's weekday */
30  
31 typedef struct tkr_pending {
32 int64_t inst, wall, first; /* the later reading, its wall time, the earlier reading */
33 } tkr_pending;
34  
35 typedef struct tkr_walk {
36 tkr_rule rule; /* the walk's own copy: an iterator outlives the object it came from */
37 const tkr_rule *r;
38 int cron, has_start;
39 int64_t s_inst; /* the start's instant and its wall fields in the rule's zone */
40 int32_t s_nsec;
41 int64_t s_wall, s_days, s_year, s_week0, s_unit;
42 unsigned s_month, s_dom;
43 int s_h, s_m, s_s, s_wday;
44 uint64_t by_second, by_minute;
45 uint32_t by_hour;
46 int day_default, month_default;
47 int64_t k; /* the period being read */
48 int64_t *cand; /* the period's candidate wall times */
49 int ncand, ccap, ci;
50 int64_t *inst, *instw; /* the period's occurrences as instants, in order, and their wall times */
51 int ninst, icap, ii, ended;
52 tkr_pending *pend; /* second readings of doubled wall times, by instant */
53 int npend, pcap;
54 int64_t last_wall; /* the wall time of the last occurrence given out */
55 int64_t fail_wall; /* the skipped wall time, on TKR_W_FAIL */
56 int64_t cur_day; /* for a rule stepping hours, minutes or seconds: the day being read, and whether it gave anything */
57 int cur_day_had;
58 int64_t last; /* the last instant given out, so the start is not given twice */
59 int64_t emitted; /* occurrences given out, for COUNT */
60 int64_t empty;
61 int started; /* the start has been given out (or passed) */
62 int done;
63 int no_skip; /* walking back: read every period, take no jump forward */
64 /* a rule in another calendar (RFC 7529): the start read in it, the
65 * last month period found, the last two months and the last year read */
66 int64_t c_year;
67 int c_ord, c_mnum, c_leap, c_dom;
68 int mc_ok, mc_ord;
69 int64_t mc_step, mc_year;
70 struct tkr_calmonth_s { int64_t first, year; int dim, ord, mnum, leap, miy, diy, yday1; } cm[2];
71 int cm_ok[2], cm_next;
72 int cy_ok, cy_miy;
73 int64_t cy_year;
74 uint16_t cy_leaps;
75 } tkr_walk;
76  
77 6584657 ▶static int64_t tkr_floor_div(int64_t a, int64_t b) { int64_t q = a / b; return (a % b != 0 && ((a < 0) != (b < 0))) ? q - 1 : q; }
Branch: 2 branches
BranchTrueFalseTotal
static int64_t tkr_floor_div(int64_t a, int64_t b) { int64_t q = a / b; return (a % b != 0 && ((a < 0) != (b < 0))) ? q - 1 : q; } 6323774 260883 6584657
static int64_t tkr_floor_div(int64_t a, int64_t b) { int64_t q = a / b; return (a % b != 0 && ((a < 0) != (b < 0))) ? q - 1 : q; } 15 6323759 6323774
78  
79 /* How many days an empty period of the rule stands for, toward the cap. */
80 2434728 ▶static int64_t tkr_period_days(const tkr_rule *r) { return r->freq == TKR_YEARLY ? 366 : r->freq == TKR_MONTHLY ? 31 : r->freq == TKR_WEEKLY ? 7 : 1; }
Branch: 3 branches
BranchTrueFalseTotal
static int64_t tkr_period_days(const tkr_rule *r) { return r->freq == TKR_YEARLY ? 366 : r->freq == TKR_MONTHLY ? 31 : r->freq == TKR_WEEKLY ? 7 : 1; } 2433999 729 2434728
static int64_t tkr_period_days(const tkr_rule *r) { return r->freq == TKR_YEARLY ? 366 : r->freq == TKR_MONTHLY ? 31 : r->freq == TKR_WEEKLY ? 7 : 1; } 2425191 8808 2433999
static int64_t tkr_period_days(const tkr_rule *r) { return r->freq == TKR_YEARLY ? 366 : r->freq == TKR_MONTHLY ? 31 : r->freq == TKR_WEEKLY ? 7 : 1; } 10475 2414716 2425191
81 31072341 ▶static int64_t tkr_empty_cap(const tkr_rule *r) { return (r->flags & TKR_F_CRON) ? TKR_EMPTY_DAYS_CRON : TKR_EMPTY_DAYS_RULE; }
Branch: 1 branch
BranchTrueFalseTotal
static int64_t tkr_empty_cap(const tkr_rule *r) { return (r->flags & TKR_F_CRON) ? TKR_EMPTY_DAYS_CRON : TKR_EMPTY_DAYS_RULE; } 126916 30945425 31072341
82  
83  
84 /* ---- the days of a period ------------------------------------------------- */
85  
86 typedef struct tkr_dayinfo {
87 int64_t year;
88 unsigned month, dom, dim, yday, diy;
89 int wday; /* 0 Monday .. 6 Sunday */
90 } tkr_dayinfo;
91  
92 static void
93 53316127 tkr_dayinfo_of(int64_t day, tkr_dayinfo *d)
94 {
95 53316127 tkr_civil_from_days(day, &d->year, &d->month, &d->dom);
96 53316127 d->dim = tkr_days_in_month(d->year, d->month);
97 53316127 d->yday = (unsigned)(day - tkr_days_from_civil(d->year, 1, 1) + 1);
98 53316127 ▶ d->diy = tkr_is_leap(d->year) ? 366 : 365;
Branch: 1 branch
BranchTrueFalseTotal
d->diy = tkr_is_leap(d->year) ? 366 : 365; 12585938 40730189 53316127
99 53316127 d->wday = (int)tkr_weekday(day) - 1;
100 53316127 }
101  
102 /* The first day of week 1 of a year, weeks starting on wkst: the week that
103 * holds 4 January. */
104 static int64_t
105 24123 tkr_week1(int64_t year, int wkst)
106 {
107 24123 int64_t jan4 = tkr_days_from_civil(year, 1, 4);
108 24123 int w = (int)tkr_weekday(jan4) - 1;
109 24123 return jan4 - ((w - wkst + 7) % 7);
110 }
111  
112 /* The week number of a day, 1 to 53, and the year the week belongs to. */
113 static int
114 8034 tkr_weekno(int64_t day, int wkst, int64_t *wyear, int *weeks_in_year)
115 {
116 tkr_dayinfo d;
117 int64_t w1;
118 8034 tkr_dayinfo_of(day, &d);
119 8034 w1 = tkr_week1(d.year, wkst);
120 8034 ▶ if (day < w1) {
Branch: 1 branch
BranchTrueFalseTotal
if (day < w1) { 18 8016 8034
121 18 *wyear = d.year - 1;
122 18 w1 = tkr_week1(d.year - 1, wkst);
123 }
124 8016 ▶ else if (day >= tkr_week1(d.year + 1, wkst)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (day >= tkr_week1(d.year + 1, wkst)) { 21 7995 8016
125 21 *wyear = d.year + 1;
126 21 w1 = tkr_week1(d.year + 1, wkst);
127 }
128 else
129 7995 *wyear = d.year;
130 8034 *weeks_in_year = (int)((tkr_week1(*wyear + 1, wkst) - w1) / 7);
131 8034 return (int)((day - w1) / 7) + 1;
132 }
133  
134 static int
135 11835 tkr_in_list16(const int16_t *list, unsigned n, int v)
136 {
137 unsigned i;
138 44032 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 32287 11745 44032
139 32287 ▶ if (list[i] == v)
Branch: 1 branch
BranchTrueFalseTotal
if (list[i] == v) 90 32197 32287
140 90 return 1;
141 11745 return 0;
142 }
143  
144 /* Does the day pass the rule's day filters, in a period whose year is
145 * pyear (for the ordinal of a weekday within the year). */
146 static int
147 53308093 tkr_day_ok(const tkr_walk *w, int64_t day, int64_t pyear)
148 {
149 53308093 const tkr_rule *r = w->r;
150 tkr_dayinfo d;
151 53308093 int md_ok = 1, wd_ok = 1, has_md = 0, has_wd = 0;
152 53308093 tkr_dayinfo_of(day, &d);
153 53308093 ▶ if (r->by_month) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_month) { 395011 52913082 53308093
154 395011 ▶ if (!(r->by_month & (1u << d.month)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(r->by_month & (1u << d.month))) 291879 103132 395011
155 291879 return 0;
156 }
157 52913082 ▶ else if (w->month_default && d.month != w->s_month)
Branch: 2 branches
BranchTrueFalseTotal
else if (w->month_default && d.month != w->s_month) 82181 52830901 52913082
else if (w->month_default && d.month != w->s_month) 75267 6914 82181
158 75267 return 0;
159 52940947 ▶ if (r->n_weekno) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->n_weekno) { 8034 52932913 52940947
160 int64_t wy;
161 8034 int wiy, i, ok = 0;
162 8034 int no = tkr_weekno(day, r->wkst, &wy, &wiy);
163 8034 ▶ if (wy != pyear)
Branch: 1 branch
BranchTrueFalseTotal
if (wy != pyear) 39 7995 8034
164 7885 return 0;
165 15841 ▶ for (i = 0; i < r->n_weekno; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r->n_weekno; i++) 7995 7846 15841
166 7995 ▶ if (r->by_weekno[i] == no || (r->by_weekno[i] < 0 && wiy + 1 + r->by_weekno[i] == no)) { ok = 1; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (r->by_weekno[i] == no || (r->by_weekno[i] < 0 && wiy + 1 + r->by_weekno[i] == no)) { ok = 1; break; } 7855 140 7995
if (r->by_weekno[i] == no || (r->by_weekno[i] < 0 && wiy + 1 + r->by_weekno[i] == no)) { ok = 1; break; } 727 7128 7855
if (r->by_weekno[i] == no || (r->by_weekno[i] < 0 && wiy + 1 + r->by_weekno[i] == no)) { ok = 1; break; } 9 718 727
167 7995 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 7846 149 7995
168 7846 return 0;
169 }
170 52933062 ▶ if (r->n_yearday) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->n_yearday) { 5114 52927948 52933062
171 5114 ▶ if (!tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday) && !tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday - (int)d.diy - 1))
Branch: 2 branches
BranchTrueFalseTotal
if (!tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday) && !tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday - (int)d.diy - 1)) 5072 42 5114
if (!tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday) && !tkr_in_list16(r->by_yearday, r->n_yearday, (int)d.yday - (int)d.diy - 1)) 5072 0 5072
172 5072 return 0;
173 }
174 52927990 ▶ if (r->by_monthday) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_monthday) { 162983 52765007 52927990
175 162983 has_md = 1;
176 162983 ▶ md_ok = (r->by_monthday & (UINT64_C(1) << d.dom)) || (r->by_monthday & (UINT64_C(1) << (32 + (d.dim - d.dom + 1))));
Branch: 2 branches
BranchTrueFalseTotal
md_ok = (r->by_monthday & (UINT64_C(1) << d.dom)) || (r->by_monthday & (UINT64_C(1) << (32 + (d.dim - d.dom + 1)))); 130693 32290 162983
md_ok = (r->by_monthday & (UINT64_C(1) << d.dom)) || (r->by_monthday & (UINT64_C(1) << (32 + (d.dim - d.dom + 1)))); 45 130648 130693
177 }
178 52927990 ▶ if (tkr_has_by_day(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_has_by_day(r)) { 22991233 29936757 52927990
179 22991233 has_wd = 1;
180 22991233 wd_ok = (r->by_day_any & (1u << d.wday)) != 0;
181 22991233 ▶ if (!wd_ok && (r->by_day_pos[d.wday] || r->by_day_neg[d.wday])) {
Branch: 3 branches
BranchTrueFalseTotal
if (!wd_ok && (r->by_day_pos[d.wday] || r->by_day_neg[d.wday])) { 17934156 5057077 22991233
if (!wd_ok && (r->by_day_pos[d.wday] || r->by_day_neg[d.wday])) { 17932780 1376 17934156
if (!wd_ok && (r->by_day_pos[d.wday] || r->by_day_neg[d.wday])) { 104 17932676 17932780
182 1480 ▶ int in_year = r->freq == TKR_YEARLY && !r->by_month;
Branch: 2 branches
BranchTrueFalseTotal
int in_year = r->freq == TKR_YEARLY && !r->by_month; 1042 438 1480
int in_year = r->freq == TKR_YEARLY && !r->by_month; 1042 0 1042
183 1480 ▶ unsigned pos = in_year ? (d.yday - 1) / 7 + 1 : (d.dom - 1) / 7 + 1;
Branch: 1 branch
BranchTrueFalseTotal
unsigned pos = in_year ? (d.yday - 1) / 7 + 1 : (d.dom - 1) / 7 + 1; 1042 438 1480
184 1480 ▶ unsigned neg = in_year ? (d.diy - d.yday) / 7 + 1 : (d.dim - d.dom) / 7 + 1;
Branch: 1 branch
BranchTrueFalseTotal
unsigned neg = in_year ? (d.diy - d.yday) / 7 + 1 : (d.dim - d.dom) / 7 + 1; 1042 438 1480
185 1480 ▶ wd_ok = (pos <= 53 && (r->by_day_pos[d.wday] & (UINT64_C(1) << pos))) || (neg <= 53 && (r->by_day_neg[d.wday] & (UINT64_C(1) << neg)));
Branch: 4 branches
BranchTrueFalseTotal
wd_ok = (pos <= 53 && (r->by_day_pos[d.wday] & (UINT64_C(1) << pos))) || (neg <= 53 && (r->by_day_neg[d.wday] & (UINT64_C(1) << neg))); 1480 0 1480
wd_ok = (pos <= 53 && (r->by_day_pos[d.wday] & (UINT64_C(1) << pos))) || (neg <= 53 && (r->by_day_neg[d.wday] & (UINT64_C(1) << neg))); 1384 96 1480
wd_ok = (pos <= 53 && (r->by_day_pos[d.wday] & (UINT64_C(1) << pos))) || (neg <= 53 && (r->by_day_neg[d.wday] & (UINT64_C(1) << neg))); 1384 0 1384
wd_ok = (pos <= 53 && (r->by_day_pos[d.wday] & (UINT64_C(1) << pos))) || (neg <= 53 && (r->by_day_neg[d.wday] & (UINT64_C(1) << neg))); 43 1341 1384
186 }
187 }
188 52927990 ▶ if (has_md && has_wd)
Branch: 2 branches
BranchTrueFalseTotal
if (has_md && has_wd) 162983 52765007 52927990
if (has_md && has_wd) 123034 39949 162983
189 123034 ▶ return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok);
Branch: 5 branches
BranchTrueFalseTotal
return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok); 54835 68199 123034
return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok); 34357 20478 54835
return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok); 34205 152 34357
return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok); 2541 65658 68199
return (r->flags & TKR_F_VIXIE) ? (md_ok || wd_ok) : (md_ok && wd_ok); 454 2087 2541
190 52804956 ▶ if (has_md)
Branch: 1 branch
BranchTrueFalseTotal
if (has_md) 39949 52765007 52804956
191 39949 return md_ok;
192 52765007 ▶ if (has_wd)
Branch: 1 branch
BranchTrueFalseTotal
if (has_wd) 22868199 29896808 52765007
193 22868199 return wd_ok;
194 29896808 switch (w->day_default) {
195 2438698 case TKR_DD_MONTHDAY: return d.dom == w->s_dom;
196 2323313 case TKR_DD_WEEKDAY: return d.wday == w->s_wday;
197 25134797 default: return 1;
198 }
199 }
200  
201 #include "tkr_rscale.h"
202  
203 /* ---- periods ------------------------------------------------------------- */
204  
205 /* The period k: its first and last+1 day for a day-level frequency, or
206 * the absolute unit (hour, minute or second since 1970) for a sub-day one. */
207 static void
208 31512490 tkr_period(pTHX_ const tkr_walk *w, int64_t k, int64_t *d0, int64_t *d1, int64_t *pyear, int64_t *unit)
209 {
210 31512490 const tkr_rule *r = w->r;
211 31512490 int64_t step = (int64_t)r->interval * k;
212 31512490 ▶ if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) { 230134 31282356 31512490
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) { 228099 2035 230134
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) { 7407 220692 228099
213 9442 *pyear = 0;
214 9442 *unit = 0;
215 9442 ▶ if (!tkr_cal_period(aTHX_ (tkr_walk *)w, k, d0, d1))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cal_period(aTHX_ (tkr_walk *)w, k, d0, d1)) 0 9442 9442
216 0 *d0 = *d1 = 0;
217 9442 return;
218 }
219 31503048 switch (r->freq) {
220 949 case TKR_YEARLY:
221 949 *pyear = w->s_year + step;
222 949 *d0 = tkr_days_from_civil(*pyear, 1, 1);
223 949 *d1 = tkr_days_from_civil(*pyear + 1, 1, 1);
224 949 break;
225 282387 case TKR_MONTHLY: {
226 282387 int64_t mi = w->s_year * 12 + (w->s_month - 1) + step, y = tkr_floor_div(mi, 12);
227 282387 unsigned m = (unsigned)(mi - y * 12) + 1;
228 282387 *pyear = y;
229 282387 *d0 = tkr_days_from_civil(y, m, 1);
230 282387 ▶ *d1 = m == 12 ? tkr_days_from_civil(y + 1, 1, 1) : tkr_days_from_civil(y, m + 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
*d1 = m == 12 ? tkr_days_from_civil(y + 1, 1, 1) : tkr_days_from_civil(y, m + 1, 1); 22835 259552 282387
231 282387 break;
232 }
233 2310491 case TKR_WEEKLY:
234 2310491 *d0 = w->s_week0 + 7 * step;
235 2310491 *d1 = *d0 + 7;
236 2310491 *pyear = 0;
237 2310491 break;
238 28031348 case TKR_DAILY:
239 28031348 *d0 = w->s_days + step;
240 28031348 *d1 = *d0 + 1;
241 28031348 *pyear = 0;
242 28031348 break;
243 877873 default:
244 877873 *unit = w->s_unit + step;
245 877873 *d0 = *d1 = 0;
246 877873 *pyear = 0;
247 877873 break;
248 }
249 }
250  
251 /* The period a wall time falls in, never before the start's. */
252 static int64_t
253 121687 tkr_k_of(pTHX_ const tkr_walk *w, int64_t wall)
254 {
255 121687 const tkr_rule *r = w->r;
256 121687 int64_t day = tkr_floor_div(wall, 86400), k = 0, y;
257 unsigned m, d;
258 121687 ▶ if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY))
Branch: 3 branches
BranchTrueFalseTotal
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 2810 118877 121687
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 1985 825 2810
if ((r->flags & TKR_F_RSCALE) && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 560 1425 1985
259 1385 return tkr_cal_k_of(aTHX_ (tkr_walk *)w, day);
260 120302 switch (r->freq) {
261 452 case TKR_YEARLY:
262 452 tkr_civil_from_days(day, &y, &m, &d);
263 452 k = tkr_floor_div(y - w->s_year, r->interval);
264 452 break;
265 28221 case TKR_MONTHLY:
266 28221 tkr_civil_from_days(day, &y, &m, &d);
267 28221 k = tkr_floor_div(y * 12 + (m - 1) - (w->s_year * 12 + (w->s_month - 1)), r->interval);
268 28221 break;
269 54621 case TKR_WEEKLY: {
270 54621 int wd = (int)tkr_weekday(day) - 1;
271 54621 int64_t w0 = day - ((wd - r->wkst + 7) % 7);
272 54621 k = tkr_floor_div((w0 - w->s_week0) / 7, r->interval);
273 54621 break;
274 }
275 29454 case TKR_DAILY: k = tkr_floor_div(day - w->s_days, r->interval); break;
276 1843 case TKR_HOURLY: k = tkr_floor_div(tkr_floor_div(wall, 3600) - w->s_unit, r->interval); break;
277 5711 case TKR_MINUTELY: k = tkr_floor_div(tkr_floor_div(wall, 60) - w->s_unit, r->interval); break;
278 0 default: k = tkr_floor_div(wall - w->s_unit, r->interval); break;
279 }
280 120302 return k < 0 ? 0 : k;
281 }
282  
283 /* ---- candidates --------------------------------------------------------- */
284  
285 static void
286 30352736 tkr_cand_push(pTHX_ tkr_walk *w, int64_t wall)
287 {
288 30352736 ▶ if (w->ncand >= w->ccap) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->ncand >= w->ccap) { 93735 30259001 30352736
289 93735 ▶ if (w->ccap >= TKR_MAX_CAND)
Branch: 1 branch
BranchTrueFalseTotal
if (w->ccap >= TKR_MAX_CAND) 1 93734 93735
290 1 croak("Time::Keeper::Recurrence: more than %d occurrences in one period of the rule", TKR_MAX_CAND);
291 93734 ▶ w->ccap = w->ccap ? w->ccap * 4 : 64;
Branch: 1 branch
BranchTrueFalseTotal
w->ccap = w->ccap ? w->ccap * 4 : 64; 146 93588 93734
292 93734 ▶ if (w->ccap > TKR_MAX_CAND)
Branch: 1 branch
BranchTrueFalseTotal
if (w->ccap > TKR_MAX_CAND) 1 93733 93734
293 1 w->ccap = TKR_MAX_CAND;
294 93734 Renew(w->cand, w->ccap, int64_t);
295 }
296 30352735 w->cand[w->ncand++] = wall;
297 30352735 }
298  
299 /* Every time of a day the rule's hour, minute and second lists give. */
300 static void
301 30251803 tkr_push_times(pTHX_ tkr_walk *w, int64_t day, uint32_t hours, uint64_t minutes, uint64_t seconds)
302 {
303 int h, m, s;
304 756295051 ▶ for (h = 0; h < 24; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < 24; h++) { 726043249 30251802 756295051
305 726043249 ▶ if (!(hours & (1u << h)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(hours & (1u << h))) 695790477 30252772 726043249
306 695790477 continue;
307 1845419032 ▶ for (m = 0; m < 60; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; m < 60; m++) { 1815166261 30252771 1845419032
308 1815166261 ▶ if (!(minutes & (UINT64_C(1) << m)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(minutes & (UINT64_C(1) << m))) 1784903525 30262736 1815166261
309 1784903525 continue;
310 1876289571 ▶ for (s = 0; s <= 60; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s <= 60; s++) 1846026836 30262735 1876289571
311 1846026836 ▶ if (seconds & (UINT64_C(1) << s))
Branch: 1 branch
BranchTrueFalseTotal
if (seconds & (UINT64_C(1) << s)) 30352736 1815674100 1846026836
312 30352736 tkr_cand_push(aTHX_ w, day * 86400 + h * 3600 + m * 60 + s);
313 }
314 }
315 30251802 }
316  
317 /* Fills the period's candidates, sorted. Returns 0 for a sub-day period
318 * whose day the rule does not want, with *skip_to the first period of the
319 * next day. */
320 static int
321 31072319 tkr_fill(pTHX_ tkr_walk *w, int64_t k, int64_t *skip_to)
322 {
323 31072319 const tkr_rule *r = w->r;
324 31072319 int64_t d0, d1, pyear, unit = 0, day;
325 31072319 w->ncand = 0;
326 31072319 w->ci = 0;
327 31072319 tkr_period(aTHX_ w, k, &d0, &d1, &pyear, &unit);
328 31302453 ▶ if (r->freq >= TKR_DAILY && (r->flags & TKR_F_RSCALE)) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->freq >= TKR_DAILY && (r->flags & TKR_F_RSCALE)) { 30634571 437748 31072319
if (r->freq >= TKR_DAILY && (r->flags & TKR_F_RSCALE)) { 230134 30404437 30634571
329 /* a calendar's year or month also takes the day moved on from its last month */
330 230134 ▶ int ym = r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY;
Branch: 2 branches
BranchTrueFalseTotal
int ym = r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY; 228099 2035 230134
int ym = r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY; 7407 220692 228099
331 230134 ▶ int64_t end = d1 + (ym && d1 > d0 && r->skip == TKR_SKIP_FORWARD);
Branch: 3 branches
BranchTrueFalseTotal
int64_t end = d1 + (ym && d1 > d0 && r->skip == TKR_SKIP_FORWARD); 9442 220692 230134
int64_t end = d1 + (ym && d1 > d0 && r->skip == TKR_SKIP_FORWARD); 9442 0 9442
int64_t end = d1 + (ym && d1 > d0 && r->skip == TKR_SKIP_FORWARD); 3179 6263 9442
332 1469851 ▶ for (day = d0; day < end; day++)
Branch: 1 branch
BranchTrueFalseTotal
for (day = d0; day < end; day++) 1239717 230134 1469851
333 1239717 ▶ if (tkr_cal_day_ok(aTHX_ w, day, !ym ? 1 : day == d0 ? 0 : day == d1 ? 2 : 1))
Branch: 4 branches
BranchTrueFalseTotal
if (tkr_cal_day_ok(aTHX_ w, day, !ym ? 1 : day == d0 ? 0 : day == d1 ? 2 : 1)) 960777 278940 1239717
if (tkr_cal_day_ok(aTHX_ w, day, !ym ? 1 : day == d0 ? 0 : day == d1 ? 2 : 1)) 951335 9442 960777
if (tkr_cal_day_ok(aTHX_ w, day, !ym ? 1 : day == d0 ? 0 : day == d1 ? 2 : 1)) 3179 948156 951335
if (tkr_cal_day_ok(aTHX_ w, day, !ym ? 1 : day == d0 ? 0 : day == d1 ? 2 : 1)) 21929 1217788 1239717
334 21929 tkr_push_times(aTHX_ w, day, w->by_hour, w->by_minute, w->by_second);
335 }
336 30842185 ▶ else if (r->freq >= TKR_DAILY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->freq >= TKR_DAILY) { 30404437 437748 30842185
337 83274781 ▶ for (day = d0; day < d1; day++)
Branch: 1 branch
BranchTrueFalseTotal
for (day = d0; day < d1; day++) 52870345 30404436 83274781
338 52870345 ▶ if (tkr_day_ok(w, day, pyear))
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_day_ok(w, day, pyear)) 30219414 22650931 52870345
339 30219414 tkr_push_times(aTHX_ w, day, w->by_hour, w->by_minute, w->by_second);
340 }
341 else {
342 437748 ▶ int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400;
Branch: 2 branches
BranchTrueFalseTotal
int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400; 427783 9965 437748
int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400; 427783 0 427783
343 int h, m, s;
344 437748 day = tkr_floor_div(unit, per_day);
345 437748 ▶ if ((r->flags & TKR_F_RSCALE) ? !tkr_cal_day_ok(aTHX_ w, day, 1) : !tkr_day_ok(w, day, 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((r->flags & TKR_F_RSCALE) ? !tkr_cal_day_ok(aTHX_ w, day, 1) : !tkr_day_ok(w, day, 0)) { 0 437748 437748
if ((r->flags & TKR_F_RSCALE) ? !tkr_cal_day_ok(aTHX_ w, day, 1) : !tkr_day_ok(w, day, 0)) { 36628 401120 437748
346 36628 int64_t next_unit = (day + 1) * per_day;
347 36628 *skip_to = k + tkr_floor_div(next_unit - unit + r->interval - 1, r->interval);
348 36628 return 0;
349 }
350 401120 ▶ if (r->interval == 1 && !w->no_skip && (r->freq == TKR_HOURLY || r->freq == TKR_MINUTELY)) {
Branch: 4 branches
BranchTrueFalseTotal
if (r->interval == 1 && !w->no_skip && (r->freq == TKR_HOURLY || r->freq == TKR_MINUTELY)) { 400085 1035 401120
if (r->interval == 1 && !w->no_skip && (r->freq == TKR_HOURLY || r->freq == TKR_MINUTELY)) { 7054 393031 400085
if (r->interval == 1 && !w->no_skip && (r->freq == TKR_HOURLY || r->freq == TKR_MINUTELY)) { 5393 1661 7054
if (r->interval == 1 && !w->no_skip && (r->freq == TKR_HOURLY || r->freq == TKR_MINUTELY)) { 5393 0 5393
351 /* an hour or minute the rule does not want: on to the next it does, as a cron's masks say */
352 7054 int64_t target = -1;
353 int h2, m2;
354 7054 ▶ if (r->freq == TKR_HOURLY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq == TKR_HOURLY) { 1661 5393 7054
355 1661 h = (int)(unit - day * 24);
356 1661 ▶ if (r->by_hour && !(r->by_hour & (1u << h))) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->by_hour && !(r->by_hour & (1u << h))) { 1450 211 1661
if (r->by_hour && !(r->by_hour & (1u << h))) { 544 906 1450
357 4375 ▶ for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ; 4213 162 4375
for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ; 3831 382 4213
358 544 ▶ target = h2 < 24 ? day * 24 + h2 : (day + 1) * 24;
Branch: 1 branch
BranchTrueFalseTotal
target = h2 < 24 ? day * 24 + h2 : (day + 1) * 24; 382 162 544
359 }
360 }
361 else {
362 5393 int64_t sod = unit - day * 1440;
363 5393 h = (int)(sod / 60);
364 5393 m = (int)(sod % 60);
365 5393 ▶ if (r->by_hour && !(r->by_hour & (1u << h))) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->by_hour && !(r->by_hour & (1u << h))) { 3987 1406 5393
if (r->by_hour && !(r->by_hour & (1u << h))) { 467 3520 3987
366 3268 ▶ for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ; 3198 70 3268
for (h2 = h + 1; h2 < 24 && !(r->by_hour & (1u << h2)); h2++) ; 2801 397 3198
367 467 ▶ target = h2 < 24 ? day * 1440 + h2 * 60 : (day + 1) * 1440;
Branch: 1 branch
BranchTrueFalseTotal
target = h2 < 24 ? day * 1440 + h2 * 60 : (day + 1) * 1440; 397 70 467
368 }
369 4926 ▶ else if (r->by_minute && !(r->by_minute & (UINT64_C(1) << m))) {
Branch: 2 branches
BranchTrueFalseTotal
else if (r->by_minute && !(r->by_minute & (UINT64_C(1) << m))) { 3769 1157 4926
else if (r->by_minute && !(r->by_minute & (UINT64_C(1) << m))) { 1582 2187 3769
370 36097 ▶ for (m2 = m + 1; m2 < 60 && !(r->by_minute & (UINT64_C(1) << m2)); m2++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (m2 = m + 1; m2 < 60 && !(r->by_minute & (UINT64_C(1) << m2)); m2++) ; 35576 521 36097
for (m2 = m + 1; m2 < 60 && !(r->by_minute & (UINT64_C(1) << m2)); m2++) ; 34515 1061 35576
371 1582 ▶ if (m2 < 60)
Branch: 1 branch
BranchTrueFalseTotal
if (m2 < 60) 1061 521 1582
372 1061 target = day * 1440 + h * 60 + m2;
373 else {
374 4194 ▶ for (h2 = h + 1; h2 < 24 && r->by_hour && !(r->by_hour & (1u << h2)); h2++) ;
Branch: 3 branches
BranchTrueFalseTotal
for (h2 = h + 1; h2 < 24 && r->by_hour && !(r->by_hour & (1u << h2)); h2++) ; 3954 240 4194
for (h2 = h + 1; h2 < 24 && r->by_hour && !(r->by_hour & (1u << h2)); h2++) ; 3901 53 3954
for (h2 = h + 1; h2 < 24 && r->by_hour && !(r->by_hour & (1u << h2)); h2++) ; 3673 228 3901
375 521 ▶ target = h2 < 24 ? day * 1440 + h2 * 60 : (day + 1) * 1440;
Branch: 1 branch
BranchTrueFalseTotal
target = h2 < 24 ? day * 1440 + h2 * 60 : (day + 1) * 1440; 281 240 521
376 }
377 }
378 }
379 7054 ▶ if (target > unit) {
Branch: 1 branch
BranchTrueFalseTotal
if (target > unit) { 2593 4461 7054
380 2593 *skip_to = k + (target - unit);
381 2593 return TKR_FILL_SKIP;
382 }
383 }
384 398527 ▶ if (r->freq == TKR_HOURLY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq == TKR_HOURLY) { 6429 392098 398527
385 6429 h = (int)(unit - day * 24);
386 6429 ▶ if (!r->by_hour || (r->by_hour & (1u << h)))
Branch: 2 branches
BranchTrueFalseTotal
if (!r->by_hour || (r->by_hour & (1u << h))) 5691 738 6429
if (!r->by_hour || (r->by_hour & (1u << h))) 1752 3939 5691
387 2490 tkr_push_times(aTHX_ w, day, 1u << h, w->by_minute, w->by_second);
388 }
389 392098 ▶ else if (r->freq == TKR_MINUTELY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->freq == TKR_MINUTELY) { 392098 0 392098
390 392098 int64_t sod = unit - day * 1440;
391 392098 h = (int)(sod / 60);
392 392098 m = (int)(sod % 60);
393 392098 ▶ if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))))
Branch: 4 branches
BranchTrueFalseTotal
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m)))) 379193 12905 392098
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m)))) 40172 339021 379193
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m)))) 50494 2583 53077
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m)))) 5387 45107 50494
394 7970 tkr_push_times(aTHX_ w, day, 1u << h, UINT64_C(1) << m, w->by_second);
395 }
396 else {
397 0 int64_t sod = unit - day * 86400;
398 0 h = (int)(sod / 3600);
399 0 m = (int)(sod % 3600 / 60);
400 0 s = (int)(sod % 60);
401 0 ▶ if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m)))
Branch: 4 branches
BranchTrueFalseTotal
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) 0 0 0
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) 0 0 0
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) 0 0 0
if ((!r->by_hour || (r->by_hour & (1u << h))) && (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) 0 0 0
402 0 ▶ && (!r->by_second || (r->by_second & (UINT64_C(1) << s))))
Branch: 2 branches
BranchTrueFalseTotal
&& (!r->by_second || (r->by_second & (UINT64_C(1) << s)))) 0 0 0
&& (!r->by_second || (r->by_second & (UINT64_C(1) << s)))) 0 0 0
403 0 tkr_cand_push(aTHX_ w, day * 86400 + sod);
404 }
405 }
406 31033097 ▶ if (r->n_setpos && w->ncand) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->n_setpos && w->ncand) { 45 31033052 31033097
if (r->n_setpos && w->ncand) { 45 0 45
407 45 int i, kept = 0;
408 878 ▶ for (i = 0; i < w->ncand; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->ncand; i++) { 833 45 878
409 833 int pos = i + 1, neg = i - w->ncand;
410 833 ▶ if (tkr_in_list16(r->by_setpos, r->n_setpos, pos) || tkr_in_list16(r->by_setpos, r->n_setpos, neg))
Branch: 2 branches
BranchTrueFalseTotal
if (tkr_in_list16(r->by_setpos, r->n_setpos, pos) || tkr_in_list16(r->by_setpos, r->n_setpos, neg)) 816 17 833
if (tkr_in_list16(r->by_setpos, r->n_setpos, pos) || tkr_in_list16(r->by_setpos, r->n_setpos, neg)) 31 785 816
411 48 w->cand[kept++] = w->cand[i];
412 }
413 45 w->ncand = kept;
414 }
415 31033097 return 1;
416 }
417  
418 /* ---- the walk --------------------------------------------------------------- */
419  
420 #define TKR_W_END 0 /* the rule has ended */
421 #define TKR_W_GOT 1
422 #define TKR_W_NEVER -1 /* the rule can never fire: nothing within the cap */
423 #define TKR_W_FAIL -2 /* a skipped wall time, and the rule says fail */
424  
425 /* Set up to give occurrences in order from the period holding from_wall
426 * (a wall time) or, for a rule with COUNT, from the start, since the count
427 * must be taken from there. */
428 static void
429 94851 tkr_walk_init(pTHX_ tkr_walk *w, const tkr_rule *rule, int64_t from_wall, int from_start)
430 {
431 const tkr_rule *r;
432 time_keeper_civil c;
433 94851 memset(w, 0, sizeof *w);
434 94851 w->rule = *rule;
435 94851 w->r = r = &w->rule;
436 94851 w->cron = (r->flags & TKR_F_CRON) != 0;
437 94851 w->has_start = (r->flags & TKR_F_HAS_START) != 0;
438 94851 ▶ if (w->has_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->has_start) { 92676 2175 94851
439 92676 w->s_inst = r->start.sec;
440 92676 w->s_nsec = r->start.nsec;
441 92676 w->s_wall = tkr_wall_of(aTHX_ r, r->start.sec, &c);
442 92676 w->s_year = c.year; w->s_month = (unsigned)c.month; w->s_dom = (unsigned)c.day;
443 92676 w->s_h = c.hour; w->s_m = c.minute; w->s_s = c.second;
444 }
445 else {
446 /* a cron with no start counts its periods from 1970 */
447 2175 w->s_wall = 0;
448 2175 w->s_year = 1970; w->s_month = 1; w->s_dom = 1;
449 2175 w->s_h = w->s_m = w->s_s = 0;
450 }
451 94851 w->s_days = tkr_floor_div(w->s_wall, 86400);
452 94851 w->s_wday = (int)tkr_weekday(w->s_days) - 1;
453 94851 w->s_week0 = w->s_days - ((w->s_wday - r->wkst + 7) % 7);
454 94851 ▶ w->s_unit = r->freq == TKR_HOURLY ? tkr_floor_div(w->s_wall, 3600) : r->freq == TKR_MINUTELY ? tkr_floor_div(w->s_wall, 60) : w->s_wall;
Branch: 2 branches
BranchTrueFalseTotal
w->s_unit = r->freq == TKR_HOURLY ? tkr_floor_div(w->s_wall, 3600) : r->freq == TKR_MINUTELY ? tkr_floor_div(w->s_wall, 60) : w->s_wall; 1382 93469 94851
w->s_unit = r->freq == TKR_HOURLY ? tkr_floor_div(w->s_wall, 3600) : r->freq == TKR_MINUTELY ? tkr_floor_div(w->s_wall, 60) : w->s_wall; 3734 89735 93469
455 /* the RFC's defaults: the start's clock where the rule gives none, the
456 * start's day or weekday where a day-level rule gives no day part */
457 94851 ▶ w->by_hour = r->by_hour ? r->by_hour : r->freq > TKR_HOURLY ? (1u << w->s_h) : 0;
Branch: 2 branches
BranchTrueFalseTotal
w->by_hour = r->by_hour ? r->by_hour : r->freq > TKR_HOURLY ? (1u << w->s_h) : 0; 4463 90388 94851
w->by_hour = r->by_hour ? r->by_hour : r->freq > TKR_HOURLY ? (1u << w->s_h) : 0; 88985 1403 90388
458 94851 ▶ w->by_minute = r->by_minute ? r->by_minute : r->freq > TKR_MINUTELY ? (UINT64_C(1) << w->s_m) : 0;
Branch: 2 branches
BranchTrueFalseTotal
w->by_minute = r->by_minute ? r->by_minute : r->freq > TKR_MINUTELY ? (UINT64_C(1) << w->s_m) : 0; 4004 90847 94851
w->by_minute = r->by_minute ? r->by_minute : r->freq > TKR_MINUTELY ? (UINT64_C(1) << w->s_m) : 0; 89822 1025 90847
459 94851 ▶ w->by_second = r->by_second ? r->by_second : r->freq > TKR_SECONDLY ? (UINT64_C(1) << w->s_s) : 0;
Branch: 2 branches
BranchTrueFalseTotal
w->by_second = r->by_second ? r->by_second : r->freq > TKR_SECONDLY ? (UINT64_C(1) << w->s_s) : 0; 1 94850 94851
w->by_second = r->by_second ? r->by_second : r->freq > TKR_SECONDLY ? (UINT64_C(1) << w->s_s) : 0; 94850 0 94850
460 94851 ▶ if (w->cron)
Branch: 1 branch
BranchTrueFalseTotal
if (w->cron) 2180 92671 94851
461 2180 ▶ w->by_second = r->by_second ? r->by_second : 1; /* a cron fires on the minute */
Branch: 1 branch
BranchTrueFalseTotal
w->by_second = r->by_second ? r->by_second : 1; /* a cron fires on the minute */ 0 2180 2180
462 94851 w->day_default = TKR_DD_NONE;
463 94851 w->month_default = 0;
464 94851 ▶ if (r->freq >= TKR_WEEKLY && !w->cron) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->freq >= TKR_WEEKLY && !w->cron) { 66568 28283 94851
if (r->freq >= TKR_WEEKLY && !w->cron) { 66568 0 66568
465 66568 ▶ int any_day = r->n_weekno || r->n_yearday || r->by_monthday || tkr_has_by_day(r);
Branch: 4 branches
BranchTrueFalseTotal
int any_day = r->n_weekno || r->n_yearday || r->by_monthday || tkr_has_by_day(r); 66564 4 66568
int any_day = r->n_weekno || r->n_yearday || r->by_monthday || tkr_has_by_day(r); 66560 4 66564
int any_day = r->n_weekno || r->n_yearday || r->by_monthday || tkr_has_by_day(r); 65670 890 66560
int any_day = r->n_weekno || r->n_yearday || r->by_monthday || tkr_has_by_day(r); 51527 14143 65670
466 66568 ▶ if (!any_day) {
Branch: 1 branch
BranchTrueFalseTotal
if (!any_day) { 14143 52425 66568
467 14143 ▶ if (r->freq == TKR_WEEKLY)
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq == TKR_WEEKLY) 7314 6829 14143
468 7314 w->day_default = TKR_DD_WEEKDAY;
469 else {
470 6829 w->day_default = TKR_DD_MONTHDAY;
471 6829 ▶ if (r->freq == TKR_YEARLY && !r->by_month && !r->by_month_leap)
Branch: 3 branches
BranchTrueFalseTotal
if (r->freq == TKR_YEARLY && !r->by_month && !r->by_month_leap) 423 6406 6829
if (r->freq == TKR_YEARLY && !r->by_month && !r->by_month_leap) 286 137 423
if (r->freq == TKR_YEARLY && !r->by_month && !r->by_month_leap) 284 2 286
472 284 w->month_default = 1;
473 }
474 }
475 }
476 94851 ▶ if (r->flags & TKR_F_RSCALE)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_RSCALE) 3049 91802 94851
477 3049 tkr_cal_start(aTHX_ w);
478 94851 ▶ w->k = from_start || r->count ? 0 : tkr_k_of(aTHX_ w, from_wall);
Branch: 2 branches
BranchTrueFalseTotal
w->k = from_start || r->count ? 0 : tkr_k_of(aTHX_ w, from_wall); 91612 3239 94851
w->k = from_start || r->count ? 0 : tkr_k_of(aTHX_ w, from_wall); 91570 42 91612
479 94851 ▶ if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY))
Branch: 5 branches
BranchTrueFalseTotal
if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 78332 16519 94851
if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 1626 76706 78332
if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 282 1344 1626
if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 230 52 282
if (w->k > 0 && (r->flags & TKR_F_RSCALE) && r->skip == TKR_SKIP_FORWARD && (r->freq == TKR_YEARLY || r->freq == TKR_MONTHLY)) 72 158 230
480 124 w->k--; /* a day moved on to the 1st belongs to the period before */
481 94851 ▶ w->started = !(w->has_start && !w->cron) || (r->flags & TKR_F_EXRULE) != 0; /* an RRULE gives its start first; an exclusion does not */
Branch: 3 branches
BranchTrueFalseTotal
w->started = !(w->has_start && !w->cron) || (r->flags & TKR_F_EXRULE) != 0; /* an RRULE gives its start first; an exclusion does not */ 92676 2175 94851
w->started = !(w->has_start && !w->cron) || (r->flags & TKR_F_EXRULE) != 0; /* an RRULE gives its start first; an exclusion does not */ 92671 5 92676
w->started = !(w->has_start && !w->cron) || (r->flags & TKR_F_EXRULE) != 0; /* an RRULE gives its start first; an exclusion does not */ 45 92626 92671
482 94851 w->last = INT64_MIN;
483 94851 w->last_wall = INT64_MIN;
484 94851 w->cur_day = INT64_MIN;
485 94851 }
486  
487 static void
488 94832 tkr_walk_free(tkr_walk *w)
489 {
490 94832 ▶ if (w->cand)
Branch: 1 branch
BranchTrueFalseTotal
if (w->cand) 93587 1245 94832
491 93587 Safefree(w->cand);
492 94832 ▶ if (w->inst)
Branch: 1 branch
BranchTrueFalseTotal
if (w->inst) 93603 1229 94832
493 93603 Safefree(w->inst);
494 94832 ▶ if (w->instw)
Branch: 1 branch
BranchTrueFalseTotal
if (w->instw) 93603 1229 94832
495 93603 Safefree(w->instw);
496 94832 ▶ if (w->pend)
Branch: 1 branch
BranchTrueFalseTotal
if (w->pend) 24 94808 94832
497 24 Safefree(w->pend);
498 94832 w->cand = w->inst = NULL;
499 94832 w->pend = NULL;
500 94832 }
501  
502 /* Does an instant (with its wall time) fall within UNTIL. */
503 static int
504 30290754 tkr_within_until(const tkr_rule *r, int64_t inst, int64_t wall)
505 {
506 30290754 switch (r->until_kind) {
507 1094 case TKR_UNTIL_UTC: return inst <= r->until_sec;
508 8 case TKR_UNTIL_FLOATING: return wall <= r->until_sec;
509 20 case TKR_UNTIL_DATE: return wall < r->until_sec + 86400;
510 30289632 default: return 1;
511 }
512 }
513  
514 /* The second reading of a doubled wall time waits here until its instant
515 * comes round, so occurrences come out in order even when a wall time's
516 * later reading is after the next wall time's earlier one. */
517 static void
518 100 tkr_pend_push(pTHX_ tkr_walk *w, int64_t inst, int64_t wall, int64_t first)
519 {
520 int i, j;
521 100 ▶ if (w->npend >= w->pcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->npend >= w->pcap) { 26 74 100
522 26 ▶ w->pcap = w->pcap ? w->pcap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
w->pcap = w->pcap ? w->pcap * 2 : 16; 2 24 26
523 26 Renew(w->pend, w->pcap, tkr_pending);
524 }
525 1876 ▶ for (i = 0; i < w->npend && w->pend[i].inst <= inst; i++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < w->npend && w->pend[i].inst <= inst; i++) ; 1776 100 1876
for (i = 0; i < w->npend && w->pend[i].inst <= inst; i++) ; 1776 0 1776
526 100 ▶ for (j = w->npend; j > i; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = w->npend; j > i; j--) 0 100 100
527 0 w->pend[j] = w->pend[j - 1];
528 100 w->pend[i].inst = inst;
529 100 w->pend[i].wall = wall;
530 100 w->pend[i].first = first;
531 100 w->npend++;
532 100 }
533  
534 /* The period k as instants in order, in w->inst: its candidates on or
535 * after the start's wall time, resolved by the rule's gap and overlap
536 * choices, cut at UNTIL (w->ended says so); a doubled wall time's second
537 * reading goes to the pending list. TKR_W_GOT with a filled list (possibly
538 * empty), 0 with *skip_to for a sub-day period on a day the rule does not
539 * want, TKR_W_FAIL (with w->fail_wall) for a skipped wall time the rule
540 * will not have. */
541 static int
542 31072319 tkr_fill_instants(pTHX_ tkr_walk *w, int64_t k, int64_t *skip_to, int64_t *day)
543 {
544 31072319 const tkr_rule *r = w->r;
545 int i;
546 31072319 w->ninst = 0;
547 31072319 w->ended = 0;
548 31072319 ▶ if (r->freq < TKR_DAILY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq < TKR_DAILY) { 437748 30634571 31072319
549 437748 ▶ int64_t d0, d1, py, unit, per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400;
Branch: 2 branches
BranchTrueFalseTotal
int64_t d0, d1, py, unit, per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400; 427783 9965 437748
int64_t d0, d1, py, unit, per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400; 427783 0 427783
550 437748 tkr_period(aTHX_ w, k, &d0, &d1, &py, &unit);
551 437748 *day = tkr_floor_div(unit, per_day);
552 }
553 else
554 30634571 *day = 0;
555 {
556 31072319 int fr = tkr_fill(aTHX_ w, k, skip_to);
557 31072318 ▶ if (fr != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (fr != 1) 39221 31033097 31072318
558 39221 return fr;
559 }
560 31033097 ▶ if (w->icap < w->ccap + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->icap < w->ccap + 2) { 95372 30937725 31033097
561 95372 w->icap = w->ccap + 2;
562 95372 Renew(w->inst, w->icap, int64_t);
563 95372 Renew(w->instw, w->icap, int64_t);
564 }
565 61284953 ▶ for (i = 0; i < w->ncand; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->ncand; i++) { 30251886 31033067 61284953
566 30251886 int64_t wall = w->cand[i], out[2];
567 int n;
568 30251886 ▶ if (w->has_start && wall < w->s_wall)
Branch: 2 branches
BranchTrueFalseTotal
if (w->has_start && wall < w->s_wall) 30247073 4813 30251886
if (w->has_start && wall < w->s_wall) 28617 30218456 30247073
569 28628 continue;
570 30223269 n = tkr_resolve(aTHX_ r, wall, out);
571 30223269 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 2 30223267 30223269
572 2 w->fail_wall = wall;
573 30 return TKR_W_FAIL;
574 }
575 30223267 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 9 30223258 30223267
576 9 continue;
577 30223258 ▶ if (w->has_start && w->cron && out[0] <= w->s_inst) {
Branch: 3 branches
BranchTrueFalseTotal
if (w->has_start && w->cron && out[0] <= w->s_inst) { 30218451 4807 30223258
if (w->has_start && w->cron && out[0] <= w->s_inst) { 8 30218443 30218451
if (w->has_start && w->cron && out[0] <= w->s_inst) { 2 6 8
578 2 ▶ if (n < 2 || out[1] <= w->s_inst)
Branch: 2 branches
BranchTrueFalseTotal
if (n < 2 || out[1] <= w->s_inst) 0 2 2
if (n < 2 || out[1] <= w->s_inst) 0 0 0
579 2 continue; /* a cron's start is a bound, never an occurrence */
580 0 out[0] = out[1];
581 0 n = 1;
582 }
583 30223256 ▶ if (!tkr_within_until(r, out[0], wall)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_within_until(r, out[0], wall)) { 28 30223228 30223256
584 28 w->ended = 1;
585 28 return TKR_W_GOT;
586 }
587 30223228 w->inst[w->ninst] = out[0];
588 30223228 w->instw[w->ninst++] = wall;
589 30223228 ▶ if (n == 2 && tkr_within_until(r, out[1], wall))
Branch: 2 branches
BranchTrueFalseTotal
if (n == 2 && tkr_within_until(r, out[1], wall)) 100 30223128 30223228
if (n == 2 && tkr_within_until(r, out[1], wall)) 100 0 100
590 100 tkr_pend_push(aTHX_ w, out[1], wall, out[0]);
591 }
592 31033067 return TKR_W_GOT;
593 }
594  
595 /* The next occurrence in order, as an instant. */
596 static int
597 30130636 tkr_walk_next(pTHX_ tkr_walk *w, int64_t *inst)
598 {
599 30130636 const tkr_rule *r = w->r;
600 30130636 ▶ if (w->done)
Branch: 1 branch
BranchTrueFalseTotal
if (w->done) 0 30130636 30130636
601 0 return TKR_W_END;
602 30130636 ▶ if (r->count && w->emitted >= r->count) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->count && w->emitted >= r->count) { 1006 30129630 30130636
if (r->count && w->emitted >= r->count) { 85 921 1006
603 85 w->done = 1;
604 85 return TKR_W_END;
605 }
606 30130551 ▶ if (!w->started) { /* the start itself, first */
Branch: 1 branch
BranchTrueFalseTotal
if (!w->started) { /* the start itself, first */ 30066628 63923 30130551
607 63923 w->started = 1;
608 63923 ▶ if (tkr_within_until(r, w->s_inst, w->s_wall)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_within_until(r, w->s_inst, w->s_wall)) { 63923 0 63923
609 63923 w->emitted++;
610 63923 w->last = w->s_inst;
611 63923 w->last_wall = w->s_wall;
612 63923 *inst = w->s_inst;
613 63923 return TKR_W_GOT;
614 }
615 0 w->done = 1;
616 0 return TKR_W_END;
617 }
618 2459686 for (;;) {
619 int64_t got, wall;
620 32526314 int from_pend = 0;
621 32526314 ▶ if (w->ii >= w->ninst) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->ii >= w->ninst) { 30615078 1911236 32526314
622 30615078 ▶ if (w->ended) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->ended) { 28 30615050 30615078
623 28 ▶ if (!w->npend) {
Branch: 1 branch
BranchTrueFalseTotal
if (!w->npend) { 28 0 28
624 28 w->done = 1;
625 28 return TKR_W_END;
626 }
627 0 from_pend = 1;
628 }
629 else {
630 30615050 int64_t skip_to = 0, day;
631 int rc;
632 30615050 ▶ if (w->empty >= tkr_empty_cap(r))
Branch: 1 branch
BranchTrueFalseTotal
if (w->empty >= tkr_empty_cap(r)) 22 30615028 30615050
633 24 return TKR_W_NEVER;
634 30615028 rc = tkr_fill_instants(aTHX_ w, w->k, &skip_to, &day);
635 30615027 ▶ if (rc == TKR_W_FAIL)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TKR_W_FAIL) 2 30615025 30615027
636 2 return rc;
637 30615025 w->ii = 0;
638 30615025 ▶ if (rc == TKR_FILL_SKIP) { /* on to the next hour or minute the rule wants */
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TKR_FILL_SKIP) { /* on to the next hour or minute the rule wants */ 2593 30612432 30615025
639 2593 ▶ if (day != w->cur_day) {
Branch: 1 branch
BranchTrueFalseTotal
if (day != w->cur_day) { 765 1828 2593
640 765 ▶ if (w->cur_day != INT64_MIN && !w->cur_day_had)
Branch: 2 branches
BranchTrueFalseTotal
if (w->cur_day != INT64_MIN && !w->cur_day_had) 501 264 765
if (w->cur_day != INT64_MIN && !w->cur_day_had) 87 414 501
641 87 w->empty++;
642 765 w->cur_day = day;
643 765 w->cur_day_had = 0;
644 }
645 2593 w->k = skip_to;
646 2454299 continue;
647 }
648 30612432 ▶ if (rc == 0) { /* a whole day the rule does not want */
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { /* a whole day the rule does not want */ 36443 30575989 30612432
649 36443 w->empty++;
650 36443 w->k = skip_to;
651 36443 continue;
652 }
653 30575989 ▶ if (r->freq < TKR_DAILY) { /* count days that gave nothing, not periods */
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq < TKR_DAILY) { /* count days that gave nothing, not periods */ 5444 30570545 30575989
654 5444 ▶ if (day != w->cur_day) {
Branch: 1 branch
BranchTrueFalseTotal
if (day != w->cur_day) { 2261 3183 5444
655 2261 ▶ if (w->cur_day != INT64_MIN && !w->cur_day_had)
Branch: 2 branches
BranchTrueFalseTotal
if (w->cur_day != INT64_MIN && !w->cur_day_had) 25 2236 2261
if (w->cur_day != INT64_MIN && !w->cur_day_had) 1 24 25
656 1 w->empty++;
657 2261 w->cur_day = day;
658 2261 w->cur_day_had = 0;
659 }
660 5444 ▶ if (w->ninst)
Branch: 1 branch
BranchTrueFalseTotal
if (w->ninst) 5368 76 5444
661 5368 w->cur_day_had = 1;
662 }
663 30570545 ▶ else if (w->ninst == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (w->ninst == 0) 2415187 28155358 30570545
664 2415187 w->empty += tkr_period_days(r);
665 30575989 w->k++;
666 30575989 ▶ if (w->ii >= w->ninst && !(w->npend && w->ended))
Branch: 3 branches
BranchTrueFalseTotal
if (w->ii >= w->ninst && !(w->npend && w->ended)) 2415263 28160726 30575989
if (w->ii >= w->ninst && !(w->npend && w->ended)) 0 2415263 2415263
if (w->ii >= w->ninst && !(w->npend && w->ended)) 0 0 0
667 2415263 continue;
668 }
669 }
670 30071962 ▶ if (!from_pend && w->npend && (w->ii >= w->ninst || w->pend[0].inst <= w->inst[w->ii]))
Branch: 4 branches
BranchTrueFalseTotal
if (!from_pend && w->npend && (w->ii >= w->ninst || w->pend[0].inst <= w->inst[w->ii])) 30071962 0 30071962
if (!from_pend && w->npend && (w->ii >= w->ninst || w->pend[0].inst <= w->inst[w->ii])) 146 30071816 30071962
if (!from_pend && w->npend && (w->ii >= w->ninst || w->pend[0].inst <= w->inst[w->ii])) 146 0 146
if (!from_pend && w->npend && (w->ii >= w->ninst || w->pend[0].inst <= w->inst[w->ii])) 69 77 146
671 69 from_pend = 1;
672 30071962 ▶ if (from_pend) {
Branch: 1 branch
BranchTrueFalseTotal
if (from_pend) { 69 30071893 30071962
673 69 got = w->pend[0].inst;
674 69 wall = w->pend[0].wall;
675 69 w->npend--;
676 69 memmove(w->pend, w->pend + 1, (size_t)w->npend * sizeof *w->pend);
677 }
678 else {
679 30071893 got = w->inst[w->ii];
680 30071893 wall = w->instw[w->ii];
681 30071893 w->ii++;
682 }
683 30071962 ▶ if (w->has_start && !w->cron && got <= w->last)
Branch: 3 branches
BranchTrueFalseTotal
if (w->has_start && !w->cron && got <= w->last) 30070039 1923 30071962
if (w->has_start && !w->cron && got <= w->last) 30070033 6 30070039
if (w->has_start && !w->cron && got <= w->last) 5385 30064648 30070033
684 5385 continue; /* the start, already given, or a candidate before it */
685 30066577 ▶ if (w->cron && from_pend && wall == w->last_wall)
Branch: 3 branches
BranchTrueFalseTotal
if (w->cron && from_pend && wall == w->last_wall) 1929 30064648 30066577
if (w->cron && from_pend && wall == w->last_wall) 67 1862 1929
if (w->cron && from_pend && wall == w->last_wall) 2 65 67
686 2 continue; /* a pinned wall minute fires once in a doubled hour; a sweep every real minute */
687 30066575 w->empty = 0;
688 30066575 w->emitted++;
689 30066575 w->last = got;
690 30066575 w->last_wall = wall;
691 30066575 *inst = got;
692 30066575 return TKR_W_GOT;
693 }
694 }
695  
696 /* A cron in UTC, a fixed offset or floating time has no clock change to
697 * walk round, so its next occurrence is found from its masks directly: the
698 * next day its day fields allow, then the next hour and minute. A cron has
699 * no ordinals, year days, week numbers or seconds; its start is a bound. */
700 static int
701 28164 tkr_cron_day_ok(const tkr_rule *r, int64_t day)
702 {
703 int64_t y;
704 unsigned m, d;
705 28164 int md = -1, wd = -1;
706 28164 tkr_civil_from_days(day, &y, &m, &d);
707 28164 ▶ if (r->by_month && !(r->by_month & (1u << m)))
Branch: 2 branches
BranchTrueFalseTotal
if (r->by_month && !(r->by_month & (1u << m))) 26981 1183 28164
if (r->by_month && !(r->by_month & (1u << m))) 24571 2410 26981
708 24571 return 0;
709 3593 ▶ if (r->by_monthday)
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_monthday) 2853 740 3593
710 2853 md = (r->by_monthday & (UINT64_C(1) << d)) != 0;
711 3593 ▶ if (r->by_day_any)
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_day_any) 1299 2294 3593
712 1299 wd = (r->by_day_any & (1u << (tkr_weekday(day) - 1))) != 0;
713 3593 ▶ if (md >= 0 && wd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (md >= 0 && wd >= 0) 2853 740 3593
if (md >= 0 && wd >= 0) 789 2064 2853
714 789 ▶ return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd);
Branch: 5 branches
BranchTrueFalseTotal
return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd); 789 0 789
return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd); 638 151 789
return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd); 414 224 638
return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd); 0 0 0
return (r->flags & TKR_F_VIXIE) ? (md || wd) : (md && wd); 0 0 0
715 2804 ▶ return md >= 0 ? md : wd >= 0 ? wd : 1;
Branch: 2 branches
BranchTrueFalseTotal
return md >= 0 ? md : wd >= 0 ? wd : 1; 740 2064 2804
return md >= 0 ? md : wd >= 0 ? wd : 1; 510 230 740
716 }
717  
718 static int
719 7350 tkr_cron_fast_ok(const tkr_rule *r)
720 {
721 2071 ▶ return (r->flags & TKR_F_CRON) && !(r->flags & TKR_F_EVERY) && r->zone.kind != TIME_KEEPER_ZONE_NAMED
Branch: 2 branches
BranchTrueFalseTotal
return (r->flags & TKR_F_CRON) && !(r->flags & TKR_F_EVERY) && r->zone.kind != TIME_KEEPER_ZONE_NAMED 2071 0 2071
return (r->flags & TKR_F_CRON) && !(r->flags & TKR_F_EVERY) && r->zone.kind != TIME_KEEPER_ZONE_NAMED 1315 756 2071
722 1315 ▶ && !r->n_yearday && !r->n_weekno && !r->n_setpos && !tkr_has_by_day_ordinal(r)
Branch: 4 branches
BranchTrueFalseTotal
&& !r->n_yearday && !r->n_weekno && !r->n_setpos && !tkr_has_by_day_ordinal(r) 1315 0 1315
&& !r->n_yearday && !r->n_weekno && !r->n_setpos && !tkr_has_by_day_ordinal(r) 1315 0 1315
&& !r->n_yearday && !r->n_weekno && !r->n_setpos && !tkr_has_by_day_ordinal(r) 1315 0 1315
&& !r->n_yearday && !r->n_weekno && !r->n_setpos && !tkr_has_by_day_ordinal(r) 1315 0 1315
723 9421 ▶ && (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE;
Branch: 6 branches
BranchTrueFalseTotal
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 2071 5279 7350
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 0 1315 1315
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 0 0 0
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 1315 0 1315
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 1315 0 1315
&& (!r->by_second || r->by_second == 1) && r->interval == 1 && !r->count && r->until_kind == TKR_UNTIL_NONE; 1315 0 1315
724 }
725  
726 static int
727 1315 tkr_cron_fast_next(const tkr_rule *r, int64_t from, int inclusive, int64_t *inst)
728 {
729 1315 ▶ int64_t off = r->zone.kind == TIME_KEEPER_ZONE_FIXED ? r->zone.value : 0, mins, day, n;
Branch: 1 branch
BranchTrueFalseTotal
int64_t off = r->zone.kind == TIME_KEEPER_ZONE_FIXED ? r->zone.value : 0, mins, day, n; 415 900 1315
730 int mod;
731 1315 ▶ if ((r->flags & TKR_F_HAS_START) && from <= r->start.sec) {
Branch: 2 branches
BranchTrueFalseTotal
if ((r->flags & TKR_F_HAS_START) && from <= r->start.sec) { 3 1312 1315
if ((r->flags & TKR_F_HAS_START) && from <= r->start.sec) { 1 2 3
732 1 from = r->start.sec; /* a cron's start is a bound, never an occurrence */
733 1 inclusive = 0;
734 }
735 1315 mins = tkr_floor_div(from + off, 60);
736 1315 ▶ if (!inclusive || (from + off) - mins * 60 != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!inclusive || (from + off) - mins * 60 != 0) 3 1312 1315
if (!inclusive || (from + off) - mins * 60 != 0) 0 3 3
737 1312 mins++; /* the first whole minute strictly after */
738 1315 day = tkr_floor_div(mins, 1440);
739 1315 mod = (int)(mins - day * 1440);
740 28168 ▶ for (n = 0; n <= TKR_EMPTY_DAYS_CRON; n++, day++, mod = 0) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; n <= TKR_EMPTY_DAYS_CRON; n++, day++, mod = 0) { 28164 4 28168
741 int h, m;
742 28164 ▶ if (!tkr_cron_day_ok(r, day))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cron_day_ok(r, day)) 26735 1429 28164
743 26735 continue;
744 4611 ▶ for (h = mod / 60; h < 24; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = mod / 60; h < 24; h++) { 4493 118 4611
745 4493 ▶ if (r->by_hour && !(r->by_hour & (1u << h)))
Branch: 2 branches
BranchTrueFalseTotal
if (r->by_hour && !(r->by_hour & (1u << h))) 4028 465 4493
if (r->by_hour && !(r->by_hour & (1u << h))) 2991 1037 4028
746 2991 continue;
747 18023 ▶ for (m = h == mod / 60 ? mod % 60 : 0; m < 60; m++)
Branch: 2 branches
BranchTrueFalseTotal
for (m = h == mod / 60 ? mod % 60 : 0; m < 60; m++) 1104 398 1502
for (m = h == mod / 60 ? mod % 60 : 0; m < 60; m++) 17832 191 18023
748 17832 ▶ if (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) { 17471 361 17832
if (!r->by_minute || (r->by_minute & (UINT64_C(1) << m))) { 950 16521 17471
749 1311 *inst = (day * 1440 + h * 60 + m) * 60 - off;
750 1311 return TKR_W_GOT;
751 }
752 }
753 }
754 4 return TKR_W_NEVER;
755 }
756  
757 /* The first occurrence strictly after an instant (or at it, when
758 * inclusive). The walk starts as far before it in wall time as a clock
759 * change near it needs (tkr_lookback): a wall time's instant can be up to
760 * that much later than the wall time says, and a doubled wall time's
761 * second reading is later still. */
762 static int
763 7350 tkr_next_after(pTHX_ const tkr_rule *r, int64_t from, int inclusive, int64_t *inst, int64_t *detail)
764 {
765 tkr_walk w;
766 int64_t got, from_wall;
767 int rc;
768 7350 ▶ if (tkr_cron_fast_ok(r))
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_fast_ok(r)) 1315 6035 7350
769 1315 return tkr_cron_fast_next(r, from, inclusive, inst);
770 6035 from_wall = tkr_wall_of(aTHX_ r, from, NULL);
771 6035 tkr_walk_init(aTHX_ &w, r, from_wall - tkr_lookback(aTHX_ r, from), 0);
772 14692 ▶ while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) {
Branch: 1 branch
BranchTrueFalseTotal
while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { 14682 10 14692
773 14682 ▶ if (got > from || (inclusive && got == from)) {
Branch: 3 branches
BranchTrueFalseTotal
if (got > from || (inclusive && got == from)) { 10674 4008 14682
if (got > from || (inclusive && got == from)) { 4227 6447 10674
if (got > from || (inclusive && got == from)) { 2017 2210 4227
774 6025 *inst = got;
775 6025 break;
776 }
777 }
778 6035 ▶ if (rc == TKR_W_FAIL && detail)
Branch: 2 branches
BranchTrueFalseTotal
if (rc == TKR_W_FAIL && detail) 2 6033 6035
if (rc == TKR_W_FAIL && detail) 1 1 2
779 1 *detail = w.fail_wall;
780 6035 tkr_walk_free(&w);
781 6035 return rc;
782 }
783  
784 /* The last occurrence strictly before an instant: the periods walked back
785 * from the one holding it, each resolved in order and read from its end;
786 * a doubled wall time's second reading counts for a cron only when
787 * another occurrence lies between the two readings. A rule with COUNT is
788 * walked from its start. */
789 static int
790 30128 tkr_previous_before(pTHX_ const tkr_rule *r, int64_t from, int64_t *inst, int64_t *detail)
791 {
792 tkr_walk w;
793 30128 int64_t got, best = INT64_MIN, from_wall = tkr_wall_of(aTHX_ r, from, NULL), k, empty = 0, lb, limit = INT64_MIN;
794 30128 int found = 0, rc = TKR_W_END;
795 30128 ▶ if (r->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->count) { 11 30117 30128
796 11 tkr_walk_init(aTHX_ &w, r, from_wall, 1);
797 54 ▶ while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT && got < from) {
Branch: 2 branches
BranchTrueFalseTotal
while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT && got < from) { 52 2 54
while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT && got < from) { 43 9 52
798 43 best = got;
799 43 found = 1;
800 }
801 11 ▶ if (rc == TKR_W_FAIL && detail)
Branch: 2 branches
BranchTrueFalseTotal
if (rc == TKR_W_FAIL && detail) 0 11 11
if (rc == TKR_W_FAIL && detail) 0 0 0
802 0 *detail = w.fail_wall;
803 11 tkr_walk_free(&w);
804 11 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 11 11
805 0 return rc;
806 11 ▶ if (found)
Branch: 1 branch
BranchTrueFalseTotal
if (found) 10 1 11
807 10 *inst = best;
808 11 return found ? TKR_W_GOT : TKR_W_END;
809 }
810 30117 lb = tkr_lookback(aTHX_ r, from);
811 30117 tkr_walk_init(aTHX_ &w, r, from_wall + lb, 0);
812 30117 w.k = tkr_k_of(aTHX_ &w, from_wall + lb); /* walking back starts in the period itself */
813 458248 ▶ for (k = w.k; k >= 0; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = w.k; k >= 0; k--) { 457291 957 458248
814 int64_t skip_to, day;
815 int i;
816 457291 ▶ if (empty >= tkr_empty_cap(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (empty >= tkr_empty_cap(r)) { 0 457291 457291
817 0 tkr_walk_free(&w);
818 0 return TKR_W_NEVER;
819 }
820 457291 w.npend = 0;
821 457291 w.no_skip = 1;
822 457291 rc = tkr_fill_instants(aTHX_ &w, k, &skip_to, &day);
823 457291 ▶ if (rc == TKR_W_FAIL) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TKR_W_FAIL) { 0 457291 457291
824 0 ▶ if (detail)
Branch: 1 branch
BranchTrueFalseTotal
if (detail) 0 0 0
825 0 *detail = w.fail_wall;
826 0 tkr_walk_free(&w);
827 0 return rc;
828 }
829 457291 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 185 457106 457291
830 /* a sub-day period on a day the rule does not want: back to the last period of the day before */
831 185 ▶ int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400, d0, d1, py, unit;
Branch: 2 branches
BranchTrueFalseTotal
int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400, d0, d1, py, unit; 185 0 185
int64_t per_day = r->freq == TKR_HOURLY ? 24 : r->freq == TKR_MINUTELY ? 1440 : 86400, d0, d1, py, unit; 185 0 185
832 185 tkr_period(aTHX_ &w, k, &d0, &d1, &py, &unit);
833 185 k -= tkr_floor_div(unit - tkr_floor_div(unit, per_day) * per_day, r->interval);
834 185 empty++;
835 185 continue;
836 }
837 457106 ▶ if (r->freq < TKR_DAILY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->freq < TKR_DAILY) { 393083 64023 457106
838 393083 ▶ if (day != w.cur_day) {
Branch: 1 branch
BranchTrueFalseTotal
if (day != w.cur_day) { 2976 390107 393083
839 2976 ▶ if (w.cur_day != INT64_MIN && !w.cur_day_had)
Branch: 2 branches
BranchTrueFalseTotal
if (w.cur_day != INT64_MIN && !w.cur_day_had) 442 2534 2976
if (w.cur_day != INT64_MIN && !w.cur_day_had) 0 442 442
840 0 empty++;
841 2976 w.cur_day = day;
842 2976 w.cur_day_had = 0;
843 }
844 393083 ▶ if (w.ninst)
Branch: 1 branch
BranchTrueFalseTotal
if (w.ninst) 5064 388019 393083
845 5064 w.cur_day_had = 1;
846 }
847 64023 ▶ else if (w.ninst == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (w.ninst == 0) 19541 44482 64023
848 19541 empty += tkr_period_days(r);
849 /* the period's second readings count too, each after its first */
850 457129 ▶ for (i = w.npend - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = w.npend - 1; i >= 0; i--) { 23 457106 457129
851 23 int64_t second = w.pend[i].inst, first = w.pend[i].first, between;
852 23 ▶ if (second >= from || second <= best)
Branch: 2 branches
BranchTrueFalseTotal
if (second >= from || second <= best) 10 13 23
if (second >= from || second <= best) 5 5 10
853 19 continue;
854 5 ▶ if (w.cron) {
Branch: 1 branch
BranchTrueFalseTotal
if (w.cron) { 5 0 5
855 5 int rc2 = tkr_next_after(aTHX_ r, first, 0, &between, NULL);
856 5 ▶ if (rc2 != TKR_W_GOT || between >= second)
Branch: 2 branches
BranchTrueFalseTotal
if (rc2 != TKR_W_GOT || between >= second) 5 0 5
if (rc2 != TKR_W_GOT || between >= second) 1 4 5
857 1 continue; /* nothing between the two readings: the pinned minute fired once */
858 }
859 4 best = second;
860 4 found = 1;
861 }
862 502707 ▶ for (i = w.ninst - 1; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = w.ninst - 1; i >= 0; i--) 74831 427876 502707
863 74831 ▶ if (w.inst[i] < from) {
Branch: 1 branch
BranchTrueFalseTotal
if (w.inst[i] < from) { 29230 45601 74831
864 29230 ▶ if (w.inst[i] > best) {
Branch: 1 branch
BranchTrueFalseTotal
if (w.inst[i] > best) { 29159 71 29230
865 29159 best = w.inst[i];
866 29159 found = 1;
867 }
868 29230 break;
869 }
870 457106 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 31343 425763 457106
871 /* near a clock change an earlier wall time can be a later instant (the
872 * second reading of a doubled hour): walk back until no wall time can */
873 int64_t d0, d1, py, unit, pstart;
874 31343 ▶ if (!lb)
Branch: 1 branch
BranchTrueFalseTotal
if (!lb) 29105 2238 31343
875 29160 break;
876 2238 ▶ if (limit == INT64_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (limit == INT64_MIN) 55 2183 2238
877 55 limit = tkr_wall_of(aTHX_ r, best, NULL) - lb;
878 2238 tkr_period(aTHX_ &w, k, &d0, &d1, &py, &unit);
879 4522 pstart = r->freq >= TKR_DAILY ? d0 * 86400
880 2238 ▶ : r->freq == TKR_HOURLY ? unit * 3600 : r->freq == TKR_MINUTELY ? unit * 60 : unit;
Branch: 3 branches
BranchTrueFalseTotal
: r->freq == TKR_HOURLY ? unit * 3600 : r->freq == TKR_MINUTELY ? unit * 60 : unit; 46 2192 2238
: r->freq == TKR_HOURLY ? unit * 3600 : r->freq == TKR_MINUTELY ? unit * 60 : unit; 8 2184 2192
: r->freq == TKR_HOURLY ? unit * 3600 : r->freq == TKR_MINUTELY ? unit * 60 : unit; 2184 0 2184
881 2238 ▶ if (pstart < limit)
Branch: 1 branch
BranchTrueFalseTotal
if (pstart < limit) 55 2183 2238
882 55 break;
883 }
884 }
885 30117 ▶ if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best)
Branch: 5 branches
BranchTrueFalseTotal
if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best) 28703 1414 30117
if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best) 28703 0 28703
if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best) 28702 1 28703
if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best) 27746 956 28702
if (w.has_start && !w.cron && w.s_inst < from && (!found || w.s_inst > best) 0 27746 27746
886 956 ▶ && tkr_within_until(r, w.s_inst, w.s_wall)) {
Branch: 1 branch
BranchTrueFalseTotal
&& tkr_within_until(r, w.s_inst, w.s_wall)) { 956 0 956
887 956 best = w.s_inst;
888 956 found = 1;
889 }
890 30117 tkr_walk_free(&w);
891 30117 ▶ if (found)
Branch: 1 branch
BranchTrueFalseTotal
if (found) 30116 1 30117
892 30116 *inst = best;
893 30117 return found ? TKR_W_GOT : TKR_W_END;
894 }
895  
896 /* Can the rule fire at all: an RRULE's start is its first occurrence
897 * unless UNTIL is before it; a cron must have an occurrence within five
898 * years of now. 1, or 0 with err. */
899 static int
900 2946 tkr_can_fire(pTHX_ const tkr_rule *r, int64_t now, char *err)
901 {
902 int64_t got;
903 2946 ▶ if (r->flags & TKR_F_EVERY)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) 16 2930 2946
904 16 return 1;
905 2930 ▶ if (!(r->flags & TKR_F_CRON)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(r->flags & TKR_F_CRON)) { 2519 411 2930
906 2519 ▶ if (!tkr_within_until(r, r->start.sec, tkr_wall_of(aTHX_ r, r->start.sec, NULL))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_within_until(r, r->start.sec, tkr_wall_of(aTHX_ r, r->start.sec, NULL))) { 2 2517 2519
907 2 tkr_err(err, "the rule can never fire: UNTIL is before the start");
908 2 return 0;
909 }
910 2517 return 1;
911 }
912 411 ▶ if (tkr_next_after(aTHX_ r, now, 0, &got, NULL) != TKR_W_GOT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_next_after(aTHX_ r, now, 0, &got, NULL) != TKR_W_GOT) { 4 407 411
913 4 tkr_err(err, "can never fire (no occurrence within five years)");
914 4 return 0;
915 }
916 407 return 1;
917 }
918  
919 #endif
« include/tkr/tkr_civil.h ↑ Summary include/tkr/tkr_cron.h »