| 1 |
|
| #ifndef TKR_WALK_SV_H |
| 2 |
|
| #define TKR_WALK_SV_H |
| 3 |
|
| |
| 4 |
|
| /* The walk's ends in Perl: instants in and values out, the words for a |
| 5 |
|
| * rule that can never fire or a skipped wall time it will not have, and |
| 6 |
|
| * the iterator's state on its code ref. */ |
| 7 |
|
| |
| 8 |
|
| static int64_t |
| 9 |
50463 |
| tkr_instant_arg(pTHX_ SV *sv, const char *what) |
| 10 |
|
| { |
| 11 |
|
| time_keeper_instant in; |
| 12 |
50463 |
▶ | if (!sv || !SvOK(sv) || (tkr_tk(aTHX)->from_sv)(aTHX_ sv, &in, NULL, NULL) != TIME_KEEPER_OK) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv) || (tkr_tk(aTHX)->from_sv)(aTHX_ sv, &in, NULL, NULL) != TIME_KEEPER_OK) |
50463 |
0 |
50463 |
| if (!sv || !SvOK(sv) || (tkr_tk(aTHX)->from_sv)(aTHX_ sv, &in, NULL, NULL) != TIME_KEEPER_OK) |
50463 |
0 |
50463 |
| if (!sv || !SvOK(sv) || (tkr_tk(aTHX)->from_sv)(aTHX_ sv, &in, NULL, NULL) != TIME_KEEPER_OK) |
1 |
50462 |
50463 |
|
| 13 |
1 |
| croak("%s: a Time::Keeper value is wanted", what); |
| 14 |
50462 |
| return in.sec; |
| 15 |
|
| } |
| 16 |
|
| |
| 17 |
|
| static int64_t |
| 18 |
428 |
| tkr_now_sec(pTHX) |
| 19 |
|
| { |
| 20 |
|
| time_keeper_instant t; |
| 21 |
428 |
| (tkr_tk(aTHX)->now)(&t); |
| 22 |
428 |
| return t.sec; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
|
| /* An occurrence carries the start's fraction of a second; a cron's is 0. */ |
| 26 |
|
| static int32_t |
| 27 |
47626 |
| tkr_occurrence_nsec(const tkr_rule *r) |
| 28 |
|
| { |
| 29 |
47626 |
▶ | return (r->flags & TKR_F_HAS_START) && !(r->flags & TKR_F_CRON) ? r->start.nsec : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (r->flags & TKR_F_HAS_START) && !(r->flags & TKR_F_CRON) ? r->start.nsec : 0; |
44362 |
3264 |
47626 |
| return (r->flags & TKR_F_HAS_START) && !(r->flags & TKR_F_CRON) ? r->start.nsec : 0; |
44356 |
6 |
44362 |
|
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| static SV * |
| 33 |
45602 |
| tkr_occurrence_sv(pTHX_ const tkr_rule *r, int64_t sec, const char *what) |
| 34 |
|
| { |
| 35 |
|
| time_keeper_instant t; |
| 36 |
|
| SV *sv; |
| 37 |
45602 |
| t.sec = sec; |
| 38 |
45602 |
| t.nsec = tkr_occurrence_nsec(r); |
| 39 |
4671 |
| sv = (r->flags & TKR_F_RSCALE) ? (tkr_tk(aTHX)->new_sv_cal)(aTHX_ &t, &r->zone, r->locale, r->calendar) |
| 40 |
45602 |
▶ | : (tkr_tk(aTHX)->new_sv)(aTHX_ &t, &r->zone, r->locale); |
Branch: 1 branch
| Branch | True | False | Total |
| : (tkr_tk(aTHX)->new_sv)(aTHX_ &t, &r->zone, r->locale); |
4671 |
40931 |
45602 |
|
| 41 |
45602 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
0 |
45602 |
45602 |
|
| 42 |
0 |
| croak("%s: an occurrence outside the years Time::Keeper holds", what); |
| 43 |
45602 |
| return sv; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| static void |
| 47 |
52169 |
| tkr_rc_croak(pTHX_ int rc, const tkr_rule *r, int64_t wall, const char *what) |
| 48 |
|
| { |
| 49 |
52169 |
▶ | if (rc == TKR_W_NEVER) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == TKR_W_NEVER) |
21 |
52148 |
52169 |
|
| 50 |
21 |
▶ | croak("%s: the rule can never fire (no occurrence within %s)", what, |
Branch: 1 branch
| Branch | True | False | Total |
| croak("%s: the rule can never fire (no occurrence within %s)", what, |
0 |
21 |
21 |
|
| 51 |
|
| (r->flags & TKR_F_CRON) ? "five years" : "fifty years"); |
| 52 |
52148 |
▶ | if (rc == TKR_W_FAIL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == TKR_W_FAIL) { |
1 |
52147 |
52148 |
|
| 53 |
1 |
| int64_t days = tkr_floor_div(wall, 86400), rem = wall - days * 86400, y; |
| 54 |
|
| unsigned m, d; |
| 55 |
1 |
| SV *zone = sv_2mortal(tkr_zone_name_sv(aTHX_ r)); |
| 56 |
1 |
| tkr_civil_from_days(days, &y, &m, &d); |
| 57 |
1 |
| croak("%s: the clocks of %" SVf " skipped %04" IVdf "-%02u-%02u %02d:%02d:%02d, and the rule says fail", what, |
| 58 |
|
| SVfARG(zone), (IV)y, m, d, (int)(rem / 3600), (int)(rem % 3600 / 60), (int)(rem % 60)); |
| 59 |
|
| } |
| 60 |
52147 |
| } |
| 61 |
|
| |
| 62 |
|
| /* An iterator: a walk, and the moment its occurrences must follow. */ |
| 63 |
|
| typedef struct tkr_iter { |
| 64 |
|
| tkr_walk w; |
| 65 |
|
| int64_t from; |
| 66 |
|
| int inclusive; |
| 67 |
|
| } tkr_iter; |
| 68 |
|
| |
| 69 |
|
| static int |
| 70 |
335 |
| tkr_iter_free(pTHX_ SV *sv, MAGIC *mg) |
| 71 |
|
| { |
| 72 |
335 |
| tkr_iter *it = (tkr_iter *)SvPVX(mg->mg_obj); |
| 73 |
|
| PERL_UNUSED_VAR(sv); |
| 74 |
335 |
| tkr_walk_free(&it->w); |
| 75 |
335 |
| return 0; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| static MGVTBL tkr_iter_vtbl = { 0, 0, 0, 0, tkr_iter_free, 0, 0, 0 }; |
| 79 |
|
| |
| 80 |
|
| static tkr_iter * |
| 81 |
2002 |
| tkr_iter_of(pTHX_ CV *cv, const char *what) |
| 82 |
|
| { |
| 83 |
|
| MAGIC *mg; |
| 84 |
2002 |
▶ | for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) |
2002 |
0 |
2002 |
|
| 85 |
2002 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_iter_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_iter_vtbl) |
2002 |
0 |
2002 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_iter_vtbl) |
2002 |
0 |
2002 |
|
| 86 |
2002 |
| return (tkr_iter *)SvPVX(mg->mg_obj); |
| 87 |
0 |
| croak("%s: not an iterator", what); |
| 88 |
|
| return NULL; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| #endif |