| 1 |
|
| #ifndef TKR_ABI_IMPL_H |
| 2 |
|
| #define TKR_ABI_IMPL_H |
| 3 |
|
| |
| 4 |
|
| /* The table time_keeper_recurrence_abi.h declares. A handle holds a |
| 5 |
|
| * reference to a rule or set object, so compile goes through the Perl |
| 6 |
|
| * constructors (under G_EVAL, their words becoming err) and every check |
| 7 |
|
| * they make is kept; next and previous, cron_next and rrule_next run the |
| 8 |
|
| * walk directly. Every entry returns a code. */ |
| 9 |
|
| |
| 10 |
|
| #define TKR_HANDLE_MAGIC 0x484b5254u |
| 11 |
|
| |
| 12 |
|
| struct time_keeper_recurrence { |
| 13 |
|
| uint32_t magic; |
| 14 |
|
| int is_set; |
| 15 |
|
| SV *obj; /* a reference to the rule or set */ |
| 16 |
|
| }; |
| 17 |
|
| |
| 18 |
|
| static void |
| 19 |
8 |
| tkra_err(char *err, size_t errlen, const char *fmt, ...) |
| 20 |
|
| { |
| 21 |
|
| va_list ap; |
| 22 |
8 |
▶ | if (!err || !errlen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!err || !errlen) |
8 |
0 |
8 |
| if (!err || !errlen) |
0 |
8 |
8 |
|
| 23 |
0 |
| return; |
| 24 |
8 |
| va_start(ap, fmt); |
| 25 |
8 |
| vsnprintf(err, errlen, fmt, ap); |
| 26 |
8 |
| va_end(ap); |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
|
| /* A die's words without its " at FILE line N." */ |
| 30 |
|
| static void |
| 31 |
2 |
| tkra_err_from_sv(pTHX_ SV *e, char *err, size_t errlen) |
| 32 |
|
| { |
| 33 |
|
| STRLEN len; |
| 34 |
2 |
| const char *s = SvPV(e, len), *at; |
| 35 |
6 |
▶ | while (len && (s[len - 1] == '\n' || s[len - 1] == '.')) |
Branch: 3 branches
| Branch | True | False | Total |
| while (len && (s[len - 1] == '\n' || s[len - 1] == '.')) |
6 |
0 |
6 |
| while (len && (s[len - 1] == '\n' || s[len - 1] == '.')) |
2 |
4 |
6 |
| while (len && (s[len - 1] == '\n' || s[len - 1] == '.')) |
2 |
2 |
4 |
|
| 36 |
4 |
| len--; |
| 37 |
46 |
▶ | for (at = s + len; at > s; at--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (at = s + len; at > s; at--) |
46 |
0 |
46 |
|
| 38 |
46 |
▶ | if (at + 4 <= s + len && memcmp(at, " at ", 4) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (at + 4 <= s + len && memcmp(at, " at ", 4) == 0) |
38 |
8 |
46 |
| if (at + 4 <= s + len && memcmp(at, " at ", 4) == 0) |
2 |
36 |
38 |
|
| 39 |
2 |
| break; |
| 40 |
2 |
▶ | if (at > s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (at > s) { |
2 |
0 |
2 |
|
| 41 |
2 |
| const char *ln = at; |
| 42 |
30 |
▶ | while (ln + 6 <= s + len && memcmp(ln, " line ", 6) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| while (ln + 6 <= s + len && memcmp(ln, " line ", 6) != 0) |
30 |
0 |
30 |
| while (ln + 6 <= s + len && memcmp(ln, " line ", 6) != 0) |
28 |
2 |
30 |
|
| 43 |
28 |
| ln++; |
| 44 |
2 |
▶ | if (ln + 6 <= s + len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ln + 6 <= s + len) { |
2 |
0 |
2 |
|
| 45 |
2 |
| const char *d = ln + 6; |
| 46 |
6 |
▶ | while (d < s + len && isDIGIT(*d)) d++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (d < s + len && isDIGIT(*d)) d++; |
4 |
2 |
6 |
| while (d < s + len && isDIGIT(*d)) d++; |
4 |
0 |
4 |
|
| 47 |
2 |
▶ | if (d == s + len || *d == ',') |
Branch: 2 branches
| Branch | True | False | Total |
| if (d == s + len || *d == ',') |
0 |
2 |
2 |
| if (d == s + len || *d == ',') |
0 |
0 |
0 |
|
| 48 |
2 |
| len = (STRLEN)(at - s); |
| 49 |
|
| } |
| 50 |
|
| } |
| 51 |
2 |
| tkra_err(err, errlen, "%.*s", (int)len, s); |
| 52 |
2 |
| } |
| 53 |
|
| |
| 54 |
|
| static time_keeper_recurrence * |
| 55 |
6 |
| tkra_handle_new(pTHX_ SV *obj) |
| 56 |
|
| { |
| 57 |
|
| time_keeper_recurrence *h; |
| 58 |
6 |
| Newx(h, 1, time_keeper_recurrence); |
| 59 |
6 |
| h->magic = TKR_HANDLE_MAGIC; |
| 60 |
6 |
| h->is_set = sv_derived_from(obj, "Time::Keeper::Recurrence::Set"); |
| 61 |
6 |
| h->obj = newSVsv(obj); |
| 62 |
6 |
| return h; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| static int |
| 66 |
25 |
| tkra_handle_ok(const time_keeper_recurrence *h) |
| 67 |
|
| { |
| 68 |
25 |
▶ | return h && h->magic == TKR_HANDLE_MAGIC && h->obj && SvROK(h->obj); |
Branch: 4 branches
| Branch | True | False | Total |
| return h && h->magic == TKR_HANDLE_MAGIC && h->obj && SvROK(h->obj); |
23 |
2 |
25 |
| return h && h->magic == TKR_HANDLE_MAGIC && h->obj && SvROK(h->obj); |
23 |
0 |
23 |
| return h && h->magic == TKR_HANDLE_MAGIC && h->obj && SvROK(h->obj); |
23 |
0 |
23 |
| return h && h->magic == TKR_HANDLE_MAGIC && h->obj && SvROK(h->obj); |
23 |
0 |
23 |
|
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* A constructor called as Perl would, with its die caught. */ |
| 72 |
|
| static int |
| 73 |
7 |
| tkra_construct(pTHX_ const char *method, SV **args, int nargs, SV **out, char *err, size_t errlen) |
| 74 |
|
| { |
| 75 |
7 |
| dSP; |
| 76 |
7 |
| int count, i, rc = TIME_KEEPER_RECURRENCE_BAD; |
| 77 |
7 |
| ENTER; |
| 78 |
7 |
| SAVETMPS; |
| 79 |
7 |
| save_scalar(PL_errgv); |
| 80 |
7 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
7 |
7 |
|
| 81 |
7 |
▶ | XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Recurrence"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Recurrence"))); |
0 |
7 |
7 |
|
| 82 |
32 |
▶ | for (i = 0; i < nargs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nargs; i++) |
25 |
7 |
32 |
|
| 83 |
25 |
▶ | XPUSHs(args[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(args[i]); |
0 |
25 |
25 |
|
| 84 |
7 |
| PUTBACK; |
| 85 |
7 |
| count = call_method(method, G_SCALAR | G_EVAL); |
| 86 |
7 |
| SPAGAIN; |
| 87 |
7 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
7 |
0 |
7 |
| if (SvTRUE(ERRSV)) |
2 |
5 |
7 |
|
| 88 |
2 |
▶ | tkra_err_from_sv(aTHX_ ERRSV, err, errlen); |
Branch: 1 branch
| Branch | True | False | Total |
| tkra_err_from_sv(aTHX_ ERRSV, err, errlen); |
2 |
0 |
2 |
|
| 89 |
5 |
▶ | else if (count == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count == 1) { |
5 |
0 |
5 |
|
| 90 |
5 |
| SV *got = POPs; |
| 91 |
5 |
▶ | if (SvROK(got)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(got)) { |
5 |
0 |
5 |
|
| 92 |
5 |
| *out = newSVsv(got); |
| 93 |
5 |
| rc = TIME_KEEPER_RECURRENCE_OK; |
| 94 |
|
| } |
| 95 |
|
| else |
| 96 |
0 |
| tkra_err(err, errlen, "%s gave no rule", method); |
| 97 |
|
| } |
| 98 |
7 |
| PUTBACK; |
| 99 |
7 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
7 |
0 |
7 |
|
| 100 |
7 |
| LEAVE; |
| 101 |
7 |
| return rc; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| static int |
| 105 |
10 |
| tkra_compile(pTHX_ int kind, const char *spec, size_t len, const time_keeper_zone *zone, |
| 106 |
|
| const time_keeper_instant *start, int gap, int overlap, |
| 107 |
|
| time_keeper_recurrence **out, char *err, size_t errlen) |
| 108 |
|
| { |
| 109 |
10 |
| const time_keeper_abi *T = tkr_tk(aTHX); |
| 110 |
|
| time_keeper_zone utc; |
| 111 |
10 |
| SV *args[10], *obj = NULL; |
| 112 |
10 |
| int n = 0, rc; |
| 113 |
10 |
▶ | if (err && errlen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && errlen) |
9 |
1 |
10 |
| if (err && errlen) |
9 |
0 |
9 |
|
| 114 |
9 |
| err[0] = '\0'; |
| 115 |
10 |
▶ | if (!spec || !out) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!spec || !out) { |
10 |
0 |
10 |
| if (!spec || !out) { |
0 |
10 |
10 |
|
| 116 |
0 |
| tkra_err(err, errlen, "compile: no spec or no place for the handle"); |
| 117 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 118 |
|
| } |
| 119 |
10 |
▶ | if (gap < -1 || gap > 3 || overlap < -1 || overlap > 2) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (gap < -1 || gap > 3 || overlap < -1 || overlap > 2) { |
10 |
0 |
10 |
| if (gap < -1 || gap > 3 || overlap < -1 || overlap > 2) { |
9 |
1 |
10 |
| if (gap < -1 || gap > 3 || overlap < -1 || overlap > 2) { |
9 |
0 |
9 |
| if (gap < -1 || gap > 3 || overlap < -1 || overlap > 2) { |
0 |
9 |
9 |
|
| 120 |
1 |
| tkra_err(err, errlen, "compile: gap is -1 to 3 and overlap -1 to 2"); |
| 121 |
1 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 122 |
|
| } |
| 123 |
9 |
| utc.kind = TIME_KEEPER_ZONE_UTC; |
| 124 |
9 |
| utc.value = 0; |
| 125 |
9 |
▶ | if (!zone) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!zone) |
1 |
8 |
9 |
|
| 126 |
1 |
| zone = &utc; |
| 127 |
9 |
| ENTER; |
| 128 |
9 |
| SAVETMPS; |
| 129 |
9 |
| switch (kind) { |
| 130 |
7 |
| case TIME_KEEPER_RECURRENCE_RRULE: |
| 131 |
|
| case TIME_KEEPER_RECURRENCE_CRON: { |
| 132 |
7 |
▶ | if (kind == TIME_KEEPER_RECURRENCE_RRULE && !start) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == TIME_KEEPER_RECURRENCE_RRULE && !start) { |
5 |
2 |
7 |
| if (kind == TIME_KEEPER_RECURRENCE_RRULE && !start) { |
1 |
4 |
5 |
|
| 133 |
1 |
| tkra_err(err, errlen, "compile: an RRULE needs a start"); |
| 134 |
1 |
| rc = TIME_KEEPER_RECURRENCE_BAD; |
| 135 |
1 |
| goto done; |
| 136 |
|
| } |
| 137 |
6 |
▶ | if (kind == TIME_KEEPER_RECURRENCE_CRON) |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == TIME_KEEPER_RECURRENCE_CRON) |
2 |
4 |
6 |
|
| 138 |
2 |
| args[n++] = sv_2mortal(newSVpvn(spec, len)); |
| 139 |
|
| else { |
| 140 |
4 |
| args[n++] = sv_2mortal(newSVpvs("rrule")); |
| 141 |
4 |
| args[n++] = sv_2mortal(newSVpvn(spec, len)); |
| 142 |
|
| } |
| 143 |
6 |
▶ | if (start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (start) { |
4 |
2 |
6 |
|
| 144 |
4 |
| SV *s = (T->new_sv)(aTHX_ start, zone, 0); |
| 145 |
4 |
▶ | if (!s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) { |
0 |
4 |
4 |
|
| 146 |
0 |
| tkra_err(err, errlen, "compile: the start is outside the years Time::Keeper holds"); |
| 147 |
0 |
| rc = TIME_KEEPER_RECURRENCE_RANGE; |
| 148 |
0 |
| goto done; |
| 149 |
|
| } |
| 150 |
4 |
| args[n++] = sv_2mortal(newSVpvs("start")); |
| 151 |
4 |
| args[n++] = sv_2mortal(s); |
| 152 |
|
| } |
| 153 |
|
| else { |
| 154 |
|
| tkr_rule probe; |
| 155 |
2 |
| tkr_rule_init(&probe); |
| 156 |
2 |
| probe.zone = *zone; |
| 157 |
2 |
| args[n++] = sv_2mortal(newSVpvs("zone")); |
| 158 |
2 |
| args[n++] = sv_2mortal(tkr_zone_name_sv(aTHX_ &probe)); |
| 159 |
|
| } |
| 160 |
6 |
▶ | if (gap >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gap >= 0) { |
1 |
5 |
6 |
|
| 161 |
1 |
| args[n++] = sv_2mortal(newSVpvs("gap")); |
| 162 |
1 |
| args[n++] = sv_2mortal(newSVpv(tkr_gap_names[gap], 0)); |
| 163 |
|
| } |
| 164 |
6 |
▶ | if (overlap >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (overlap >= 0) { |
0 |
6 |
6 |
|
| 165 |
0 |
| args[n++] = sv_2mortal(newSVpvs("overlap")); |
| 166 |
0 |
| args[n++] = sv_2mortal(newSVpv(tkr_overlap_names[overlap], 0)); |
| 167 |
|
| } |
| 168 |
6 |
▶ | rc = tkra_construct(aTHX_ kind == TIME_KEEPER_RECURRENCE_CRON ? "cron" : "new", args, n, &obj, err, errlen); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = tkra_construct(aTHX_ kind == TIME_KEEPER_RECURRENCE_CRON ? "cron" : "new", args, n, &obj, err, errlen); |
2 |
4 |
6 |
|
| 169 |
6 |
| break; |
| 170 |
|
| } |
| 171 |
1 |
| case TIME_KEEPER_RECURRENCE_ICAL: |
| 172 |
1 |
| args[n++] = sv_2mortal(newSVpvn(spec, len)); |
| 173 |
1 |
| rc = tkra_construct(aTHX_ "parse_ical", args, n, &obj, err, errlen); |
| 174 |
1 |
| break; |
| 175 |
1 |
| default: |
| 176 |
1 |
| tkra_err(err, errlen, "compile: kind %d is not RRULE, CRON or ICAL", kind); |
| 177 |
1 |
| rc = TIME_KEEPER_RECURRENCE_BAD; |
| 178 |
|
| } |
| 179 |
9 |
| done: |
| 180 |
9 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
7 |
2 |
9 |
|
| 181 |
9 |
| LEAVE; |
| 182 |
9 |
▶ | if (rc == TIME_KEEPER_RECURRENCE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == TIME_KEEPER_RECURRENCE_OK) { |
5 |
4 |
9 |
|
| 183 |
5 |
| *out = tkra_handle_new(aTHX_ obj); |
| 184 |
5 |
| SvREFCNT_dec(obj); |
| 185 |
|
| } |
| 186 |
9 |
| return rc; |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| static int |
| 190 |
5 |
| tkra_rc(int rc) |
| 191 |
|
| { |
| 192 |
5 |
| switch (rc) { |
| 193 |
0 |
| case TKR_W_GOT: return TIME_KEEPER_RECURRENCE_OK; |
| 194 |
1 |
| case TKR_W_FAIL: return TIME_KEEPER_RECURRENCE_GAP; |
| 195 |
4 |
| default: return TIME_KEEPER_RECURRENCE_NONE; |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
|
| /* next (dir 0) or previous (dir 1) on a handle. */ |
| 200 |
|
| static int |
| 201 |
22 |
| tkra_walk(pTHX_ const time_keeper_recurrence *h, const time_keeper_instant *from, time_keeper_instant *out, int dir) |
| 202 |
|
| { |
| 203 |
22 |
| int64_t got = 0; |
| 204 |
|
| int rc; |
| 205 |
|
| int32_t nsec; |
| 206 |
22 |
▶ | if (!tkra_handle_ok(h) || !from || !out) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!tkra_handle_ok(h) || !from || !out) |
21 |
1 |
22 |
| if (!tkra_handle_ok(h) || !from || !out) |
21 |
0 |
21 |
| if (!tkra_handle_ok(h) || !from || !out) |
0 |
21 |
21 |
|
| 207 |
1 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 208 |
21 |
▶ | if (h->is_set) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->is_set) { |
2 |
19 |
21 |
|
| 209 |
2 |
| const tkr_set *s = tkr_set_of(aTHX_ h->obj, "Time::Keeper::Recurrence ABI"); |
| 210 |
2 |
▶ | if (dir) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dir) |
1 |
1 |
2 |
|
| 211 |
1 |
| rc = tkr_set_previous(aTHX_ h->obj, from->sec, &got, NULL); |
| 212 |
|
| else { |
| 213 |
|
| tkr_setiter it; |
| 214 |
1 |
| rc = tkr_setiter_init(aTHX_ &it, h->obj, from->sec, 0, 0); |
| 215 |
1 |
▶ | if (rc == TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == TKR_W_GOT) |
1 |
0 |
1 |
|
| 216 |
1 |
| rc = tkr_setiter_next(aTHX_ &it, &got); |
| 217 |
1 |
| tkr_setiter_free(aTHX_ &it); |
| 218 |
|
| } |
| 219 |
2 |
| nsec = tkr_set_nsec(s); |
| 220 |
|
| } |
| 221 |
|
| else { |
| 222 |
19 |
| const tkr_rule *r = tkr_rule_of(aTHX_ h->obj, "Time::Keeper::Recurrence ABI"); |
| 223 |
19 |
▶ | if (r->flags & TKR_F_EVERY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) { |
0 |
19 |
19 |
|
| 224 |
0 |
▶ | got = dir ? from->sec - r->every_sec : from->sec + r->every_sec; |
Branch: 1 branch
| Branch | True | False | Total |
| got = dir ? from->sec - r->every_sec : from->sec + r->every_sec; |
0 |
0 |
0 |
|
| 225 |
0 |
| rc = TKR_W_GOT; |
| 226 |
|
| } |
| 227 |
|
| else |
| 228 |
19 |
▶ | rc = dir ? tkr_previous_before(aTHX_ r, from->sec, &got, NULL) : tkr_next_after(aTHX_ r, from->sec, 0, &got, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = dir ? tkr_previous_before(aTHX_ r, from->sec, &got, NULL) : tkr_next_after(aTHX_ r, from->sec, 0, &got, NULL); |
3 |
16 |
19 |
|
| 229 |
19 |
| nsec = tkr_occurrence_nsec(r); |
| 230 |
|
| } |
| 231 |
21 |
▶ | if (rc != TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != TKR_W_GOT) |
3 |
18 |
21 |
|
| 232 |
3 |
| return tkra_rc(rc); |
| 233 |
18 |
| out->sec = got; |
| 234 |
18 |
| out->nsec = nsec; |
| 235 |
18 |
| return TIME_KEEPER_RECURRENCE_OK; |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
|
| static int |
| 239 |
18 |
| tkra_next(pTHX_ const time_keeper_recurrence *h, const time_keeper_instant *from, time_keeper_instant *out) |
| 240 |
|
| { |
| 241 |
18 |
| return tkra_walk(aTHX_ h, from, out, 0); |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
|
| static int |
| 245 |
4 |
| tkra_previous(pTHX_ const time_keeper_recurrence *h, const time_keeper_instant *from, time_keeper_instant *out) |
| 246 |
|
| { |
| 247 |
4 |
| return tkra_walk(aTHX_ h, from, out, 1); |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
|
| static void |
| 251 |
8 |
| tkra_release(time_keeper_recurrence *h) |
| 252 |
|
| { |
| 253 |
|
| dTHX; |
| 254 |
8 |
▶ | if (!h || h->magic != TKR_HANDLE_MAGIC) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h || h->magic != TKR_HANDLE_MAGIC) |
6 |
2 |
8 |
| if (!h || h->magic != TKR_HANDLE_MAGIC) |
0 |
6 |
6 |
|
| 255 |
2 |
| return; |
| 256 |
6 |
| h->magic = 0; |
| 257 |
6 |
| SvREFCNT_dec(h->obj); |
| 258 |
6 |
| h->obj = NULL; |
| 259 |
6 |
| Safefree(h); |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
|
| /* An epoch as seconds and nanoseconds; 0 outside the years Time::Keeper holds. */ |
| 263 |
|
| static int |
| 264 |
18 |
| tkra_epoch_in(double e, int64_t *sec, int32_t *nsec) |
| 265 |
|
| { |
| 266 |
|
| double f; |
| 267 |
18 |
▶ | if (!(e == e) || e > 3.15e16 || e < -3.15e16) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e == e) || e > 3.15e16 || e < -3.15e16) |
18 |
0 |
18 |
| if (!(e == e) || e > 3.15e16 || e < -3.15e16) |
17 |
1 |
18 |
| if (!(e == e) || e > 3.15e16 || e < -3.15e16) |
0 |
17 |
17 |
|
| 268 |
1 |
| return 0; |
| 269 |
17 |
| f = floor(e); |
| 270 |
17 |
| *sec = (int64_t)f; |
| 271 |
17 |
| *nsec = (int32_t)((e - f) * 1e9 + 0.5); |
| 272 |
17 |
▶ | if (*nsec >= 1000000000) { *sec += 1; *nsec -= 1000000000; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*nsec >= 1000000000) { *sec += 1; *nsec -= 1000000000; } |
0 |
17 |
17 |
|
| 273 |
17 |
| return 1; |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
|
| static int |
| 277 |
13 |
| tkra_zone_in(pTHX_ const char *zone, size_t zlen, time_keeper_zone *z, char *err, size_t errlen) |
| 278 |
|
| { |
| 279 |
13 |
| z->kind = TIME_KEEPER_ZONE_UTC; |
| 280 |
13 |
| z->value = 0; |
| 281 |
13 |
▶ | if (!zone || !zlen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!zone || !zlen) |
12 |
1 |
13 |
| if (!zone || !zlen) |
1 |
11 |
12 |
|
| 282 |
2 |
| return 1; |
| 283 |
11 |
▶ | if ((tkr_tk(aTHX)->zone_parse)(aTHX_ zone, zlen, z) == TIME_KEEPER_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((tkr_tk(aTHX)->zone_parse)(aTHX_ zone, zlen, z) == TIME_KEEPER_OK) |
10 |
1 |
11 |
|
| 284 |
10 |
| return 1; |
| 285 |
1 |
▶ | tkra_err(err, errlen, "'%.*s' is not a time zone", (int)(zlen < 60 ? zlen : 60), zone); |
Branch: 1 branch
| Branch | True | False | Total |
| tkra_err(err, errlen, "'%.*s' is not a time zone", (int)(zlen < 60 ? zlen : 60), zone); |
1 |
0 |
1 |
|
| 286 |
1 |
| return 0; |
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
|
| static int |
| 290 |
10 |
| tkra_cron_next(pTHX_ const char *expr, size_t elen, const char *zone, size_t zlen, |
| 291 |
|
| double from, double *out, char *err, size_t errlen) |
| 292 |
|
| { |
| 293 |
|
| tkr_rule r; |
| 294 |
|
| char e[TKR_ERR]; |
| 295 |
|
| int64_t f, got; |
| 296 |
|
| int32_t fn; |
| 297 |
|
| int rc; |
| 298 |
10 |
▶ | if (err && errlen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && errlen) |
10 |
0 |
10 |
| if (err && errlen) |
10 |
0 |
10 |
|
| 299 |
10 |
| err[0] = '\0'; |
| 300 |
10 |
▶ | if (!expr || !out) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!expr || !out) { |
10 |
0 |
10 |
| if (!expr || !out) { |
0 |
10 |
10 |
|
| 301 |
0 |
| tkra_err(err, errlen, "cron_next: no expression or no place for the answer"); |
| 302 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 303 |
|
| } |
| 304 |
10 |
▶ | if (!tkra_epoch_in(from, &f, &fn)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkra_epoch_in(from, &f, &fn)) |
1 |
9 |
10 |
|
| 305 |
1 |
| return TIME_KEEPER_RECURRENCE_RANGE; |
| 306 |
9 |
| tkr_rule_init(&r); |
| 307 |
9 |
| r.gap = TKR_GAP_SKIP; |
| 308 |
9 |
| r.overlap = TKR_OVERLAP_BOTH; |
| 309 |
9 |
▶ | if (!tkra_zone_in(aTHX_ zone, zlen, &r.zone, err, errlen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkra_zone_in(aTHX_ zone, zlen, &r.zone, err, errlen)) |
1 |
8 |
9 |
|
| 310 |
1 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 311 |
8 |
▶ | if (!tkr_cron_parse(expr, elen, &r, e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkr_cron_parse(expr, elen, &r, e)) { |
1 |
7 |
8 |
|
| 312 |
1 |
| tkra_err(err, errlen, "%s", e); |
| 313 |
1 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 314 |
|
| } |
| 315 |
7 |
▶ | if (r.flags & TKR_F_EVERY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r.flags & TKR_F_EVERY) { |
1 |
6 |
7 |
|
| 316 |
1 |
| *out = from + (double)r.every_sec; |
| 317 |
1 |
| return TIME_KEEPER_RECURRENCE_OK; |
| 318 |
|
| } |
| 319 |
6 |
| rc = tkr_next_after(aTHX_ &r, f, 0, &got, NULL); |
| 320 |
6 |
▶ | if (rc != TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != TKR_W_GOT) |
1 |
5 |
6 |
|
| 321 |
1 |
| return tkra_rc(rc); |
| 322 |
5 |
| *out = (double)got; |
| 323 |
5 |
| return TIME_KEEPER_RECURRENCE_OK; |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| static int |
| 327 |
4 |
| tkra_rrule_next(pTHX_ const char *rrule, size_t rlen, const char *zone, size_t zlen, |
| 328 |
|
| double start, double from, double *out, char *err, size_t errlen) |
| 329 |
|
| { |
| 330 |
|
| tkr_rule r; |
| 331 |
|
| char e[TKR_ERR]; |
| 332 |
|
| int64_t f, got; |
| 333 |
|
| int32_t fn; |
| 334 |
|
| int rc; |
| 335 |
|
| time_keeper_calfields cf; |
| 336 |
4 |
▶ | if (err && errlen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && errlen) |
4 |
0 |
4 |
| if (err && errlen) |
4 |
0 |
4 |
|
| 337 |
4 |
| err[0] = '\0'; |
| 338 |
4 |
▶ | if (!rrule || !out) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rrule || !out) { |
4 |
0 |
4 |
| if (!rrule || !out) { |
0 |
4 |
4 |
|
| 339 |
0 |
| tkra_err(err, errlen, "rrule_next: no rule or no place for the answer"); |
| 340 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 341 |
|
| } |
| 342 |
4 |
| tkr_rule_init(&r); |
| 343 |
4 |
| r.gap = TKR_GAP_FORWARD; |
| 344 |
4 |
| r.overlap = TKR_OVERLAP_EARLIER; |
| 345 |
4 |
▶ | if (!tkra_epoch_in(start, &r.start.sec, &r.start.nsec) || !tkra_epoch_in(from, &f, &fn)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tkra_epoch_in(start, &r.start.sec, &r.start.nsec) || !tkra_epoch_in(from, &f, &fn)) |
4 |
0 |
4 |
| if (!tkra_epoch_in(start, &r.start.sec, &r.start.nsec) || !tkra_epoch_in(from, &f, &fn)) |
0 |
4 |
4 |
|
| 346 |
0 |
| return TIME_KEEPER_RECURRENCE_RANGE; |
| 347 |
4 |
| r.flags |= TKR_F_HAS_START; |
| 348 |
4 |
▶ | if (!tkra_zone_in(aTHX_ zone, zlen, &r.zone, err, errlen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkra_zone_in(aTHX_ zone, zlen, &r.zone, err, errlen)) |
0 |
4 |
4 |
|
| 349 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 350 |
4 |
▶ | if (!tkr_rrule_parse(rrule, rlen, &r, e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkr_rrule_parse(rrule, rlen, &r, e)) { |
0 |
4 |
4 |
|
| 351 |
0 |
| tkra_err(err, errlen, "%s", e); |
| 352 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 353 |
|
| } |
| 354 |
4 |
▶ | if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) { |
1 |
3 |
4 |
| if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) { |
1 |
0 |
1 |
|
| 355 |
1 |
| tkra_err(err, errlen, "UNTIL must be in UTC (end in Z) when the start has a zone"); |
| 356 |
1 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 357 |
|
| } |
| 358 |
3 |
▶ | if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) { |
0 |
3 |
3 |
| if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) { |
0 |
0 |
0 |
|
| 359 |
0 |
| tkra_err(err, errlen, "UNTIL must be floating (no Z) when the start is"); |
| 360 |
0 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 361 |
|
| } |
| 362 |
3 |
▶ | if ((r.flags & TKR_F_RSCALE) && (tkr_tk(aTHX)->cal_fields)(aTHX_ &r.start, &r.zone, r.calendar, &cf) != TIME_KEEPER_OK) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((r.flags & TKR_F_RSCALE) && (tkr_tk(aTHX)->cal_fields)(aTHX_ &r.start, &r.zone, r.calendar, &cf) != TIME_KEEPER_OK) |
0 |
3 |
3 |
| if ((r.flags & TKR_F_RSCALE) && (tkr_tk(aTHX)->cal_fields)(aTHX_ &r.start, &r.zone, r.calendar, &cf) != TIME_KEEPER_OK) |
0 |
0 |
0 |
|
| 363 |
0 |
| return TIME_KEEPER_RECURRENCE_RANGE; |
| 364 |
3 |
| rc = tkr_next_after(aTHX_ &r, f, 0, &got, NULL); |
| 365 |
3 |
▶ | if (rc != TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != TKR_W_GOT) |
1 |
2 |
3 |
|
| 366 |
1 |
| return tkra_rc(rc); |
| 367 |
2 |
| *out = (double)got + (double)r.start.nsec / 1e9; |
| 368 |
2 |
| return TIME_KEEPER_RECURRENCE_OK; |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
|
| static SV * |
| 372 |
3 |
| tkra_new_sv(pTHX_ const time_keeper_recurrence *h) |
| 373 |
|
| { |
| 374 |
3 |
▶ | if (!tkra_handle_ok(h)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tkra_handle_ok(h)) |
1 |
2 |
3 |
|
| 375 |
1 |
| return NULL; |
| 376 |
2 |
| return newSVsv(h->obj); |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| static int |
| 380 |
3 |
| tkra_from_sv(pTHX_ SV *sv, time_keeper_recurrence **out) |
| 381 |
|
| { |
| 382 |
3 |
▶ | if (!sv || !out || !SvROK(sv) || !sv_isobject(sv) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!sv || !out || !SvROK(sv) || !sv_isobject(sv) |
3 |
0 |
3 |
| if (!sv || !out || !SvROK(sv) || !sv_isobject(sv) |
3 |
0 |
3 |
| if (!sv || !out || !SvROK(sv) || !sv_isobject(sv) |
2 |
1 |
3 |
| if (!sv || !out || !SvROK(sv) || !sv_isobject(sv) |
2 |
0 |
2 |
|
| 383 |
2 |
▶ | || !(sv_derived_from(sv, "Time::Keeper::Recurrence") || sv_derived_from(sv, "Time::Keeper::Recurrence::Set"))) |
Branch: 2 branches
| Branch | True | False | Total |
| || !(sv_derived_from(sv, "Time::Keeper::Recurrence") || sv_derived_from(sv, "Time::Keeper::Recurrence::Set"))) |
1 |
1 |
2 |
| || !(sv_derived_from(sv, "Time::Keeper::Recurrence") || sv_derived_from(sv, "Time::Keeper::Recurrence::Set"))) |
1 |
0 |
1 |
|
| 384 |
2 |
| return TIME_KEEPER_RECURRENCE_BAD; |
| 385 |
1 |
| *out = tkra_handle_new(aTHX_ sv); |
| 386 |
1 |
| return TIME_KEEPER_RECURRENCE_OK; |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| static const time_keeper_recurrence_abi TKR_ABI = { |
| 390 |
|
| TIME_KEEPER_RECURRENCE_ABI_VERSION, |
| 391 |
|
| tkra_compile, |
| 392 |
|
| tkra_next, |
| 393 |
|
| tkra_previous, |
| 394 |
|
| tkra_release, |
| 395 |
|
| tkra_cron_next, |
| 396 |
|
| tkra_rrule_next, |
| 397 |
|
| tkra_new_sv, |
| 398 |
|
| tkra_from_sv, |
| 399 |
|
| }; |
| 400 |
|
| |
| 401 |
|
| #endif |