blib/lib/App/dateseq.pm

filter: Statementstmt 12.8% 18 / 140 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 56 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 44.4% 4 / 9 Podpod 100% 1 / 1 totaltotal 6.8% 23 / 334 CC 68 SCAR 66.3
CC
68
Cov
6%
dateseq
1260.0
__ANON__
552.0
__ANON__
90.0
CRAP 759.1
↑ Summary
1 package App::dateseq;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2022-10-12'; # DATE
11 1 our $DIST = 'App-dateseq'; # DIST
12 1 our $VERSION = '0.112'; # VERSION
13  
14 1 our %SPEC;
15  
16 $SPEC{dateseq} = {
17 v => 1.1,
18 summary => 'Generate a sequence of dates',
19 description => <<'_',
20  
21 This utility is similar to Unix <prog:seq> command, except that it generates a
22 sequence of dates.
23  
24 _
25 args_rels => {
26 'choose_one&' => [
27 [qw/business business6/],
28 ],
29 },
30 args => {
31 from => {
32 summary => 'Starting date',
33 schema => ['date*', {
34 'x.perl.coerce_to' => 'DateTime',
35 'x.perl.coerce_rules' => ['From_str::natural'],
36 }],
37 pos => 0,
38 },
39 to => {
40 summary => 'End date, if not specified will generate an infinite* stream of dates',
41 schema => ['date*', {
42 'x.perl.coerce_to' => 'DateTime',
43 'x.perl.coerce_rules' => ['From_str::natural'],
44 }],
45 pos => 1,
46 },
47 increment => {
48 schema => ['duration*', {
49 'x.perl.coerce_to' => 'DateTime::Duration',
50 }],
51 cmdline_aliases => {i=>{}},
52 pos => 2,
53 },
54 reverse => {
55 summary => 'Decrement instead of increment',
56 schema => 'true*',
57 cmdline_aliases => {r=>{}},
58 },
59 business => {
60 summary => 'Only list business days (Mon-Fri), '.
61 'or non-business days',
62 schema => ['bool*'],
63 tags => ['category:filtering'],
64 },
65  
66 include_dow => {
67 summary => 'Only show dates with these day-of-weeks',
68 schema => 'date::dow_nums*',
69 tags => ['category:filtering'],
70 },
71 exclude_dow => {
72 summary => 'Do not show dates with these day-of-weeks',
73 schema => 'date::dow_nums*',
74 tags => ['category:filtering'],
75 },
76  
77 include_month => {
78 summary => 'Only show dates with these month numbers',
79 schema => 'date::month_nums*',
80 tags => ['category:filtering'],
81 },
82 exclude_month => {
83 summary => 'Do not show dates with these month numbers',
84 schema => 'date::month_nums*',
85 tags => ['category:filtering'],
86 },
87  
88 business6 => {
89 summary => 'Only list business days (Mon-Sat), '.
90 'or non-business days',
91 schema => ['bool*'],
92 tags => ['category:filtering'],
93 },
94 header => {
95 summary => 'Add a header row',
96 schema => 'str*',
97 },
98 limit => {
99 summary => 'Only generate a certain amount of dates',
100 schema => ['posint*'],
101 cmdline_aliases => {n=>{}},
102 },
103 random => {
104 summary => 'Instead of incrementing/decrementing monotonically, generate random date between --to and --from',
105 schema => ['true*'],
106 description => <<'_',
107  
108 If you specify this, you have to specify `--to` *and* `--from`.
109  
110 Also, currently, if you also specify `--limit-yearly` or `--limit-monthly`, the
111 script may hang because it runs out of dates, so be careful when specifying
112 these options combined.
113  
114 _
115 },
116 limit_yearly => {
117 summary => 'Only output at most this number of dates for each year',
118 schema => ['posint*'],
119 },
120 limit_monthly => {
121 summary => 'Only output at most this number of dates for each month',
122 schema => ['posint*'],
123 },
124 # XXX limit_weekly, limit_daily, limit_hourly, limit_minutely, limit_secondly
125 strftime => {
126 summary => 'strftime() format for each date',
127 description => <<'_',
128  
129 Default is `%Y-%m-%d`, unless when hour/minute/second is specified, then it is
130 `%Y-%m-%dT%H:%M:%S`.
131  
132 `dateseq` actually uses <pm:DateTimeX::strftimeq>, so you can embed Perl code
133 for flexibility. For example:
134  
135 % dateseq 2019-11-19 2019-11-25 -f '%Y-%m-%d%( $_->day_of_week == 7 ? "su" : "" )q'
136  
137 will print something like:
138  
139 2019-11-19
140 2019-11-20
141 2019-11-21
142 2019-11-22
143 2019-11-23
144 2019-11-24su
145 2019-11-25
146  
147 _
148 schema => ['str*'],
149 cmdline_aliases => {f=>{}},
150 tags => ['category:formatting'],
151 },
152 format_class => {
153 summary => 'Use a DateTime::Format::* class for formatting',
154 schema => ['perl::modname'],
155 tags => ['category:formatting'],
156 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 require Complete::Module;
158 0 my %args = @_;
159 Complete::Module::complete_module(
160 0 word => $args{word}, ns_prefix => 'DateTime::Format');
161 },
162 1 description => <<'_',
163  
164 By default, <pm:DateTime::Format::Strptime> is used with pattern set from the
165 <strftime> option.
166  
167 _
168 },
169 format_class_attrs => {
170 summary => 'Arguments to pass to constructor of DateTime::Format::* class',
171 schema => ['hash'],
172 tags => ['category:formatting'],
173 },
174 eval => {
175 summary => 'Run perl code for each date',
176 schema => 'str*',
177 tags => ['category:output'],
178 cmdline_aliases => {e=>{}},
179 description => <<'_',
180  
181 Specified perl code will receive the date as DateTime object in `$_`and expected
182 to return result to print.
183  
184 _
185 },
186 },
187 examples => [
188 {
189 summary => 'Generate "infinite" dates from today',
190 src => '[[prog]]',
191 src_plang => 'bash',
192 test => 0,
193 'x.doc.show_result' => 0,
194 },
195 {
196 summary => 'Generate dates from 2015-01-01 to 2015-01-31',
197 src => '[[prog]] 2015-01-01 2015-01-31',
198 src_plang => 'bash',
199 'x.doc.max_result_lines' => 5,
200 },
201 {
202 summary => 'Generate dates from yesterday to 2 weeks from now',
203 src => '[[prog]] yesterday "2 weeks from now"',
204 src_plang => 'bash',
205 'x.doc.max_result_lines' => 5,
206 },
207 {
208 summary => 'Generate dates from 2015-01-31 to 2015-01-01 (reverse)',
209 src => '[[prog]] 2015-01-31 2015-01-01 -r',
210 src_plang => 'bash',
211 'x.doc.max_result_lines' => 5,
212 },
213 {
214 summary => 'Generate "infinite" dates from 2015-01-01 (reverse)',
215 src => '[[prog]] 2015-01-01 -r',
216 src_plang => 'bash',
217 test => 0,
218 'x.doc.show_result' => 0,
219 },
220 {
221 summary => 'Generate 10 dates from 2015-01-01',
222 src => '[[prog]] 2015-01-01 -n 10',
223 src_plang => 'bash',
224 'x.doc.max_result_lines' => 5,
225 },
226 {
227 summary => 'Generate dates with increment of 3 days',
228 src => '[[prog]] 2015-01-01 2015-01-31 -i P3D',
229 src_plang => 'bash',
230 'x.doc.max_result_lines' => 5,
231 },
232 {
233 summary => 'Generate first 20 business days (Mon-Fri) after 2015-01-01',
234 src => '[[prog]] 2015-01-01 --business -n 20 -f "%Y-%m-%d(%a)"',
235 src_plang => 'bash',
236 'x.doc.max_result_lines' => 10,
237 },
238 {
239 summary => 'Generate first 5 non-business days (Sat-Sun) after 2015-01-01',
240 src => '[[prog]] 2015-01-01 --no-business -n 5',
241 src_plang => 'bash',
242 'x.doc.max_result_lines' => 5,
243 },
244 {
245 summary => 'Show the first business day (Mon-Fri) of each month in 2021',
246 src => '[[prog]] 2021-01-01 2021-12-13 --business --limit-monthly 1 -f "%Y-%m-%d(%a)"',
247 src_plang => 'bash',
248 'x.doc.max_result_lines' => 10,
249 },
250 {
251 summary => 'Show the last business day (Mon-Fri) of each month in 2021',
252 src => '[[prog]] 2021-12-31 2021-01-01 -r --business --limit-monthly 1 -f "%Y-%m-%d(%a)"',
253 src_plang => 'bash',
254 'x.doc.max_result_lines' => 10,
255 },
256 {
257 summary => 'Show Mondays, Wednesdays, and Fridays between 2015-01-01 and 2015-02-28',
258 src => '[[prog]] 2015-01-01 2015-02-28 --include-dow Mo,We,Fr -f "%Y-%m-%d(%a)"',
259 src_plang => 'bash',
260 'x.doc.max_result_lines' => 5,
261 'x.doc.show_result' => 0, # temp, coerce fail
262 },
263 {
264 summary => 'Show dates (except Mondays) after 2015-01-01 and 2015-02-28',
265 src => '[[prog]] 2015-01-01 2015-02-28 --exclude-dow Mo -f "%Y-%m-%d(%a)"',
266 src_plang => 'bash',
267 'x.doc.max_result_lines' => 5,
268 'x.doc.show_result' => 0, # temp, coerce fail
269 },
270 {
271 summary => 'Generate a CSV data',
272 src => '[[prog]] 2010-01-01 2015-01-31 -f "%Y,%m,%d" --header "year,month,day"',
273 src_plang => 'bash',
274 'x.doc.max_result_lines' => 5,
275 },
276 {
277 summary => 'Generate periods (YYYY-MM)',
278 src => '[[prog]] 2010-01-01 2015-12-31 -i P1M -f "%Y-%m"',
279 src_plang => 'bash',
280 'x.doc.max_result_lines' => 5,
281 },
282 {
283 summary => 'List non-holidays in 2015 (using Indonesian holidays)',
284 description => <<'_',
285  
286 See also <prog:dateseq-idn> as alternative.
287  
288 _
289 src => 'setop --diff <([[prog]] 2015-01-01 2015-12-31) <(list-idn-holidays --year 2015)',
290 src_plang => 'bash',
291 'x.doc.max_result_lines' => 10,
292 },
293 {
294 summary => 'List non-holidays business days in 2015 (using Indonesian holidays)',
295 description => <<'_',
296  
297 See also <prog:dateseq-idn> as alternative.
298  
299 _
300 src => 'setop --diff <([[prog]] 2015-01-01 2015-12-31 --business) <(list-idn-holidays --year 2015)',
301 src_plang => 'bash',
302 'x.doc.max_result_lines' => 10,
303 },
304 {
305 summary => 'Use with fsql',
306 src => q{[[prog]] 2010-01-01 2015-12-01 -f "%Y,%m" -i P1M --header "year,month" | fsql --add-csv - --add-csv data.csv -F YEAR -F MONTH 'SELECT year, month, data1 FROM stdin WHERE YEAR(data.date)=year AND MONTH(data.date)=month'},
307 src_plang => 'bash',
308 'x.doc.show_result' => 0,
309 },
310 {
311 summary => 'Use %q (see DateTimeX::strftimeq)',
312 src => q{[[prog]] 2020-12-24 2021-01-15 -f '%Y-%m-%d%( $_->day_of_week == 7 ? "su" : "" )q'},
313 src_plang => 'bash',
314 'x.doc.max_result_lines' => 10,
315 },
316 {
317 summary => 'Print first and last days of each month of 2021',
318 src => q{[[prog]] 2021-01-01 2021-12-01 --increment '1 month' -e 'my $dt2 = $_->clone; $dt2->add(months=>1); $dt2->add(days => -1); $_->ymd . " " . $dt2->ymd'},
319 src_plang => 'bash',
320 'x.doc.max_result_lines' => 10,
321 },
322 {
323 summary => 'Retrieve MetaCPAN releases data for 2020, saved in monthly JSON files',
324 src => q{[[prog]] 2020-01-01 2020-12-01 --increment '1 month' -e 'my $dt2 = $_->clone; $dt2->add(months=>1); $dt2->add(days => -1); sprintf "list-metacpan-releases --from-date %sT00:00:00 --to-date %sT23:59:59 --json > %04d%02d.json", $_->ymd, $dt2->ymd, $_->year, $_->month' | bash},
325 src_plang => 'bash',
326 'x.doc.show_result' => 0,
327 },
328 {
329 summary => 'Generate 100 random dates between a certain range',
330 src => q{[[prog]] --random --from "1 year ago" --to "1 year from now" --limit 100},
331 src_plang => 'bash',
332 'x.doc.max_result_lines' => 5,
333 },
334 ],
335 links => [
336 {url=>'prog:durseq', summary=>'Produce sequence of date durations'},
337 {url=>'prog:dateseq-idn', summary=>'A wrapper for dateseq, with built-in support for Indonesian holidays'},
338 {url=>'prog:seq'},
339 {url=>'prog:seq-pl', summary=>'Perl variant of seq'},
340 ],
341 };
342 0 ▶sub dateseq {
Subroutine dateseq: not called CC=35 SCAR=71.4
Pod documented
343 0 require DateTime::Duration;
344 0 require DateTime::Format::Strftimeq;
345  
346 0 my %args = @_;
347  
348 0 ▶ $args{from} //= DateTime->today;
Condition0%0/3
$args{"from"} //= "DateTime"->today
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"from"} //= "DateTime"->today
$args{"from"} "DateTime"->today
Truth table0%0/3
A //= B
A= $args{"from"}B= "DateTime"->today
ABresult
1X1
011
000
349 0 ▶ $args{increment} //= DateTime::Duration->new(days=>1);
Condition0%0/3
$args{"increment"} //= "DateTime::Duration"->new("days", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"increment"} //= "DateTime::Duration"->new("days", 1)
$args{"increment"} "DateTime::Duration"->new("days", 1)
Truth table0%0/3
A //= B
A= $args{"increment"}B= "DateTime::Duration"->new("days", 1)
ABresult
1X1
011
000
350 0 my $reverse = $args{reverse};
351  
352 0 my $random = $args{random};
353 return [412, "If you specify --random, you must also specify --from *and* --to"]
354 0 ▶ if $random && !$args{to};
Branch: 1 branch
BranchTrueFalseTotal
if $random and not $args{"to"} 0 0 0
Condition0%0/3
$random and not $args{"to"}
!ll&&!rl&&r
000
MC/DC0%0/2
$random and not $args{"to"}
$random not $args{"to"}
Truth table0%0/3
A and not(B)
A= $randomB= not $args{"to"}
ABresult
0X0
100
111
355  
356 0 my $formatter;
357 0 ▶ if (my $cl = $args{format_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cl = $args{'format_class'}) { } 0 0 0
358 0 $cl = "DateTime::Format::$cl";
359 0 (my $cl_pm = "$cl.pm") =~ s!::!/!g;
360 0 require $cl_pm;
361 0 ▶ my $attrs = $args{format_class_attrs} // {};
Condition0%0/2
$args{"format_class_attrs"} // {}
l!l
00
MC/DC0%0/1
$args{"format_class_attrs"} // {}
$args{"format_class_attrs"}
Truth table0%0/2
A
A= $args{"format_class_attrs"}
Aresult
11
00
362 0 $formatter = $cl->new(%$attrs);
363 } else {
364 0 ▶ my $strftime = $args{strftime} // do {
Condition0%0/3
$args{"strftime"} // do { my $has_hms; { if ($args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second) { ++$has_hms; last; } ; if (defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second) { ++$has_hms; last; } ; if ($args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds) { ++$has_hms; last; } ; } ; $has_hms ? "%Y-%m-%dT%H:%M:%S" : "%Y-%m-%d" }
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"strftime"} // do { my $has_hms; { if ($args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second) { ++$has_hms; last; } ; if (defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second) { ++$has_hms; last; } ; if ($args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds) { ++$has_hms; last; } ; } ; $has_hms ? "%Y-%m-%dT%H:%M:%S" : "%Y-%m-%d" }
$args{"strftime"} do { my $has_hms; { if ($args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second) { ++$has_hms; last; } ; if (defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second) { ++$has_hms; last; } ; if ($args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds) { ++$has_hms; last; } ; } ; $has_hms ? "%Y-%m-%dT%H:%M:%S" : "%Y-%m-%d" }
Truth table0%0/3
A // B
A= $args{"strftime"}B= do { my $has_hms; { if ($args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second) { ++$has_hms; last; } ; if (defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second) { ++$has_hms; last; } ; if ($args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds) { ++$has_hms; last; } ; } ; $has_hms ? "%Y-%m-%dT%H:%M:%S" : "%Y-%m-%d" }
ABresult
1X1
011
000
365 0 my $has_hms;
366 {
367 0 ▶ if ($args{from}->hour || $args{from}->minute || $args{from}->second) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second) 0 0 0
Condition0%0/6
$args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second
l!l&&r!l&&!r
000
$args{"from"}->hour or $args{"from"}->minute
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"from"}->hour or $args{"from"}->minute or $args{"from"}->second
$args{"from"}->hour $args{"from"}->minute $args{"from"}->second
Truth table0%0/4
A or B or C
A= $args{"from"}->hourB= $args{"from"}->minuteC= $args{"from"}->second
ABCresult
1XX1
01X1
0011
0000
368 0 $has_hms++; last;
369 }
370 0 ▶ if (defined($args{to}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second) 0 0 0
Condition0%0/9
defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second
!ll&&!rl&&r
000
$args{"to"}->hour or $args{"to"}->minute or $args{"to"}->second
l!l&&r!l&&!r
000
$args{"to"}->hour or $args{"to"}->minute
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $args{"to"} and $args{"to"}->hour || $args{"to"}->minute || $args{"to"}->second
defined $args{"to"} $args{"to"}->hour $args{"to"}->minute $args{"to"}->second
Truth table0%0/5
A and B or C or D
A= defined $args{"to"}B= $args{"to"}->hourC= $args{"to"}->minuteD= $args{"to"}->second
ABCDresult
0XXX0
10000
11XX1
101X1
10011
371 ($args{to}->hour || $args{to}->minute || $args{to}->second)) {
372 0 $has_hms++; last;
373 }
374 0 ▶ if ($args{increment}->hours || $args{increment}->minutes || $args{increment}->seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds) 0 0 0
Condition0%0/6
$args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds
l!l&&r!l&&!r
000
$args{"increment"}->hours or $args{"increment"}->minutes
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"increment"}->hours or $args{"increment"}->minutes or $args{"increment"}->seconds
$args{"increment"}->hours $args{"increment"}->minutes $args{"increment"}->seconds
Truth table0%0/4
A or B or C
A= $args{"increment"}->hoursB= $args{"increment"}->minutesC= $args{"increment"}->seconds
ABCresult
1XX1
01X1
0011
0000
375 0 $has_hms++; last;
376 }
377 }
378 0 ▶ $has_hms ? '%Y-%m-%dT%H:%M:%S' : '%Y-%m-%d';
Branch: 1 branch
BranchTrueFalseTotal
$has_hms ? : 0 0 0
379 };
380 0 $formatter = DateTime::Format::Strftimeq->new(
381 format => $strftime,
382 );
383 }
384  
385 0 my %seen_years; # key=year (e.g. 2021), val=int
386 my %seen_months; # key=year-mon (e.g. 2021-01), val=int
387 0 my $num_dates = 0;
388 0 ▶ my $code_filter = sub {
Subroutine __ANON__: not called CC=23 SCAR=63.1
389 0 my $dt = shift;
390 0 ▶ if (defined $args{business}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"business"}) 0 0 0
391 0 my $dow = $dt->day_of_week;
392 0 ▶ if ($args{business}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'business'}) { } 0 0 0
393 0 ▶ return 0 if $dow >= 6;
Branch: 1 branch
BranchTrueFalseTotal
if $dow >= 6 0 0 0
394 } else {
395 0 ▶ return 0 if $dow < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $dow < 6 0 0 0
396 }
397 }
398 0 ▶ if (defined $args{business6}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"business6"}) 0 0 0
399 0 my $dow = $dt->day_of_week;
400 0 ▶ if ($args{business6}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'business6'}) { } 0 0 0
401 0 ▶ return 0 if $dow >= 7;
Branch: 1 branch
BranchTrueFalseTotal
if $dow >= 7 0 0 0
402 } else {
403 0 ▶ return 0 if $dow < 7;
Branch: 1 branch
BranchTrueFalseTotal
if $dow < 7 0 0 0
404 }
405 }
406 0 ▶ if (defined $args{include_dow}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"include_dow"}) 0 0 0
407 0 my $dt_dow = $dt->day_of_week;
408 0 ▶ return 0 unless grep { $dt_dow == $_ } @{ $args{include_dow} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$dt_dow == $_;} @{$args{"include_dow"};} 0 0 0
409 }
410 0 ▶ if (defined $args{exclude_dow}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"exclude_dow"}) 0 0 0
411 0 my $dt_dow = $dt->day_of_week;
412 0 ▶ return 0 if grep { $dt_dow == $_ } @{ $args{exclude_dow} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$dt_dow == $_;} @{$args{"exclude_dow"};} 0 0 0
413 }
414 0 ▶ if (defined $args{include_month}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"include_month"}) 0 0 0
415 0 my $dt_mon = $dt->month;
416 0 ▶ return 0 unless grep { $dt_mon == $_ } @{ $args{include_month} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$dt_mon == $_;} @{$args{"include_month"};} 0 0 0
417 }
418 0 ▶ if (defined $args{exclude_dow}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"exclude_dow"}) 0 0 0
419 0 my $dt_mon = $dt->month;
420 0 ▶ return 0 if grep { $dt_mon == $_ } @{ $args{exclude_month} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$dt_mon == $_;} @{$args{"exclude_month"};} 0 0 0
421 }
422  
423 0 ▶ if ($args{_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"_filter"}) 0 0 0
424 0 ▶ return 0 unless $args{_filter}->($dt, \%args);
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_filter"}->($dt, \%args) 0 0 0
425 }
426  
427 0 ▶ if ($args{limit_yearly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"limit_yearly"}) 0 0 0
428 0 my $key = $dt->year;
429 0 ▶ return 0 if ++$seen_years{$key} > $args{limit_yearly};
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen_years{$key} > $args{"limit_yearly"} 0 0 0
430 }
431 0 ▶ if ($args{limit_monthly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"limit_monthly"}) 0 0 0
432 0 my $key = $dt->strftime("%Y-%m");
433 0 ▶ return 0 if ++$seen_months{$key} > $args{limit_monthly};
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen_months{$key} > $args{"limit_monthly"} 0 0 0
434 }
435  
436 0 1;
437 0 };
438  
439 0 my $_eval_code;
440 0 ▶ if ($args{eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"eval"}) 0 0 0
441 0 $_eval_code = eval "package main; sub { no strict; no warnings; $args{eval} }"; ## no critic: BuiltinFunctions::ProhibitStringyEval
442 0 ▶ die "Can't compile Perl code '$args{eval}': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
443 }
444  
445 0 ▶ my $_format = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
446 0 my $dt = shift;
447 0 ▶ if ($_eval_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_eval_code) { } 0 0 0
448 0 my $res;
449 {
450 0 local $_ = $dt;
451 0 $res = $_eval_code->();
452 0 ▶ $res = $_ unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
453 0 ▶ $res = $formatter->format_datetime($res) if blessed($res);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($res) 0 0 0
454 }
455 0 $res;
456 } else {
457 0 $formatter->format_datetime($dt);
458 }
459 0 };
460  
461 0 my $num_secs;
462 0 ▶ if ($random) {
Branch: 1 branch
BranchTrueFalseTotal
if ($random) 0 0 0
463 0 my $epoch_from = $args{from}->epoch;
464 0 my $epoch_to = $args{to}->epoch;
465 0 $num_secs = $epoch_to-$epoch_from;
466 }
467  
468 0 ▶ if ((defined $args{to} || defined $args{limit}) && !$random) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'to'} || defined $args{'limit'} and not $random) { } 0 0 0
Condition0%0/6
defined $args{"to"} || defined $args{"limit"} and not $random
!ll&&!rl&&r
000
defined $args{"to"} or defined $args{"limit"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $args{"to"} || defined $args{"limit"} and not $random
defined $args{"to"} defined $args{"limit"} not $random
Truth table0%0/5
A or B and not(C)
A= defined $args{"to"}B= defined $args{"limit"}C= not $random
ABCresult
00X0
1X00
0100
1X11
0111
469 0 my @res;
470 0 ▶ push @res, $args{header} if defined $args{header};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"header"} 0 0 0
471 0 my $dt = $args{from}->clone;
472 0 while (1) {
473 0 ▶ last if defined($args{limit}) && $num_dates >= $args{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"limit"} and $num_dates >= $args{"limit"} 0 0 0
Condition0%0/3
defined $args{"limit"} and $num_dates >= $args{"limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"limit"} and $num_dates >= $args{"limit"}
defined $args{"limit"} $num_dates >= $args{"limit"}
Truth table0%0/3
A and B
A= defined $args{"limit"}B= $num_dates >= $args{"limit"}
ABresult
0X0
100
111
474 #say "D:$dt vs $args{to}? ", DateTime->compare($dt, $args{to});
475 0 ▶ if (defined $args{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"to"}) 0 0 0
476 0 ▶ last if !$reverse && DateTime->compare($dt, $args{to}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $reverse and "DateTime"->compare($dt, $args{"to"}) > 0 0 0 0
Condition0%0/3
not $reverse and "DateTime"->compare($dt, $args{"to"}) > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $reverse and "DateTime"->compare($dt, $args{"to"}) > 0
not $reverse "DateTime"->compare($dt, $args{"to"}) > 0
Truth table0%0/3
not(A) and B
A= not $reverseB= "DateTime"->compare($dt, $args{"to"}) > 0
ABresult
0X0
100
111
477 0 ▶ last if $reverse && DateTime->compare($dt, $args{to}) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse and "DateTime"->compare($dt, $args{"to"}) < 0 0 0 0
Condition0%0/3
$reverse and "DateTime"->compare($dt, $args{"to"}) < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$reverse and "DateTime"->compare($dt, $args{"to"}) < 0
$reverse "DateTime"->compare($dt, $args{"to"}) < 0
Truth table0%0/3
A and B
A= $reverseB= "DateTime"->compare($dt, $args{"to"}) < 0
ABresult
0X0
100
111
478 }
479 0 ▶ if ($code_filter->($dt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code_filter($dt)) 0 0 0
480 0 push @res, $_format->($dt);
481 0 $num_dates++;
482 }
483 0 ▶ $dt = $reverse ? $dt - $args{increment} : $dt + $args{increment};
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
484 }
485 0 return [200, "OK", \@res];
486 } else {
487 # stream
488 # --random always goes here
489 0 my $dt = $args{from}->clone;
490 0 my $has_printed_header;
491 0 ▶ my $func = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
492 0 ▶ return $args{header} if defined $args{header} && !$has_printed_header++;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"header"} and not $has_printed_header++ 0 0 0
Condition0%0/3
defined $args{"header"} and not $has_printed_header++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"header"} and not $has_printed_header++
defined $args{"header"} not $has_printed_header++
Truth table0%0/3
A and not(B)
A= defined $args{"header"}B= not $has_printed_header++
ABresult
0X0
100
111
493 0 ▶ return if defined $args{limit} && $num_dates++ >= $args{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"limit"} and $num_dates++ >= $args{"limit"} 0 0 0
Condition0%0/3
defined $args{"limit"} and $num_dates++ >= $args{"limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"limit"} and $num_dates++ >= $args{"limit"}
defined $args{"limit"} $num_dates++ >= $args{"limit"}
Truth table0%0/3
A and B
A= defined $args{"limit"}B= $num_dates++ >= $args{"limit"}
ABresult
0X0
100
111
494 REPEAT:
495 0 ▶ if ($random) {
Branch: 1 branch
BranchTrueFalseTotal
if ($random) 0 0 0
496 0 $dt = $args{from}->clone->add(seconds => $num_secs * rand());
497 }
498 0 ▶ goto REPEAT unless $code_filter->($dt);
Branch: 1 branch
BranchTrueFalseTotal
unless &$code_filter($dt) 0 0 0
499 0 my $res = $_format->($dt);
500  
501 0 ▶ if ($random) {
Branch: 1 branch
BranchTrueFalseTotal
if ($random) { } 0 0 0
502 } else {
503 $dt = $reverse ?
504 0 ▶ $dt - $args{increment} : $dt + $args{increment};
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
505 }
506 0 $res;
507 0 };
508 0 return [200, "OK", $func, {schema=>'str*', stream=>1}];
509 }
510 }
511  
512 1 1;
513 # ABSTRACT: Generate a sequence of dates
514  
515 __END__
↑ Summary