Toolkit.pm

filter: Statementstmt 87.1% 446 / 512 Branchbran 70.7% 235 / 332 Conditioncond 60.0% 99 / 165 MC/DCmcdc 31.5% 29 / 92 Subroutinesub 88.0% 44 / 50 Podpod 60.0% 12 / 20 totaltotal 73.8% 865 / 1171 CC 238 SCAR 39.0
CC
238
Cov
77%
_build_node
165.4
new
34.4
_set_date
31.6
CRAP 49.3
↑ Summary Toolkit/Pattern/NearestWeekday.pm »
1 package Cron::Toolkit;
2  
3 # VERSION
4 2 $VERSION = 1.04;
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Cron::Toolkit::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Cron::Toolkit::Pattern::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cron::Toolkit::Pattern::Wildcard;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Cron::Toolkit::Pattern::Range;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cron::Toolkit::Pattern::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Cron::Toolkit::Pattern::Last;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Cron::Toolkit::Pattern::LastW;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Cron::Toolkit::Pattern::Nth;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Cron::Toolkit::Pattern::Unspecified;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Cron::Toolkit::Pattern::NearestWeekday;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Cron::Toolkit::Pattern::StepValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Cron::Toolkit::Pattern::Step;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =encoding utf-8
28  
29 =head1 NAME
30  
31 Cron::Toolkit - Quartz-compatible cron parser with unique extensions and over 400 tests
32  
33 =head1 SYNOPSIS
34  
35 use Cron::Toolkit;
36 use feature qw(say);
37  
38 my $c = Cron::Toolkit->new(
39 expression => "0 30 14 ? * 6-2 *",
40 time_zone => "Europe/London",
41 );
42  
43 say $c->describe;
44 # 2:30 PM every day from Saturday to Tuesday of every month
45  
46 # next occurence in epoch seconds
47 say $c->next;
48  
49 # previous occurence in epoch seconds
50 say $c->previous;
51  
52 # Question: when does February 29th next land on a Monday?
53 say Cron::Toolkit->new(expression => "0 0 0 29 2 1 *")->next;
54 # Mon Feb 29 00:00:00 2044
55  
56 # See exactly what was parsed
57 $c->dump_tree;
58 # ┌─ second: 0
59 # ├─ minute: 30
60 # ├─ hour: 14
61 # ├─ dom: ?
62 # ├─ month: *
63 # ├─ dow: 6-2
64 # └─ year: *
65  
66 =head1 DESCRIPTION
67  
68 C<Cron::Toolkit> implements a complete, rigorously-tested cron expression parser that supports the full Quartz Scheduler syntax plus several useful extensions not found in other implementations.
69  
70 Notable features include:
71  
72 =over 4
73  
74 =item * Full 7-field Quartz syntax (seconds and year fields)
75  
76 =item * Both day-of-month and day-of-week may be specified simultaneously (AND logic)
77  
78 =item * Wrapped day-of-week ranges (e.g. C<6-2> = Saturday through Tuesday)
79  
80 =item * Proper Quartz-compatible DST handling
81  
82 =item * Time-zone support via IANA names or fixed UTC offsets
83  
84 =item * Natural-language English descriptions
85  
86 =item * Complete crontab parsing with environment variable expansion
87  
88 =item * Full abstract syntax tree and C<dump_tree()> for debugging
89  
90 =back
91  
92 =head1 RELIABILITY
93  
94 The distribution ships with over 400 data-driven tests covering every supported token, leap years, DST transitions, all time zones from UTC−12 to UTC+14, and every edge case discovered during development.
95  
96 If it parses, the result is correct.
97  
98 =head1 UNIQUE EXTENSIONS
99  
100 =over 4
101  
102 =item * DOM + DOW = AND logic
103  
104 Allows queries such as "next February 29 that falls on a Monday".
105  
106 =item * Wrapped day-of-week ranges
107  
108 6-2 matches Saturday, Sunday, Monday, Tuesday
109  
110 =item * Internal day-of-week: 1–7 = Monday–Sunday
111  
112 Matches L<Time::Moment> and L<DateTime>. C<as_quartz_string()> converts back to Quartz's 1=Sunday convention.
113  
114 =back
115  
116 =head1 FIELD REFERENCE & ALLOWED VALUES
117  
118 Field Allowed values Allowed special characters
119 -------------------------------------------------------------------
120 Second 0–59 *,/,-
121 Minute 0–59 *,/,-,
122 Hour 0–23 *,/,-,
123 Day of month 1–31 *,/,-,?,L,LW,W
124 Month 1–12 or JAN–DEC *,/,-
125 Day of week 1–7 or MON-SUN *,/,-,?,L,#
126 Year (optional) 1970–2099 *,/,-
127  
128 Legend:
129 * wildcard
130 , list
131 - range
132 / step
133 ? no specific value (DOM or DOW only)
134 L last (day or day-of-week)
135 L-n n to last day of the month
136 nL last n-day of the month
137 LW last weekday of month
138 nW nearest weekday to n
139 # nth day-of-week (e.g. 3#2 = 2nd Wednesday)
140  
141 @aliases: @yearly @annually @monthly @weekly @daily @hourly (Quartz standard)
142  
143 =head1 METHODS
144  
145 =over 4
146  
147 =item C<< Cron::Toolkit->new( expression => $expr, %options ) >>
148  
149 Main constructor; auto-detects Unix vs Quartz format.
150  
151 =item C<< Cron::Toolkit->new_from_unix( expression => $expr, %options ) >>
152  
153 Force traditional 5-field Unix interpretation.
154  
155 =item C<< Cron::Toolkit->new_from_quartz( expression => $expr, %options ) >>
156  
157 Force Quartz interpretation.
158  
159 =item C<< Cron::Toolkit->new_from_crontab( $string ) >>
160  
161 Parse a full crontab; returns a list of C<Cron::Toolkit> objects.
162 Supports C<$VAR> expansion, user field, and comments.
163  
164 =item C<< $c->as_string >>
165  
166 Normalized 7-field representation (DOW 1–7 = Mon–Sun).
167  
168 =item C<< $c->as_quartz_string >>
169  
170 Quartz-compatible string (DOW 1=Sunday).
171  
172 =item C<< $c->describe >>
173  
174 Human-readable English description.
175  
176 =item C<< $c->next( [$from_epoch] ) >>
177  
178 Next occurrence after C<$from_epoch> or C<time>.
179  
180 =item C<< $c->previous( [$from_epoch] ) >>
181  
182 Previous occurrence before C<$from_epoch> or C<time>.
183  
184 =item C<< $c->is_match( $epoch ) >>
185  
186 Returns true if C<$epoch> matches the expression.
187  
188 =item C<< $c->dump_tree >>
189  
190 Pretty-printed abstract syntax tree (invaluable for debugging).
191  
192 =item C<< $c->to_json >>
193  
194 JSON representation of the object (expression, description, bounds, etc.).
195  
196 =item Accessors
197  
198 $c->time_zone("Europe/Berlin")
199 $c->utc_offset(+180) # minutes
200 $c->begin_epoch($epoch)
201 $c->end_epoch($epoch) # undef = no limit
202  
203 =back
204  
205 =head1 TIME ZONES AND DST
206  
207 All calculations are performed in the configured time zone.
208 DST transitions follow Quartz Scheduler rules exactly:
209  
210 =over 4
211  
212 =item * Spring forward — times that do not exist are skipped
213  
214 =item * Fall back — repeated local times fire twice
215  
216 =back
217  
218 =head1 BUGS AND CONTRIBUTIONS
219  
220 The test suite currently contains over 400 data-driven tests covering every supported token, DST transitions, leap years, all time zones, and many edge cases — but real-world cron expressions can be surprisingly creative.
221  
222 If you find:
223  
224 =over 4
225  
226 =item * an expression that should be valid but dies or is rejected
227  
228 =item * a next/previous occurrence that is wrong
229  
230 =item * a description that is misleading or unclear
231  
232 =item * any behaviour that differs from Quartz Scheduler (when using Quartz syntax)
233  
234 =back
235  
236 ...please file a bug report at
237 L<https://github.com/nathanielgraham/cron-toolkit-perl/issues>
238  
239 Pull requests with failing test cases are especially welcome — they are the fastest way to get a fix merged.
240  
241 Feature requests (e.g. more natural-language locales, RRULE export, etc.) are also very much appreciated.
242  
243 Thank you!
244  
245 =cut
246  
247 =head1 AUTHOR
248  
249 Nathaniel Graham
250  
251 =head1 COPYRIGHT AND LICENSE
252  
253 Copyright 2025 Nathaniel Graham
254  
255 This library is free software; you may redistribute it and/or modify it
256 under the same terms as Perl itself.
257  
258 =cut
259  
260 0 ▶sub new_from_unix {
Subroutine new_from_unix: not called CC=1 SCAR=6.9
Pod documented
261 0 my ( $class, %args ) = @_;
262 0 $args{is_quartz} = 0;
263 0 my $self = $class->new(%args);
264 0 return $self;
265 }
266  
267 0 ▶sub new_from_quartz {
Subroutine new_from_quartz: not called CC=1 SCAR=6.9
Pod documented
268 0 my ( $class, %args ) = @_;
269 0 $args{is_quartz} = 1;
270 0 my $self = $class->new(%args);
271 0 return $self;
272 }
273  
274 99 ▶sub new {
Subroutine new: called CC=25 SCAR=35.4
Pod documented
275 99 my ( $class, %args ) = @_;
276 99 ▶ die "expression required" unless defined $args{expression};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"expression"} 0 99 99
277 99 my $expr = uc $args{expression};
278 99 $expr =~ s/\s+/ /g;
279 99 $expr =~ s/^\s+|\s+$//g;
280  
281 # alias support
282 99 ▶ if ( $expr =~ /^(@.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(\@.*)/) 6 93 99
283 6 my $alias = lc($1);
284 6 ▶ $expr = $ALIASES{$alias} or die "no such alias: $alias";
Branch: 1 branch
BranchTrueFalseTotal
$expr = $Cron::Toolkit::ALIASES{$alias} or die "no such alias: $alias" 6 0 6
285 }
286  
287 99 my @fields = split /\s+/, $expr;
288 99 my @raw_fields = @fields;
289  
290 # normalize to 7-fields
291 99 ▶ unshift( @fields, 0 ) if scalar @fields == 5; # seconds
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields == 5 4 95 99
292 99 ▶ push( @fields, '*' ) if scalar @fields == 6; # year
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields == 6 4 95 99
293 99 ▶ die "expected 5-7 fields" unless scalar @fields == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @fields == 7 0 99 99
294  
295 # normalize to 7-field quartz expression
296 99 ▶ if ( $args{is_quartz} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'is_quartz'}) { } 0 99 99
297  
298 # Reject Quartz DOW 0
299 0 ▶ if ( $fields[5] =~ /\b0\b/ && $fields[5] !~ /#\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[5] =~ /\b0\b/ and not $fields[5] =~ /#\d+/) 0 0 0
Condition0%0/3
$fields[5] =~ /\b0\b/ and not $fields[5] =~ /#\d+/
!ll&&!rl&&r
000
MC/DC0%0/2
$fields[5] =~ /\b0\b/ and not $fields[5] =~ /#\d+/
$fields[5] =~ /\b0\b/ not $fields[5] =~ /#\d+/
Truth table0%0/3
A and not(B)
A= $fields[5] =~ /\b0\b/B= not $fields[5] =~ /#\d+/
ABresult
0X0
100
111
300 0 die "Invalid dow value: 0, must be [1-7] in Quartz";
301 }
302  
303 # Map Quartz DOW names. Allow trailing L/# and cron punctuation so
304 # MONL → nL and MON#3 → n#3 without matching inside MONDAY.
305 0 ▶ while ( my ( $name, $num ) = each %DOW_MAP_QUARTZ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Cron::Toolkit::DOW_MAP_QUARTZ) 0 0 0
306 0 $fields[5] =~ s/\b\Q$name\E(?=L\b|[#,\-\/?\s]|$)/$num/gi;
307 }
308  
309 # Normalize Quartz DOW 1-7 to 0-6, skip nth and step
310 0 $fields[5] =~ s/(?<![#\/])(\b[1-7]\b)(?![#\/])/$1-1/ge;
311 }
312 else {
313 # convert dow names to unix numerical equivalent. Allow trailing L/#
314 # and cron punctuation so MONL → 1L and MON#3 → 1#3; MON-FRI still works.
315 99 ▶ while ( my ( $name, $num ) = each %DOW_MAP_UNIX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Cron::Toolkit::DOW_MAP_UNIX) 1386 99 1485
316 1386 $fields[5] =~ s/\b\Q$name\E(?=L\b|[#,\-\/?\s]|$)/$num/gi;
317 }
318 }
319  
320 # Convert month names to numerical equivalent
321 99 ▶ while ( my ( $name, $num ) = each %MONTH_MAP ) { $fields[4] =~ s/\b\Q$name\E\b/$num/gi; }
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Cron::Toolkit::MONTH_MAP) 2277 99 2376
322  
323 # align dom and dow fields
324 99 ▶ if ( $fields[3] ne '?' && $fields[5] eq '*' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fields[3] ne '?' and $fields[5] eq '*') { } 31 68 99
elsif ($fields[3] eq '*' and $fields[5] ne '?') { } 2 66 68
elsif ($fields[3] eq '?' and $fields[5] eq '?') { } 0 98 98
Condition100%3/3
$fields[3] ne "?" and $fields[5] eq "*"
!ll&&!rl&&r
343431
MC/DC100%2/2
$fields[3] ne "?" and $fields[5] eq "*"
$fields[3] ne "?" $fields[5] eq "*"
Truth table100%3/3
A and B
A= $fields[3] ne "?"B= $fields[5] eq "*"
ABresult
0X0
100
111
325 31 $fields[5] = '?';
326 }
327 ▶ elsif ( $fields[3] eq '*' && $fields[5] ne '?' ) {
Condition100%3/3
$fields[3] eq "*" and $fields[5] ne "?"
!ll&&!rl&&r
6332
MC/DC100%2/2
$fields[3] eq "*" and $fields[5] ne "?"
$fields[3] eq "*" $fields[5] ne "?"
Truth table100%3/3
A and B
A= $fields[3] eq "*"B= $fields[5] ne "?"
ABresult
0X0
100
111
328 2 $fields[3] = '?';
329 }
330 ▶ elsif ( $fields[3] eq '?' && $fields[5] eq '?' ) {
Condition66%2/3
$fields[3] eq "?" and $fields[5] eq "?"
!ll&&!rl&&r
32340
MC/DC0%0/2
$fields[3] eq "?" and $fields[5] eq "?"
$fields[3] eq "?" $fields[5] eq "?"
Truth table66%2/3
A and B
A= $fields[3] eq "?"B= $fields[5] eq "?"
ABresult
0X0
100
111
331 0 die "dow and dom cannot both be unspecified\n";
332 }
333  
334 99 ▶ die "Invalid characters" unless join( ' ', @fields ) =~ /^[#LW\d\?\*\s\-\/,]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless join(" ", @fields) =~ m[^[#LW\d\?\*\s\-/,]+$] 1 98 99
335  
336 98 my $self = bless {
337 fields => \@fields,
338 raw_fields => \@raw_fields,
339 nodes => [],
340 utc_offset => 0,
341 time_zone => 'UTC',
342 begin_epoch => time - ( 10 * 365 * 86400 ), # ~10 years ago
343 end_epoch => time + ( 10 * 365 * 86400 ), # ~10 years ahead
344 }, $class;
345  
346 98 ▶ $self->utc_offset( $args{utc_offset} ) if defined $args{utc_offset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"utc_offset"} 0 98 98
347 98 ▶ $self->time_zone( $args{time_zone} ) if defined $args{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"time_zone"} 0 98 98
348 98 ▶ $self->user( $args{user} ) if defined $args{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"user"} 2 96 98
349 98 ▶ $self->command( $args{command} ) if defined $args{command};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"command"} 6 92 98
350 98 ▶ $self->env( $args{env} ) if defined $args{env};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"env"} 6 92 98
351  
352 98 $self->_build_tree;
353  
354 98 return $self;
355 }
356  
357 98 ▶sub _build_tree {
Subroutine _build_tree: called CC=2 SCAR=6.9
358 98 my $self = shift;
359 98 my @types = qw(second minute hour dom month dow year);
360 98 for my $i ( 0 .. $#types ) {
361 686 my $node = $self->_build_node( $types[$i], $self->{fields}[$i] );
362 686 $node = $self->_optimize_node( $node, $types[$i] );
363 686 push( @{ $self->{nodes} }, $node );
364 }
365 98 $self->_finalize_dow( $self->{nodes}[5] );
366 }
367  
368 697 ▶sub _optimize_node {
Subroutine _optimize_node: called CC=15 SCAR=28.8
369 697 my ( $self, $node, $field ) = @_;
370  
371 # Get field limits
372 697 my ( $min, $max ) = @{ $LIMITS{$field} };
373 697 ▶ $min = 0 if $field eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "dow" 99 598 697
374  
375 # Step collapse — only if degenerate
376 697 ▶ if ( $node->type eq 'step' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->type eq "step") 11 686 697
377 11 my $base_node = $node->{children}[0];
378 11 my $step = $node->{children}[1]{value};
379 11 my @values;
380  
381 11 ▶ if ( $base_node->type eq 'wildcard' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base_node->type eq 'wildcard') { } 8 3 11
elsif ($base_node->type eq 'single') { } 1 2 3
elsif ($base_node->type eq 'range') { } 2 0 2
382 8 my ( $min, $max ) = @{ $LIMITS{$field} };
383 8 ▶ $min = 0 if $field eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "dow" 1 7 8
384 8 @values = ( $min .. $max );
385 }
386  
387 elsif ( $base_node->type eq 'single' ) {
388 1 my $start = $base_node->{value};
389 1 @values = ( $start .. $max );
390 }
391 elsif ( $base_node->type eq 'range' ) {
392 2 my ( $start, $end ) = map { $_->{value} } @{ $base_node->{children} };
393 2 @values = ( $start .. $end );
394 }
395  
396 11 my @stepped;
397 11 ▶ for ( my $v = $values[0] ; $v <= $values[-1] ; $v += $step ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v <= $values[-1]) 60 11 71
398 60 ▶ push @stepped, $v if grep { $_ == $v } @values;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $v;} @values 60 0 60
399 }
400  
401 # === DEGENERATE CASE: 0 or 1 value → collapse ===
402 11 ▶ if ( @stepped == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@stepped == 0) { } 0 11 11
elsif (@stepped == 1) { } 0 11 11
403 0 return Cron::Toolkit::Pattern::Wildcard->new(
404 value => '*',
405 field_type => $field
406 );
407 }
408 elsif ( @stepped == 1 ) {
409 0 return Cron::Toolkit::Pattern::Single->new(
410 value => $stepped[0],
411 field_type => $field
412 );
413 }
414  
415 # === NON-DEGENERATE: keep as step (but optimize base if possible) ===
416 # Recursively optimize base (e.g., 1-10/5 → range(1,10))
417 11 my $optimized_base = $self->_optimize_node( $base_node, $field );
418 11 ▶ return $node if $optimized_base == $base_node; # no change
Branch: 1 branch
BranchTrueFalseTotal
if $optimized_base == $base_node 11 0 11
419  
420 0 my $new_step = Cron::Toolkit::Pattern::Step->new( field_type => $field );
421 0 $new_step->add_child($optimized_base);
422 0 $new_step->add_child( $node->{children}[1] ); # step value
423 0 return $new_step;
424 }
425  
426 # List-to-range
427 686 ▶ if ( $node->type eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->type eq "list") 9 677 686
428 23 my @values = sort { $a <=> $b } map { $_->{value} }
429 9 grep { $_->type eq 'single' } @{ $node->{children} };
430 9 ▶ if ( @values >= 2 && $values[-1] - $values[0] == $#values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values >= 2 and $values[-1] - $values[0] == $#values) 0 9 9
Condition66%2/3
@values >= 2 and $values[-1] - $values[0] == $#values
!ll&&!rl&&r
180
MC/DC0%0/2
@values >= 2 and $values[-1] - $values[0] == $#values
@values >= 2 $values[-1] - $values[0] == $#values
Truth table66%2/3
A and B
A= @values >= 2B= $values[-1] - $values[0] == $#values
ABresult
0X0
100
111
431 0 my $range = Cron::Toolkit::Pattern::Range->new( field_type => $field );
432 0 $range->add_child(
433 Cron::Toolkit::Pattern::Single->new(
434 value => $values[0],
435 field_type => $field
436 )
437 );
438 0 $range->add_child(
439 Cron::Toolkit::Pattern::Single->new(
440 value => $values[-1],
441 field_type => $field
442 )
443 );
444 0 return $range;
445 }
446 }
447  
448 686 return $node;
449 }
450  
451 127 ▶sub _finalize_dow {
Subroutine _finalize_dow: called CC=5 SCAR=16.1
452 127 my $self = shift;
453 127 my $dow_node = shift;
454  
455 127 ▶ if ( $dow_node->has_children ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dow_node->has_children) { } 14 113 127
elsif ($dow_node->type eq 'single' and $dow_node->{'value'} == 0) { } 1 197 198
456 14 $self->_finalize_dow($_) for @{ $dow_node->{children} };
457 }
458  
459 ▶ elsif ( $dow_node->type eq 'single' && $dow_node->{value} == 0 ) {
Condition100%3/3
$dow_node->type eq "single" and $dow_node->{"value"} == 0
!ll&&!rl&&r
85271
MC/DC100%2/2
$dow_node->type eq "single" and $dow_node->{"value"} == 0
$dow_node->type eq "single" $dow_node->{"value"} == 0
Truth table100%3/3
A and B
A= $dow_node->type eq "single"B= $dow_node->{"value"} == 0
ABresult
0X0
100
111
460 1 $dow_node->{value} = 7;
461 }
462 }
463  
464 713 ▶sub _build_node {
Subroutine _build_node: called CC=64 SCAR=51.1
465 713 my ( $self, $field, $value ) = @_;
466  
467 713 ▶ die "Invalid characters in $field: $value" unless $value =~ $ALLOWED_CHARS{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$Cron::Toolkit::ALLOWED_CHARS{$field}/ 0 713 713
468  
469 713 my ( $min, $max ) = @{ $LIMITS{$field} };
470 713 ▶ $min = 0 if $field eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "dow" 103 610 713
471  
472 713 my $node;
473  
474 # validation and node creation
475 713 ▶ if ( $value eq '*' ) {
Branch: 12 branches
BranchTrueFalseTotal
if ($value eq '*') { } 198 515 713
elsif ($value eq '?') { } 98 417 515
elsif ($value =~ /^(\d+)?L$/) { } 15 402 417
elsif ($value =~ qr/^L-(\d+)$/) { } 1 401 402
elsif ($value =~ /^LW$/) { } 1 400 401
elsif ($value =~ /^(\d+)W$/) { } 3 397 400
elsif ($value =~ /^(\d+)#(\d+)$/) { } 10 387 397
elsif ($value =~ /^\d+$/) { } 348 39 387
elsif ($value =~ /^(\*|\d+)-(\d+)$/) { } 19 20 39
elsif ($value =~ m[^(\*|\d+)/(\d+)$]) { } 9 11 20
elsif ($value =~ m[^(\*|\d+)-(\d+)/(\d+)$]) { } 2 9 11
elsif ($value =~ /,/) { } 9 0 9
476 198 $node = Cron::Toolkit::Pattern::Wildcard->new(
477 value => '*',
478 field_type => $field
479 );
480 }
481 elsif ( $value eq '?' ) {
482 98 ▶ die "Syntax: ? only allowed in dom or dow, not $field"
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^(dom|dow)$/ 0 98 98
483 unless $field =~ /^(dom|dow)$/;
484 98 $node = Cron::Toolkit::Pattern::Unspecified->new(
485 value => '?',
486 field_type => $field
487 );
488 }
489 elsif ( $value =~ /^(\d+)?L$/ ) {
490 15 my ($day) = ($1);
491 15 ▶ die "Syntax: L only allowed in dow or dom, not $field"
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^dom|dow$/ 0 15 15
492 unless $field =~ /^dom|dow$/;
493  
494 15 $node = Cron::Toolkit::Pattern::Last->new(
495 value => $value,
496 offset => 0,
497 field_type => $field,
498 );
499  
500 15 ▶ if ( $field eq 'dom' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'dom') { } 5 10 15
501 5 ▶ die $day . "L not allowed in dom" if defined $day;
Branch: 1 branch
BranchTrueFalseTotal
if defined $day 0 5 5
502 }
503 else {
504 10 ▶ $day //= $max;
Condition66%2/3
$day //= $max
l!l&&r!l&&!r
910
MC/DC0%0/2
$day //= $max
$day $max
Truth table66%2/3
A //= B
A= $dayB= $max
ABresult
1X1
011
000
505 10 ▶ die "dow $day out of range [$min-$max]" unless $day >= $min && $day <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= $min and $day <= $max 0 10 10
Condition33%1/3
$day >= $min and $day <= $max
!ll&&!rl&&r
0010
MC/DC0%0/2
$day >= $min and $day <= $max
$day >= $min $day <= $max
Truth table33%1/3
A and B
A= $day >= $minB= $day <= $max
ABresult
0X0
100
111
506 10 $node->{dow} = $day;
507 10 $node->{value} = $day . 'L';
508 }
509 }
510 elsif ( $value =~ qr/^L-(\d+)$/ ) {
511 1 my $offset = $1;
512 1 ▶ die "Syntax: L only allowed in dom, not $field" unless $field eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
unless $field eq "dom" 0 1 1
513  
514 1 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 1 0 1
515 1 ▶ die "dom offset $offset too large" if $offset >= $max - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= $max - 1 0 1 1
516 }
517  
518 1 $node = Cron::Toolkit::Pattern::Last->new(
519 value => $value,
520 offset => $offset,
521 field_type => $field
522 );
523 }
524 elsif ( $value =~ /^LW$/ ) {
525 1 ▶ die "Syntax: LW only allowed in dom, not $field" unless $field eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
unless $field eq "dom" 0 1 1
526 1 $node = Cron::Toolkit::Pattern::LastW->new(
527 value => 'LW',
528 field_type => $field
529 );
530 }
531 elsif ( $value =~ /^(\d+)W$/ ) {
532 3 ▶ die "Syntax: W only allowed in dom, not $field" unless $field eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
unless $field eq "dom" 0 3 3
533 3 my ($day) = ($1);
534 3 ▶ die "dom $day out of range [1-31]" unless $day >= 1 && $day <= 31;
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= 1 and $day <= 31 0 3 3
Condition33%1/3
$day >= 1 and $day <= 31
!ll&&!rl&&r
003
MC/DC0%0/2
$day >= 1 and $day <= 31
$day >= 1 $day <= 31
Truth table33%1/3
A and B
A= $day >= 1B= $day <= 31
ABresult
0X0
100
111
535 3 $node = Cron::Toolkit::Pattern::NearestWeekday->new(
536 value => $value,
537 dom => $day,
538 field_type => $field
539 );
540 }
541 elsif ( $value =~ /^(\d+)#(\d+)$/ ) {
542 10 ▶ die "Syntax: # only allowed in dow, not $field" unless $field eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
unless $field eq "dow" 0 10 10
543 10 my ( $day, $nth ) = ( $1, $2 );
544 10 ▶ die "dow $day out of range [1-7]" unless $day >= 1 && $day <= 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= 1 and $day <= 7 0 10 10
Condition33%1/3
$day >= 1 and $day <= 7
!ll&&!rl&&r
0010
MC/DC0%0/2
$day >= 1 and $day <= 7
$day >= 1 $day <= 7
Truth table33%1/3
A and B
A= $day >= 1B= $day <= 7
ABresult
0X0
100
111
545 10 ▶ die "nth $nth out of range [1-5]" unless $nth >= 1 && $nth <= 5;
Branch: 1 branch
BranchTrueFalseTotal
unless $nth >= 1 and $nth <= 5 0 10 10
Condition33%1/3
$nth >= 1 and $nth <= 5
!ll&&!rl&&r
0010
MC/DC0%0/2
$nth >= 1 and $nth <= 5
$nth >= 1 $nth <= 5
Truth table33%1/3
A and B
A= $nth >= 1B= $nth <= 5
ABresult
0X0
100
111
546 10 $node = Cron::Toolkit::Pattern::Nth->new(
547 value => $value,
548 nth => $nth,
549 dow => $day,
550 field_type => $field
551 );
552 }
553 elsif ( $value =~ /^\d+$/ ) {
554 348 ▶ die "$field $value out of range [$min-$max]" unless $value >= $min && $value <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $value >= $min and $value <= $max 0 348 348
Condition33%1/3
$value >= $min and $value <= $max
!ll&&!rl&&r
00348
MC/DC0%0/2
$value >= $min and $value <= $max
$value >= $min $value <= $max
Truth table33%1/3
A and B
A= $value >= $minB= $value <= $max
ABresult
0X0
100
111
555 348 $node = Cron::Toolkit::Pattern::Single->new(
556 value => $value,
557 field_type => $field
558 );
559 }
560 elsif ( $value =~ /^(\*|\d+)-(\d+)$/ ) {
561 19 my ( $start, $end ) = ( $1, $2 );
562 19 ▶ $start = $min if $start eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $start eq "*" 0 19 19
563 19 ▶ die "$field start $start out of range [$min-$max]" unless $start >= $min && $start <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $start >= $min and $start <= $max 0 19 19
Condition33%1/3
$start >= $min and $start <= $max
!ll&&!rl&&r
0019
MC/DC0%0/2
$start >= $min and $start <= $max
$start >= $min $start <= $max
Truth table33%1/3
A and B
A= $start >= $minB= $start <= $max
ABresult
0X0
100
111
564 19 ▶ die "$field end $end out of range [$min-$max]" unless $end >= $min && $end <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $end >= $min and $end <= $max 0 19 19
Condition33%1/3
$end >= $min and $end <= $max
!ll&&!rl&&r
0019
MC/DC0%0/2
$end >= $min and $end <= $max
$end >= $min $end <= $max
Truth table33%1/3
A and B
A= $end >= $minB= $end <= $max
ABresult
0X0
100
111
565 19 ▶ die "$field range start $start must be <= end $end" if $start > $end && $field ne 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end and $field ne "dow" 0 19 19
Condition66%2/3
$start > $end and $field ne "dow"
!ll&&!rl&&r
1540
MC/DC0%0/2
$start > $end and $field ne "dow"
$start > $end $field ne "dow"
Truth table66%2/3
A and B
A= $start > $endB= $field ne "dow"
ABresult
0X0
100
111
566  
567 19 $node = Cron::Toolkit::Pattern::Range->new( field_type => $field );
568 19 $node->add_child( Cron::Toolkit::Pattern::Single->new( value => $start, field_type => $field ) );
569 19 $node->add_child( Cron::Toolkit::Pattern::Single->new( value => $end, field_type => $field ) );
570  
571 19 ▶ if ( $field eq 'dow' && $start > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "dow" and $start > $end) 4 15 19
Condition100%3/3
$field eq "dow" and $start > $end
!ll&&!rl&&r
874
MC/DC100%2/2
$field eq "dow" and $start > $end
$field eq "dow" $start > $end
Truth table100%3/3
A and B
A= $field eq "dow"B= $start > $end
ABresult
0X0
100
111
572 4 $node->{wrapped} = 1;
573 }
574 }
575 elsif ( $value =~ /^(\*|\d+)\/(\d+)$/ ) {
576 9 my ( $base_str, $step ) = ( $1, $2 );
577 9 ▶ die "$field step $step out of range [$min-$max]" unless $step >= $min && $step <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $step >= $min and $step <= $max 0 9 9
Condition33%1/3
$step >= $min and $step <= $max
!ll&&!rl&&r
009
MC/DC0%0/2
$step >= $min and $step <= $max
$step >= $min $step <= $max
Truth table33%1/3
A and B
A= $step >= $minB= $step <= $max
ABresult
0X0
100
111
578 9 ▶ die "$field base $base_str out of range [$min-$max]" if $base_str ne '*' && ( $base_str < $min || $base_str > $max );
Branch: 1 branch
BranchTrueFalseTotal
if $base_str ne "*" and $base_str < $min || $base_str > $max 0 9 9
Condition50%3/6
$base_str ne "*" and $base_str < $min || $base_str > $max
!ll&&!rl&&r
810
$base_str < $min or $base_str > $max
l!l&&r!l&&!r
001
MC/DC0%0/3
$base_str ne "*" and $base_str < $min || $base_str > $max
$base_str ne "*" $base_str < $min $base_str > $max
Truth table50%2/4
A and B or C
A= $base_str ne "*"B= $base_str < $minC= $base_str > $max
ABCresult
0XX0
1000
11X1
1011
579 9 $node = Cron::Toolkit::Pattern::Step->new(
580 type => 'step',
581 field_type => $field
582 );
583 9 ▶ my $base_node =
Branch: 1 branch
BranchTrueFalseTotal
$base_str eq '*' ? : 8 1 9
584 $base_str eq '*'
585 ? Cron::Toolkit::Pattern::Wildcard->new( type => 'wildcard', value => '*', field_type => $field )
586 : Cron::Toolkit::Pattern::Single->new( type => 'single', value => $base_str, field_type => $field );
587 9 $node->add_child($base_node);
588 9 $node->add_child(
589 Cron::Toolkit::Pattern::StepValue->new(
590 type => 'step_value',
591 value => $step,
592 field_type => $field
593 )
594 );
595 }
596 elsif ( $value =~ /^(\*|\d+)-(\d+)\/(\d+)$/ ) {
597 2 my ( $base_str, $end, $step ) = ( $1, $2, $3 );
598 2 ▶ my $start = $base_str eq '*' ? $min : $base_str;
Branch: 1 branch
BranchTrueFalseTotal
$base_str eq '*' ? : 0 2 2
599  
600 2 ▶ die "$field start $start out of range" unless $start >= $min && $start <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $start >= $min and $start <= $max 0 2 2
Condition33%1/3
$start >= $min and $start <= $max
!ll&&!rl&&r
002
MC/DC0%0/2
$start >= $min and $start <= $max
$start >= $min $start <= $max
Truth table33%1/3
A and B
A= $start >= $minB= $start <= $max
ABresult
0X0
100
111
601 2 ▶ die "$field end $end out of range" unless $end >= $min && $end <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $end >= $min and $end <= $max 0 2 2
Condition33%1/3
$end >= $min and $end <= $max
!ll&&!rl&&r
002
MC/DC0%0/2
$end >= $min and $end <= $max
$end >= $min $end <= $max
Truth table33%1/3
A and B
A= $end >= $minB= $end <= $max
ABresult
0X0
100
111
602 2 ▶ die "$field step $step invalid" unless $step > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $step > 0 0 2 2
603  
604 2 my $wrapped = 0;
605 2 ▶ if ( $field eq 'dow' && $start > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'dow' and $start > $end) { } 0 2 2
Condition33%1/3
$field eq "dow" and $start > $end
!ll&&!rl&&r
200
MC/DC0%0/2
$field eq "dow" and $start > $end
$field eq "dow" $start > $end
Truth table33%1/3
A and B
A= $field eq "dow"B= $start > $end
ABresult
0X0
100
111
606 0 $wrapped = 1;
607 }
608 else {
609 2 ▶ die "$field range start $start must be <= end $end" if $start > $end && $field ne 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end and $field ne "dow" 0 2 2
Condition33%1/3
$start > $end and $field ne "dow"
!ll&&!rl&&r
200
MC/DC0%0/2
$start > $end and $field ne "dow"
$start > $end $field ne "dow"
Truth table33%1/3
A and B
A= $start > $endB= $field ne "dow"
ABresult
0X0
100
111
610 }
611  
612 2 my $range_node = Cron::Toolkit::Pattern::Range->new(
613 field_type => $field,
614 wrapped => $wrapped
615 );
616 2 $range_node->add_child( Cron::Toolkit::Pattern::Single->new( value => $start, field_type => $field ) );
617 2 $range_node->add_child( Cron::Toolkit::Pattern::Single->new( value => $end, field_type => $field ) );
618  
619 2 $node = Cron::Toolkit::Pattern::Step->new( field_type => $field );
620 2 $node->add_child($range_node);
621 2 $node->add_child( Cron::Toolkit::Pattern::StepValue->new( value => $step, field_type => $field ) );
622 }
623 elsif ( $value =~ /,/ ) {
624 9 $node = Cron::Toolkit::Pattern::List->new(
625 type => 'list',
626 field_type => $field
627 );
628 9 for my $sub ( split /,/, $value ) {
629 27 eval {
630 27 my $sub_node = $self->_build_node( $field, $sub );
631 27 ▶ die "Invalid list element in $field: list not allowed" if $sub_node->type eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
if $sub_node->type eq "list" 0 27 27
632 27 $node->add_child($sub_node);
633 };
634 27 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 27 27
635 0 my $error = $@;
636 0 $error =~ s/^Invalid $field:/Invalid $field list element:/;
637 0 $error =~ s/^$field ([^:]+):/Invalid $field list element $1:/;
638 0 die $error;
639 }
640 }
641 }
642 else {
643 0 die "Unsupported field: $value ($field)";
644 }
645  
646 713 return $node;
647 }
648  
649 126 ▶sub utc_offset {
Subroutine utc_offset: called CC=5 SCAR=18.4
Pod undocumented
650 126 my ( $self, $offset ) = @_;
651 126 ▶ if ( $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 17 109 126
652 17 ▶ if ( $offset !~ /^-?\d+$/ || $offset < -1080 || $offset > 1080 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $offset =~ /^-?\d+$/ or $offset < -1080 or $offset > 1080) 0 17 17
Condition33%2/6
not $offset =~ /^-?\d+$/ or $offset < -1080 or $offset > 1080
l!l&&r!l&&!r
0017
not $offset =~ /^-?\d+$/ or $offset < -1080
l!l&&r!l&&!r
0017
MC/DC0%0/3
not $offset =~ /^-?\d+$/ or $offset < -1080 or $offset > 1080
not $offset =~ /^-?\d+$/ $offset < -1080 $offset > 1080
Truth table25%1/4
not(A) or B or C
A= not $offset =~ /^-?\d+$/B= $offset < -1080C= $offset > 1080
ABCresult
1XX1
01X1
0011
0000
653 0 die "Invalid utc_offset '$offset': must be an integer between -1080 and 1080 minutes";
654 }
655 17 $self->{utc_offset} = $offset;
656 }
657 126 return $self->{utc_offset};
658 }
659  
660 40 ▶sub time_zone {
Subroutine time_zone: called CC=3 SCAR=11.1
Pod undocumented
661 40 my ( $self, $tz ) = @_;
662 40 ▶ if ( $tz ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz) 40 0 40
663 40 my $zone = eval { DateTime::TimeZone->new( name => $tz ) };
664 40 ▶ die "Invalid time_zone '$tz': must be a valid TZ identifier ($@)" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 40 40
665 40 $self->{time_zone} = $tz;
666 40 my $tm = Time::Moment->now_utc;
667 40 $self->{utc_offset} = $zone->offset_for_datetime($tm) / 60; # Recalc to minutes (DST-aware)
668 }
669 40 return $self->{time_zone};
670 }
671  
672 0 ▶sub begin_epoch {
Subroutine begin_epoch: not called CC=5 SCAR=34.0
Pod undocumented
673 0 my ( $self, $new_begin ) = @_;
674 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
675 0 ▶ die "Invalid begin_epoch '$new_begin': must be a non-negative integer" unless defined $new_begin && $new_begin =~ /^\d+$/ && $new_begin >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_begin and $new_begin =~ /^\d+$/ and $new_begin >= 0 0 0 0
Condition0%0/6
defined $new_begin and $new_begin =~ /^\d+$/ and $new_begin >= 0
!ll&&!rl&&r
000
defined $new_begin and $new_begin =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $new_begin and $new_begin =~ /^\d+$/ and $new_begin >= 0
defined $new_begin $new_begin =~ /^\d+$/ $new_begin >= 0
Truth table0%0/4
A and B and C
A= defined $new_beginB= $new_begin =~ /^\d+$/C= $new_begin >= 0
ABCresult
0XX0
10X0
1100
1111
676 0 $self->{begin_epoch} = $new_begin;
677 }
678 0 return $self->{begin_epoch};
679 }
680  
681 0 ▶sub end_epoch {
Subroutine end_epoch: not called CC=5 SCAR=34.0
Pod undocumented
682 0 my ( $self, $new_end ) = @_;
683 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
684 0 ▶ die "Invalid end_epoch '$new_end': must be undef or a non-negative integer" unless !defined $new_end || ( $new_end =~ /^\d+$/ && $new_end >= 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $new_end or $new_end =~ /^\d+$/ and $new_end >= 0 0 0 0
Condition0%0/6
not defined $new_end or $new_end =~ /^\d+$/ and $new_end >= 0
l!l&&r!l&&!r
000
$new_end =~ /^\d+$/ and $new_end >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $new_end or $new_end =~ /^\d+$/ and $new_end >= 0
not defined $new_end $new_end =~ /^\d+$/ $new_end >= 0
Truth table0%0/4
not(A) or B and C
A= not defined $new_endB= $new_end =~ /^\d+$/C= $new_end >= 0
ABCresult
1XX1
0111
00X0
0100
685 0 $self->{end_epoch} = $new_end;
686 }
687 0 return $self->{end_epoch};
688 }
689  
690 5 ▶sub user {
Subroutine user: called CC=2 SCAR=6.9
Pod undocumented
691 5 my ($self, $user) = @_;
692 5 ▶ $self->{user} = $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 2 3 5
693 5 return $self->{user};
694 }
695  
696 12 ▶sub command {
Subroutine command: called CC=2 SCAR=6.9
Pod undocumented
697 12 my ($self, $command) = @_;
698 12 ▶ $self->{command} = $command if $command;
Branch: 1 branch
BranchTrueFalseTotal
if $command 6 6 12
699 12 return $self->{command};
700 }
701  
702 8 ▶sub env {
Subroutine env: called CC=2 SCAR=6.9
Pod undocumented
703 8 my ($self, $env) = @_;
704 8 ▶ $self->{env} = $env if $env;
Branch: 1 branch
BranchTrueFalseTotal
if $env 6 2 8
705 8 return $self->{env};
706 }
707  
708 0 ▶sub as_unix_string {
Subroutine as_unix_string: not called CC=1 SCAR=6.9
Pod undocumented
709 0 my $self = shift;
710 0 my $expr = $self->_as_string;
711 0 $expr =~ s/\?/*/;
712 0 my @fields = split( /\s+/, $expr );
713 0 shift @fields; # remove seconds
714 0 pop @fields; # remove year
715 0 return join( ' ', @fields );
716 }
717  
718 84 ▶sub as_quartz_string {
Subroutine as_quartz_string: called CC=3 SCAR=11.0
Pod documented
719 84 my $self = shift;
720 84 my $expr = $self->_as_string;
721 84 my @fields = split /\s+/, $expr;
722  
723 84 ▶ return $expr unless @fields > 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields > 5 0 84 84
724  
725 84 my $dow = $fields[5];
726  
727 84 $dow =~ s{
728 (?<![L#/]) # not preceded by L or #
729 \b([1-7])\b # standalone 1-7
730 }{
731 33 ▶ $1 == 7 ? 1 : $1 + 1
Branch: 1 branch
BranchTrueFalseTotal
$1 == 7 ? : 3 30 33
732 }gex;
733  
734 84 $fields[5] = $dow;
735 84 return join ' ', @fields;
736 }
737  
738 99 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
739 99 my $self = shift;
740 99 return $self->_as_string;
741 }
742  
743 183 ▶sub _as_string {
Subroutine _as_string: called CC=1 SCAR=0.0
744 183 my $self = shift;
745 183 my $string = join( ' ', map { $self->_rebuild_from_node($_) } @{ $self->{nodes} } );
746 }
747  
748 0 ▶sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
Pod documented
749 0 my $self = shift;
750 0 return JSON::PP::encode_json(
751 {
752 expression => $self->_as_string,
753 description => $self->describe,
754 utc_offset => $self->utc_offset,
755 time_zone => $self->time_zone,
756 begin_epoch => $self->begin_epoch,
757 end_epoch => $self->end_epoch,
758 }
759 );
760 }
761  
762 1 ▶sub new_from_crontab {
Subroutine new_from_crontab: called CC=21 SCAR=30.9
Pod documented
763 1 my ( $class, $content ) = @_;
764 1 ▶ die "crontab content required (string)" unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 1 1
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
001
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
765 1 my @crons;
766 my %env;
767 1 foreach my $line ( split /\n/, $content ) {
768  
769 # Strip trailing comments and trim
770 22 $line =~ s/\s*#.*$//; # Remove comments from end
771 22 $line =~ s/^\s+|\s+$//g; # Trim whitespace
772 22 ▶ next unless $line =~ /\S/; # Skip empty
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 13 9 22
773  
774 9 ▶ if ( $line =~ /^([A-Z_][A-Z0-9_]*)=(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([A-Z_][A-Z0-9_]*)=(.*)$/) 3 6 9
775 3 $env{$1} = $2;
776 3 next;
777 }
778  
779 6 ▶ while ( my ( $var, $val ) = each %env ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %env) 14 6 20
780 14 $line =~ s/\$$var\b/$val/g;
781 }
782  
783 6 my @parts = split /\s+/, $line;
784  
785 6 my @cron_parts;
786 6 my $is_alias = 0;
787 6 for my $part (@parts) {
788 33 ▶ last if @cron_parts >= 7; # Cap at max Quartz fields
Branch: 1 branch
BranchTrueFalseTotal
if @cron_parts >= 7 1 32 33
789 32 ▶ if ( @cron_parts == 0 && $part =~ /^@/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@cron_parts == 0 and $part =~ /^@/) { } 1 31 32
elsif ($part =~ m[^[0-9*?,/\-L#W?]+$] or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::DOW_MAP_UNIX) or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::MONTH_MAP)) { } 27 4 31
Condition100%3/3
@cron_parts == 0 and $part =~ /^@/
!ll&&!rl&&r
2651
MC/DC100%2/2
@cron_parts == 0 and $part =~ /^@/
@cron_parts == 0 $part =~ /^@/
Truth table100%3/3
A and B
A= @cron_parts == 0B= $part =~ /^@/
ABresult
0X0
100
111
790  
791 # Alias as single token
792 1 push @cron_parts, $part;
793 1 $is_alias = 1;
794 1 last; # Aliases are single
795 }
796 70 ▶ elsif ( $part =~ /^[0-9*?,\/\-L#W?]+$/ || scalar (grep { $part =~ /$_/ } keys %DOW_MAP_UNIX) || scalar (grep { $part =~ /$_/ } keys %MONTH_MAP) ) { # Cron-like: digits, *, ?, -, /, ,, L, W, #
Condition100%4/4
$part =~ m[^[0-9*?,/\-L#W?]+$] or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::DOW_MAP_UNIX) or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::MONTH_MAP)
l!l
274
$part =~ m[^[0-9*?,/\-L#W?]+$] or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::DOW_MAP_UNIX)
l!l
265
MC/DC100%1/1
$part =~ m[^[0-9*?,/\-L#W?]+$] or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::DOW_MAP_UNIX) or scalar grep({$part =~ /$_/;} keys %Cron::Toolkit::MONTH_MAP)
$part =~ m[^[0-9*?,/\-L#W?]+$]
Truth table100%2/2
A
A= $part =~ m[^[0-9*?,/\-L#W?]+$]
Aresult
11
00
797 27 push @cron_parts, $part;
798 }
799 else {
800 4 last; # Non-cron token
801 }
802 }
803  
804 # Validate expression length
805 6 my $expr = join ' ', @cron_parts;
806 6 ▶ next unless $is_alias || ( @cron_parts >= 5 && @cron_parts <= 7 );
Branch: 1 branch
BranchTrueFalseTotal
unless $is_alias or @cron_parts >= 5 and @cron_parts <= 7 0 6 6
Condition50%3/6
$is_alias or @cron_parts >= 5 and @cron_parts <= 7
l!l&&r!l&&!r
150
@cron_parts >= 5 and @cron_parts <= 7
!ll&&!rl&&r
005
MC/DC0%0/3
$is_alias or @cron_parts >= 5 and @cron_parts <= 7
$is_alias @cron_parts >= 5 @cron_parts <= 7
Truth table50%2/4
A or B and C
A= $is_aliasB= @cron_parts >= 5C= @cron_parts <= 7
ABCresult
1XX1
0111
00X0
0100
807  
808 # Extract user: Next token after prefix, if simple word (alphanumeric, no / or special)
809 6 my ($user, $command);
810 6 my $cron_end = scalar @cron_parts;
811 6 my $next_start = $cron_end;
812 6 ▶ if ( @parts > $cron_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > $cron_end) 6 0 6
813 6 my $potential_user = $parts[$cron_end];
814 6 ▶ if ( $potential_user =~ /^\w+$/ ) { # Simple username: letters/digits/_
Branch: 1 branch
BranchTrueFalseTotal
if ($potential_user =~ /^\w+$/) 2 4 6
815 2 $user = $potential_user;
816 2 $next_start = $cron_end + 1;
817 }
818 }
819  
820 6 ▶ $command = join ' ', @parts[ $next_start .. $#parts ] if @parts > $next_start;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > $next_start 6 0 6
821  
822 6 my $cron;
823 6 eval {
824 6 $cron = $class->new(
825 expression => $expr,
826 user => $user,
827 command => $command,
828 env => {%env} # Copy current env
829 );
830 };
831 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 6 6
832 0 warn "Skipped invalid crontab line: '$line' ($@)";
833 }
834 else {
835 6 push @crons, $cron;
836 }
837 }
838 1 return @crons;
839 }
840  
841 84 ▶sub dump_tree {
Subroutine dump_tree: called CC=5 SCAR=16.1
Pod documented
842 84 my ( $self, $indent ) = @_;
843 84 my $out;
844  
845 84 my @names = qw(second minute hour dom month dow year);
846 84 for my $i ( 0 .. $#{ $self->{nodes} } ) {
847 588 my $node = $self->{nodes}[$i];
848 588 my $name = $names[$i];
849  
850 588 ▶ my $prefix = $i == 0 ? '┌─' : $i == $#{ $self->{nodes} } ? '└─' : '├─';
Branch: 2 branches
BranchTrueFalseTotal
$i == 0 ? : 84 504 588
$i == $#{$$self{'nodes'};} ? : 84 420 504
851 588 ▶ my $child_indent = $i == $#{ $self->{nodes} } ? ' ' : '│ ';
Branch: 1 branch
BranchTrueFalseTotal
$i == $#{$$self{'nodes'};} ? : 84 504 588
852  
853 588 $out .= "$prefix $name: " . $node->_dump_tree($child_indent) . "\n";
854 }
855 84 return $out;
856 }
857  
858 1459 ▶sub _rebuild_from_node {
Subroutine _rebuild_from_node: called CC=12 SCAR=24.9
859 1459 my ( $self, $node ) = @_;
860 1459 my $type = $node->type;
861 1459 ▶ return '*' if $type eq 'wildcard';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "wildcard" 376 1083 1459
862 1083 ▶ return '?' if $type eq 'unspecified';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "unspecified" 183 900 1083
863 900 ▶ return $node->value if $type eq 'single' || $type eq 'last' || $type eq 'lastW' || $type eq 'nth' || $type eq 'nearest_weekday' || $type eq 'step_value';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" or $type eq "last" or $type eq "lastW" or $type eq "nth" or $type eq "nearest_weekday" or $type eq "step_value" 820 80 900
Condition100%15/15
$type eq "single" or $type eq "last" or $type eq "lastW" or $type eq "nth" or $type eq "nearest_weekday" or $type eq "step_value"
l!l&&r!l&&!r
7982280
$type eq "single" or $type eq "last" or $type eq "lastW" or $type eq "nth" or $type eq "nearest_weekday"
l!l&&r!l&&!r
7926102
$type eq "single" or $type eq "last" or $type eq "lastW" or $type eq "nth"
l!l&&r!l&&!r
77319108
$type eq "single" or $type eq "last" or $type eq "lastW"
l!l&&r!l&&!r
7712127
$type eq "single" or $type eq "last"
l!l&&r!l&&!r
74229129
MC/DC100%6/6
$type eq "single" or $type eq "last" or $type eq "lastW" or $type eq "nth" or $type eq "nearest_weekday" or $type eq "step_value"
$type eq "single" $type eq "last" $type eq "lastW" $type eq "nth" $type eq "nearest_weekday" $type eq "step_value"
Truth table100%7/7
A or B or C or D or E or F
A= $type eq "single"B= $type eq "last"C= $type eq "lastW"D= $type eq "nth"E= $type eq "nearest_weekday"F= $type eq "step_value"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
864 80 ▶ return $self->_rebuild_from_node( $node->{children}[0] ) . '-' . $self->_rebuild_from_node( $node->{children}[1] ) if $type eq 'range';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "range" 40 40 80
865 40 ▶ return $self->_rebuild_from_node( $node->{children}[0] ) . '/' . $self->_rebuild_from_node( $node->{children}[1] ) if $type eq 'step';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "step" 22 18 40
866 18 ▶ return join ',', map { $self->_rebuild_from_node($_) } @{ $node->{children} } if $type eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "list" 18 0 18
867 0 die "Unsupported for rebuild: $type";
868 }
869  
870 # describing
871  
872 84 ▶sub describe {
Subroutine describe: called CC=21 SCAR=30.5
Pod documented
873 84 my $self = shift;
874 84 my $hms;
875 84 my $dmy = '';
876 84 my @nodes;
877  
878 84 my $wildcards = scalar grep { $_->type eq 'wildcard' } @{ $self->{nodes} }[ 0 .. 2 ];
879 84 my $singles = scalar grep { $_->type eq 'single' } @{ $self->{nodes} }[ 0 .. 2 ];
880  
881 # dedupe wildcards
882 84 my $prev_type = '';
883 84 for my $node ( @{ $self->{nodes} } ) {
884 588 ▶ push @nodes, $node->type eq 'unspecified' || ($node->type eq 'wildcard' && $prev_type eq 'wildcard') ? undef : $node;
Branch: 1 branch
BranchTrueFalseTotal
$node->type eq 'unspecified' || $node->type eq 'wildcard' && $prev_type eq 'wildcard' ? : 126 462 588
Condition100%6/6
$node->type eq "unspecified" or $node->type eq "wildcard" and $prev_type eq "wildcard"
l!l&&r!l&&!r
8442462
$node->type eq "wildcard" and $prev_type eq "wildcard"
!ll&&!rl&&r
34212042
MC/DC100%3/3
$node->type eq "unspecified" or $node->type eq "wildcard" and $prev_type eq "wildcard"
$node->type eq "unspecified" $node->type eq "wildcard" $prev_type eq "wildcard"
Truth table100%4/4
A or B and C
A= $node->type eq "unspecified"B= $node->type eq "wildcard"C= $prev_type eq "wildcard"
ABCresult
1XX1
0111
00X0
0100
885
886 588 $prev_type = $node->type;
887 }
888  
889 # HMS
890 84 ▶ if ( $wildcards == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wildcards == 3) { } 2 82 84
elsif ($singles == 3) { } 65 17 82
891 2 $hms = $nodes[0]->to_english;
892 }
893 elsif ( $singles == 3 ) {
894 65 $hms = format_time( map { $_->value } @nodes[ 0 .. 2 ] );
895 }
896 else {
897 17 ▶ $hms = join( ' of ', map { $_->to_english } grep { defined $_ && !( $_->type eq 'single' && $_->value == 0 ) } @nodes[ 0 .. 2 ] );
Condition100%6/6
defined $_ && !($_->type eq 'single' && $_->value == 0)
!ll&&!rl&&r
51729
$_->type eq 'single' && $_->value == 0
!ll&&!rl&&r
26317
MC/DC100%3/3
defined $_ && !($_->type eq 'single' && $_->value == 0)
defined $_ $_->type eq 'single' $_->value == 0
Truth table100%4/4
A && not(B && C)
A= defined $_B= $_->type eq 'single'C= $_->value == 0
ABCresult
0XX0
1110
10X1
1101
898 }
899  
900 # DMY
901 84 ▶ if ( defined $nodes[3] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[3]) 45 39 84
902 45 ▶ if ( $nodes[3]->type eq 'single' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[3]->type eq "single") 24 21 45
903 24 $dmy .= 'on ';
904 }
905 45 $dmy .= $nodes[3]->to_english . ' of ' . $self->{nodes}[4]->to_english;
906 }
907  
908 84 ▶ if ( defined $nodes[3] && defined $nodes[5] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[3] and defined $nodes[5]) 0 84 84
Condition66%2/3
defined $nodes[3] and defined $nodes[5]
!ll&&!rl&&r
39450
MC/DC0%0/2
defined $nodes[3] and defined $nodes[5]
defined $nodes[3] defined $nodes[5]
Truth table66%2/3
A and B
A= defined $nodes[3]B= defined $nodes[5]
ABresult
0X0
100
111
909 0 $dmy .= ' and ';
910 }
911  
912 84 ▶ if ( defined $nodes[5] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[5]) 26 58 84
913  
914 26 ▶ if ( $nodes[5]->type eq 'single' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[5]->type eq "single") 3 23 26
915 3 $dmy .= 'every ';
916 }
917 26 $dmy .= $nodes[5]->to_english . ' of ' . $self->{nodes}[4]->to_english;
918 }
919  
920 84 ▶ if ( defined $nodes[4] && $nodes[4]->type ne 'wildcard' && !defined $nodes[3] && !defined $nodes[5] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[4] and $nodes[4]->type ne "wildcard" and not defined $nodes[3] and not defined $nodes[5]) 1 83 84
Condition88%8/9
defined $nodes[4] and $nodes[4]->type ne "wildcard" and not defined $nodes[3] and not defined $nodes[5]
!ll&&!rl&&r
8301
defined $nodes[4] and $nodes[4]->type ne "wildcard" and not defined $nodes[3]
!ll&&!rl&&r
57261
defined $nodes[4] and $nodes[4]->type ne "wildcard"
!ll&&!rl&&r
203727
MC/DC75%3/4
defined $nodes[4] and $nodes[4]->type ne "wildcard" and not defined $nodes[3] and not defined $nodes[5]
defined $nodes[4] $nodes[4]->type ne "wildcard" not defined $nodes[3] not defined $nodes[5]
Truth table80%4/5
A and B and not(C) and not(D)
A= defined $nodes[4]B= $nodes[4]->type ne "wildcard"C= not defined $nodes[3]D= not defined $nodes[5]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
921 1 $dmy .= 'of ' . $self->{nodes}[4]->to_english;
922 }
923  
924 84 ▶ if ( defined $nodes[6] && $nodes[6]->type ne 'wildcard' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[6] and $nodes[6]->type ne "wildcard") 22 62 84
Condition66%2/3
defined $nodes[6] and $nodes[6]->type ne "wildcard"
!ll&&!rl&&r
06222
MC/DC50%1/2
defined $nodes[6] and $nodes[6]->type ne "wildcard"
defined $nodes[6] $nodes[6]->type ne "wildcard"
Truth table66%2/3
A and B
A= defined $nodes[6]B= $nodes[6]->type ne "wildcard"
ABresult
0X0
100
111
925 22 $dmy .= ' ' . $self->{nodes}[6]->to_english;
926 }
927 84 return join ' ', grep { $_ } ($hms, $dmy);
928 }
929  
930 # matching
931  
932 5 ▶sub is_match {
Subroutine is_match: called CC=2 SCAR=7.0
Pod documented
933 5 my ( $self, $epoch_seconds ) = @_;
934 5 my $tm = Time::Moment->from_epoch($epoch_seconds);
935 5 ▶ return unless $tm;
Branch: 1 branch
BranchTrueFalseTotal
unless $tm 0 5 5
936 5 return $self->_is_match($tm);
937 }
938  
939 498427 ▶sub _is_match {
Subroutine _is_match: called CC=6 SCAR=17.9
940 498427 my ( $self, $tm ) = @_;
941  
942 498427 NODE: for my $node ( @{ $self->{nodes} } ) {
943 1989654 my $value = $self->_field_value( $tm, $node->field_type );
944 1989654 ▶ if ( $node->type eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->type eq "list") 140 1989514 1989654
945 140 for my $child ( @{ $node->children } ) {
946 428 ▶ next NODE if $child->match( $value, $tm );
Branch: 1 branch
BranchTrueFalseTotal
if $child->match($value, $tm) 26 402 428
947 }
948 114 return 0;
949 }
950 1989514 ▶ return 0 unless $node->match( $value, $tm );
Branch: 1 branch
BranchTrueFalseTotal
unless $node->match($value, $tm) 498114 1491400 1989514
951 }
952 199 return 1;
953 }
954  
955 87 ▶sub next {
Subroutine next: called CC=12 SCAR=25.0
Pod documented
956 87 my ( $self, $epoch_seconds ) = @_;
957 87 ▶ $epoch_seconds //= time;
Condition50%1/2
$epoch_seconds //= time
l!l
870
MC/DC0%0/1
$epoch_seconds //= time
$epoch_seconds
Truth table50%1/2
A
A= $epoch_seconds
Aresult
11
00
958  
959 87 my $clamped = max( $epoch_seconds, $self->{begin_epoch} );
960 87 ▶ return if $clamped > $self->{end_epoch};
Branch: 1 branch
BranchTrueFalseTotal
if $clamped > $self->{"end_epoch"} 0 87 87
961  
962 my $tm = Time::Moment->from_epoch($clamped)
963 87 ->with_offset_same_instant( $self->{utc_offset} );
964 87 $tm = $tm->plus_seconds(1);
965  
966 # HMS shortcut / odometer carry
967 87 NODE: foreach my $i ( 0 .. 2 ) {
968 246 my $node = $self->{nodes}[$i];
969 246 my $curval = $self->_field_value( $tm, $node->field_type );
970 246 my $lowval = $node->lowest($tm);
971 246 my $highval = $node->highest($tm);
972  
973 # Changed condition + explicit handling for exact highval
974 246 ▶ if ($curval > $highval || ($curval == $highval && !$node->match($curval, $tm))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curval > $highval or $curval == $highval and not $node->match($curval, $tm)) 195 51 246
Condition66%4/6
$curval > $highval or $curval == $highval and not $node->match($curval, $tm)
l!l&&r!l&&!r
195051
$curval == $highval and not $node->match($curval, $tm)
!ll&&!rl&&r
4470
MC/DC33%1/3
$curval > $highval or $curval == $highval and not $node->match($curval, $tm)
$curval > $highval $curval == $highval not $node->match($curval, $tm)
Truth table75%3/4
A or B and not(C)
A= $curval > $highvalB= $curval == $highvalC= not $node->match($curval, $tm)
ABCresult
1XX1
0111
00X0
0100
975 195 $tm = $self->_set_date( $tm, $node->field_type, $lowval );
976 195 $tm = $self->_plus_one( $tm, $self->{nodes}[ $i + 1 ]->field_type );
977 195 next NODE;
978 }
979  
980 51 for my $c ( $curval .. $highval ) {
981 697 my $c_tm = $self->_set_date( $tm, $node->field_type, $c );
982 697 ▶ if ( $self->_is_match($c_tm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_match($c_tm)) 18 679 697
983 18 $tm = $c_tm;
984 18 last NODE;
985 }
986 }
987  
988 # flip if no match found in this field
989 33 $tm = $self->_set_date( $tm, $node->field_type, $lowval );
990 33 $tm = $self->_plus_one( $tm, $self->{nodes}[ $i + 1 ]->field_type );
991 }
992  
993 # the brute force approach for DMY is correct here because:
994 # 1) the design is simple and easy to understand and debug
995 # 2) solves all tricky end-of-month and leap year calculations
996 # 3) 365 iterations per one-year time window is good enough
997  
998 87 my $max_tm = Time::Moment->new( year => 2099, month => 12, day => 31,
999 hour => 23, minute => 59, second => 59 );
1000 87 ▶ my $max_iter = $tm->delta_days($max_tm) || 1;
Condition50%1/2
$tm->delta_days($max_tm) || 1
l!l
870
MC/DC0%0/1
$tm->delta_days($max_tm) || 1
$tm->delta_days($max_tm)
Truth table50%1/2
A
A= $tm->delta_days($max_tm)
Aresult
11
00
1001  
1002 87 for my $day ( 1 .. $max_iter ) {
1003 330177 ▶ return $tm->epoch if $self->_is_match($tm);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_match($tm) 75 330102 330177
1004 330102 $tm = $tm->plus_days(1);
1005 }
1006 12 return;
1007 }
1008  
1009 84 ▶sub previous {
Subroutine previous: called CC=12 SCAR=25.0
Pod documented
1010 84 my ( $self, $epoch_seconds ) = @_;
1011 84 ▶ $epoch_seconds //= time;
Condition50%1/2
$epoch_seconds //= time
l!l
840
MC/DC0%0/1
$epoch_seconds //= time
$epoch_seconds
Truth table50%1/2
A
A= $epoch_seconds
Aresult
11
00
1012  
1013 84 my $clamped = min( $epoch_seconds, $self->{end_epoch} );
1014 84 ▶ return if $clamped < $self->{begin_epoch};
Branch: 1 branch
BranchTrueFalseTotal
if $clamped < $self->{"begin_epoch"} 0 84 84
1015  
1016 my $tm = Time::Moment->from_epoch($clamped)
1017 84 ->with_offset_same_instant( $self->{utc_offset} );
1018 84 $tm = $tm->minus_seconds(1);
1019  
1020 # HMS shortcut / odometer carry (backward)
1021 84 NODE: foreach my $i ( 0 .. 2 ) {
1022 233 my $node = $self->{nodes}[$i];
1023 233 my $curval = $self->_field_value( $tm, $node->field_type );
1024 233 my $lowval = $node->lowest($tm);
1025 233 my $highval = $node->highest($tm);
1026  
1027 # Changed condition + explicit handling for exact lowval
1028 233 ▶ if ($curval < $lowval || ($curval == $lowval && !$node->match($curval, $tm))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curval < $lowval or $curval == $lowval and not $node->match($curval, $tm)) 2 231 233
Condition66%4/6
$curval < $lowval or $curval == $lowval and not $node->match($curval, $tm)
l!l&&r!l&&!r
20231
$curval == $lowval and not $node->match($curval, $tm)
!ll&&!rl&&r
22830
MC/DC33%1/3
$curval < $lowval or $curval == $lowval and not $node->match($curval, $tm)
$curval < $lowval $curval == $lowval not $node->match($curval, $tm)
Truth table75%3/4
A or B and not(C)
A= $curval < $lowvalB= $curval == $lowvalC= not $node->match($curval, $tm)
ABCresult
1XX1
0111
00X0
0100
1029 2 $tm = $self->_set_date( $tm, $node->field_type, $highval );
1030 2 $tm = $self->_minus_one( $tm, $self->{nodes}[ $i + 1 ]->field_type );
1031 2 next NODE;
1032 }
1033  
1034 231 ▶ for ( my $c = $curval ; $c >= $lowval ; $c-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= $lowval) 9950 205 10155
1035 9950 my $c_tm = $self->_set_date( $tm, $node->field_type, $c );
1036 9950 ▶ if ( $self->_is_match($c_tm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_match($c_tm)) 26 9924 9950
1037 26 $tm = $c_tm;
1038 26 last NODE;
1039 }
1040 }
1041  
1042 # flip if no match found in this field
1043 205 $tm = $self->_set_date( $tm, $node->field_type, $highval );
1044 205 $tm = $self->_minus_one( $tm, $self->{nodes}[ $i + 1 ]->field_type );
1045 }
1046  
1047 # Brute force for DOM/DOW/year (no year adjustment block)
1048 84 my $min_tm = Time::Moment->new( year => 1970, month => 1, day => 1,
1049 hour => 0, minute => 0, second => 0 );
1050 84 ▶ my $min_iter = $min_tm->delta_days($tm) || 1;
Condition50%1/2
$min_tm->delta_days($tm) || 1
l!l
840
MC/DC0%0/1
$min_tm->delta_days($tm) || 1
$min_tm->delta_days($tm)
Truth table50%1/2
A
A= $min_tm->delta_days($tm)
Aresult
11
00
1051  
1052 84 for my $day ( 0 .. $min_iter ) {
1053 157598 ▶ return $tm->epoch if $self->_is_match($tm);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_match($tm) 77 157521 157598
1054 157521 $tm = $tm->minus_days(1);
1055 }
1056 7 return;
1057 }
1058  
1059 1990133 ▶sub _field_value {
Subroutine _field_value: called CC=8 SCAR=20.8
1060 1990133 my ( $self, $tm, $field_type ) = @_;
1061 1990133 ▶ return $tm->second if $field_type eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "second" 498598 1491535 1990133
1062 1491535 ▶ return $tm->minute if $field_type eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "minute" 493902 997633 1491535
1063 997633 ▶ return $tm->hour if $field_type eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "hour" 489495 508138 997633
1064 508138 ▶ return $tm->day_of_month if $field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dom" 488183 19955 508138
1065 19955 ▶ return $tm->month if $field_type eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "month" 16550 3405 19955
1066 3405 ▶ return $tm->day_of_week if $field_type eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dow" 1939 1466 3405
1067 1466 ▶ return $tm->year if $field_type eq 'year';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "year" 1466 0 1466
1068 }
1069  
1070 11082 ▶sub _set_date {
Subroutine _set_date: called CC=9 SCAR=34.5
1071 11082 my ( $self, $tm, $field_type, $value ) = @_;
1072 11082 ▶ return $tm->with_second($value) if $field_type eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "second" 5052 6030 11082
1073 6030 ▶ return $tm->with_minute($value) if $field_type eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "minute" 4684 1346 6030
1074 1346 ▶ return $tm->with_hour($value) if $field_type eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "hour" 1346 0 1346
1075 0 ▶ return $tm->with_day_of_month($value) if $field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dom" 0 0 0
1076 0 ▶ return $tm->with_month($value) if $field_type eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "month" 0 0 0
1077 0 ▶ if ( $field_type eq 'dow' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_type eq "dow") 0 0 0
1078 0 ▶ $value = 7 if $value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 0 0 0 0
1079 0 return $tm->with_day_of_week($value);
1080 }
1081 0 ▶ return $tm->with_year($value) if $field_type eq 'year';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "year" 0 0 0
1082 }
1083  
1084 228 ▶sub _plus_one {
Subroutine _plus_one: called CC=8 SCAR=27.7
1085 228 my ( $self, $tm, $field_type ) = @_;
1086 228 ▶ return $tm->plus_seconds(1) if $field_type eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "second" 0 228 228
1087 228 ▶ return $tm->plus_minutes(1) if $field_type eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "minute" 82 146 228
1088 146 ▶ return $tm->plus_hours(1) if $field_type eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "hour" 77 69 146
1089 69 ▶ return $tm->plus_days(1) if $field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dom" 69 0 69
1090 0 ▶ return $tm->plus_months(1) if $field_type eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "month" 0 0 0
1091 0 ▶ return $tm->plus_weeks(1) if $field_type eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dow" 0 0 0
1092 0 ▶ return $tm->plus_years(1) if $field_type eq 'year';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "year" 0 0 0
1093 }
1094  
1095 207 ▶sub _minus_one {
Subroutine _minus_one: called CC=8 SCAR=27.7
1096 207 my ( $self, $tm, $field_type ) = @_;
1097 207 ▶ return $tm->minus_seconds(1) if $field_type eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "second" 0 207 207
1098 207 ▶ return $tm->minus_minutes(1) if $field_type eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "minute" 78 129 207
1099 129 ▶ return $tm->minus_hours(1) if $field_type eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "hour" 71 58 129
1100 58 ▶ return $tm->minus_days(1) if $field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dom" 58 0 58
1101 0 ▶ return $tm->minus_months(1) if $field_type eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "month" 0 0 0
1102 0 ▶ return $tm->minus_weeks(1) if $field_type eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "dow" 0 0 0
1103 0 ▶ return $tm->minus_years(1) if $field_type eq 'year';
Branch: 1 branch
BranchTrueFalseTotal
if $field_type eq "year" 0 0 0
1104 }
1105  
1106 2 1;
1107 __END__
↑ Summary Toolkit/Pattern/NearestWeekday.pm »