Natural/Calc.pm

filter: Statementstmt 99.1% 230 / 232 Branchbran 76.9% 40 / 52 Conditioncond 52.6% 10 / 19 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 38 / 38 Podpod n/a totaltotal 91.4% 320 / 350 CC 34 SCAR 10.6
CC
34
Cov
92%
_count_weekday_variant_month
10.0
_first_last_day_unit
4.0
_daytime
3.0
CRAP 2.9
« Natural/Utils.pm ↑ Summary Natural/Wrappers.pm »
1 package DateTime::Format::Natural::Calc;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 DateTime::Format::Natural::Compat
7 DateTime::Format::Natural::Utils
8 DateTime::Format::Natural::Wrappers
9 27 );
10  
11 27 ▶use constant MORNING => '08';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use constant AFTERNOON => '14';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use constant EVENING => '20';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 our $VERSION = '1.50';
16  
17 my $multiply_by = sub
18 3235 ▶{
Subroutine __ANON__: called CC=2 SCAR=6.9
19 3235 my ($value, $opts) = @_;
20 3235 ▶ return $value * $opts->{multiply_by} if exists $opts->{multiply_by};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"multiply_by"} 138 3097 3235
21 3097 return $value;
22 27 };
23  
24 sub _no_op
25 92 ▶{
Subroutine _no_op: called CC=1 SCAR=0.0
26 92 my $self = shift;
27 92 $self->_register_trace;
28 92 my $opts = pop;
29 }
30  
31 sub _ago_variant
32 497 ▶{
Subroutine _ago_variant: called CC=1 SCAR=0.0
33 497 my $self = shift;
34 497 $self->_register_trace;
35 497 my $opts = pop;
36 497 $self->_subtract($opts->{unit} => $multiply_by->(shift, $opts));
37 }
38  
39 sub _now_variant
40 108 ▶{
Subroutine _now_variant: called CC=1 SCAR=0.0
41 108 my $self = shift;
42 108 $self->_register_trace;
43 108 my $opts = pop;
44 108 my ($value, $when) = @_;
45 $self->_add_or_subtract({
46 when => $when,
47 unit => $opts->{unit},
48 108 value => $multiply_by->($value, $opts),
49 });
50 }
51  
52 sub _daytime_variant
53 409 ▶{
Subroutine _daytime_variant: called CC=3 SCAR=11.0
54 409 my $self = shift;
55 409 $self->_register_trace;
56 409 my $opts = pop;
57 409 my ($daytime) = @_;
58 409 my %lookup = (
59 0 => 'morning',
60 1 => 'afternoon',
61 2 => 'evening',
62 );
63 409 $daytime = $lookup{$daytime};
64 409 my %daytimes = (
65 morning => MORNING,
66 afternoon => AFTERNOON,
67 evening => EVENING,
68 );
69 my $hour = exists $self->{Daytime}{$daytime}
70 ? $self->{Daytime}{$daytime}
71 409 ▶ : $daytimes{$daytime};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'Daytime'}{$daytime} ? : 19 390 409
72 409 ▶ if ($self->_valid_time(hour => $hour)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_time("hour", $hour)) 409 0 409
73 409 $self->_set(hour => $hour);
74 }
75 }
76  
77 sub _daytime
78 390 ▶{
Subroutine _daytime: called CC=3 SCAR=11.0
79 390 my $self = shift;
80 390 $self->_register_trace;
81 390 my $opts = pop;
82 390 my ($hour) = @_;
83 390 ▶ $hour += $opts->{hours} || 0;
Condition100%2/2
$opts->{'hours'} || 0
l!l
36354
MC/DC100%1/1
$opts->{'hours'} || 0
$opts->{'hours'}
Truth table100%2/2
A
A= $opts->{'hours'}
Aresult
11
00
84 390 ▶ if ($self->_valid_time(hour => $hour)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_time("hour", $hour)) 390 0 390
85 390 $self->_set(hour => $hour);
86 }
87 }
88  
89 sub _hourtime_variant
90 96 ▶{
Subroutine _hourtime_variant: called CC=3 SCAR=11.0
91 96 my $self = shift;
92 96 $self->_register_trace;
93 96 my $opts = pop;
94 96 my ($value, $when) = @_;
95 96 ▶ my $hours = $opts->{hours} || 0;
Condition100%2/2
$opts->{'hours'} || 0
l!l
4848
MC/DC100%1/1
$opts->{'hours'} || 0
$opts->{'hours'}
Truth table100%2/2
A
A= $opts->{'hours'}
Aresult
11
00
96 96 ▶ if ($self->_valid_time(hour => $hours)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_time("hour", $hours)) 96 0 96
97 96 $self->_set(hour => $hours);
98 96 $self->{datetime}->set(minute => 0, second => 0, nanosecond => 0);
99 $self->_add_or_subtract({
100 when => $when,
101 unit => $opts->{unit},
102 96 value => $multiply_by->($value, $opts),
103 });
104 }
105 }
106  
107 sub _month_day
108 2660 ▶{
Subroutine _month_day: called CC=2 SCAR=7.0
109 2660 my $self = shift;
110 2660 $self->_register_trace;
111 2660 my $opts = pop;
112 2660 my ($day, $month) = @_;
113 2660 ▶ if ($self->_valid_date(month => $month, day => $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date("month", $month, "day", $day)) 2660 0 2660
114 2660 $self->_set(
115 month => $month,
116 day => $day,
117 );
118 }
119 }
120  
121 sub _unit_date
122 519 ▶{
Subroutine _unit_date: called CC=3 SCAR=11.0
123 519 my $self = shift;
124 519 $self->_register_trace;
125 519 my $opts = pop;
126 519 my ($value) = @_;
127 519 ▶ $self->{datetime}->set(day => 1) if $opts->{unit} eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"unit"} eq "month" 85 434 519
128 519 ▶ if ($self->_valid_date($opts->{unit} => $value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date($opts->{"unit"}, $value)) 519 0 519
129 519 $self->_set($opts->{unit} => $value);
130 }
131 }
132  
133 sub _weekday
134 2319 ▶{
Subroutine _weekday: called CC=2 SCAR=6.9
135 2319 my $self = shift;
136 2319 $self->_register_trace;
137 2319 my $opts = pop;
138 2319 my ($day) = @_;
139 2319 ▶ if ($day > $self->{datetime}->wday) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day > $self->{'datetime'}->wday) { } 748 1571 2319
140 748 $self->_add(day => ($day - $self->{datetime}->wday));
141 }
142 else {
143 1571 $self->_subtract(day => ($self->{datetime}->wday - $day));
144 }
145 }
146  
147 sub _count_day_variant_week
148 1116 ▶{
Subroutine _count_day_variant_week: called CC=2 SCAR=6.9
149 1116 my $self = shift;
150 1116 $self->_register_trace;
151 1116 my $opts = pop;
152 1116 my ($when, $day) = @_;
153 my %days = (
154 -1 => ($self->{datetime}->wday + (7 - $day)),
155 0 => ($day - $self->{datetime}->wday),
156 1116 1 => (7 - $self->{datetime}->wday + $day),
157 );
158 $self->_add_or_subtract({
159 when => ($when == 0) ? 1 : $when,
160 unit => 'day',
161 1116 ▶ value => $days{$when},
Branch: 1 branch
BranchTrueFalseTotal
$when == 0 ? : 345 771 1116
162 });
163 }
164  
165 sub _count_day_variant_month
166 54 ▶{
Subroutine _count_day_variant_month: called CC=2 SCAR=7.0
167 54 my $self = shift;
168 54 $self->_register_trace;
169 54 my $opts = pop;
170 54 my ($when, $day) = @_;
171 54 ▶ if ($self->_valid_date(day => $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date("day", $day)) 54 0 54
172 54 $self->_add(month => $when);
173 54 $self->_set(day => $day);
174 }
175 }
176  
177 sub _unit_variant
178 2254 ▶{
Subroutine _unit_variant: called CC=1 SCAR=0.0
179 2254 my $self = shift;
180 2254 $self->_register_trace;
181 2254 my $opts = pop;
182 2254 my ($when) = @_;
183 $self->_add_or_subtract({
184 when => $when,
185 unit => $opts->{unit},
186 2254 value => $multiply_by->(1, $opts),
187 });
188 }
189  
190 sub _count_month_variant_year
191 54 ▶{
Subroutine _count_month_variant_year: called CC=2 SCAR=7.0
192 54 my $self = shift;
193 54 $self->_register_trace;
194 54 my $opts = pop;
195 54 my ($when, $month) = @_;
196 54 ▶ if ($self->_valid_date(month => $month)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date("month", $month)) 54 0 54
197 54 $self->_add(year => $when);
198 54 $self->_set(month => $month);
199 }
200 }
201  
202 sub _in_count_variant
203 136 ▶{
Subroutine _in_count_variant: called CC=1 SCAR=0.0
204 136 my $self = shift;
205 136 $self->_register_trace;
206 136 my $opts = pop;
207 136 $self->_add_or_subtract($opts->{unit} => $multiply_by->(shift, $opts));
208 }
209  
210 sub _month_variant
211 108 ▶{
Subroutine _month_variant: called CC=2 SCAR=7.0
212 108 my $self = shift;
213 108 $self->_register_trace;
214 108 my $opts = pop;
215 108 my ($when, $month) = @_;
216 108 ▶ if ($self->_valid_date(month => $month)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date("month", $month)) 108 0 108
217 108 $self->_add(year => $when);
218 108 $self->_set(month => $month);
219 }
220 }
221  
222 sub _count_weekday_variant_month
223 168 ▶{
Subroutine _count_weekday_variant_month: called CC=7 SCAR=23.0
224 168 my $self = shift;
225 168 $self->_register_trace;
226 168 my $opts = pop;
227 168 my ($when, $count, $day, $month) = @_;
228 168 my $year;
229 168 local $@;
230 168 eval {
231 ($year, $month, $day) =
232 $self->_Nth_Weekday_of_Month_Year(
233 $self->{datetime}->year + $when,
234 defined $month
235 ? $month
236 : $self->{datetime}->month,
237 168 ▶ $day,
Branch: 1 branch
BranchTrueFalseTotal
defined $month ? : 90 78 168
238 $count,
239 );
240 };
241 168 ▶ if (!$@
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and (defined $year and defined $month and defined $day) and $self->_check_date($year, $month, $day)) { } 168 0 168
Condition33%4/12
not $@ and (defined $year and defined $month and defined $day) and $self->_check_date($year, $month, $day)
!ll&&!rl&&r
00168
not $@ and (defined $year and defined $month and defined $day)
!ll&&!rl&&r
00168
defined $year and defined $month and defined $day
!ll&&!rl&&r
00168
defined $year and defined $month
!ll&&!rl&&r
00168
MC/DC0%0/5
not $@ and (defined $year and defined $month and defined $day) and $self->_check_date($year, $month, $day)
not $@ defined $year defined $month defined $day $self->_check_date($year, $month, $day)
Truth table16%1/6
not(A) and B and C and D and E
A= not $@B= defined $yearC= defined $monthD= defined $dayE= $self->_check_date($year, $month, $day)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
242 and defined $year && defined $month && defined $day
243 and $self->_check_date($year, $month, $day)
244 ) {
245 168 $self->_set(
246 year => $year,
247 month => $month,
248 day => $day,
249 );
250 }
251 else {
252 0 $self->_set_failure;
253 0 $self->_set_error("(date is not valid)");
254 }
255 }
256  
257 sub _daytime_unit_variant
258 144 ▶{
Subroutine _daytime_unit_variant: called CC=1 SCAR=0.0
259 144 my $self = shift;
260 144 $self->_register_trace;
261 144 my $opts = pop;
262 144 my ($value, $when, $days) = @_;
263 144 $self->_add(day => $days);
264 144 $self->{datetime}->set(hour => 0, minute => 0, second => 0, nanosecond => 0);
265 $self->_add_or_subtract({
266 when => $when,
267 unit => $opts->{unit},
268 144 value => $multiply_by->($value, $opts),
269 });
270 }
271  
272 # wrapper for <time> AM/PM
273 sub _at
274 6487 ▶{
Subroutine _at: called CC=1 SCAR=0.0
275 6487 my $self = shift;
276 6487 $self->_register_trace;
277 6487 $self->_at_time(@_);
278 }
279  
280 # wrapper for <time>
281 sub _time
282 1835 ▶{
Subroutine _time: called CC=1 SCAR=0.0
283 1835 my $self = shift;
284 1835 $self->_register_trace;
285 1835 $self->_at_time(@_);
286 }
287  
288 sub _at_time
289 8322 ▶{
Subroutine _at_time: called CC=3 SCAR=11.0
290 8322 my $self = shift;
291 8322 my $opts = pop;
292 8322 my ($time) = @_;
293 8322 my @units = qw(hour minute second nanosecond);
294 8322 my %values = map { shift @units => $_ } split /[:\.]/, $time;
295 8322 ▶ $values{nanosecond} *= 1_000_000 if exists $values{nanosecond}; # milli to nano
Branch: 1 branch
BranchTrueFalseTotal
if exists $values{"nanosecond"} 54 8268 8322
296 8322 ▶ if ($self->_valid_time(%values)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_time(%values)) 8322 0 8322
297 8322 $self->_set(%values);
298 }
299 }
300  
301 sub _count_yearday_variant_year
302 132 ▶{
Subroutine _count_yearday_variant_year: called CC=1 SCAR=0.0
303 132 my $self = shift;
304 132 $self->_register_trace;
305 132 my $opts = pop;
306 132 my ($day, $when) = @_;
307 132 my ($year, $month);
308 132 ($year, $month, $day) = $self->_Add_Delta_Days($self->{datetime}->year, $day);
309 132 $self->_set(
310 year => $year + $when,
311 month => $month,
312 day => $day,
313 );
314 }
315  
316 sub _count_weekday
317 78 ▶{
Subroutine _count_weekday: called CC=1 SCAR=0.0
318 78 my $self = shift;
319 78 $self->_count_weekday_variant_month(0, @_[0,1], undef, $_[-1]);
320 }
321  
322 sub _day_month_year
323 27 ▶{
Subroutine _day_month_year: called CC=2 SCAR=7.0
324 27 my $self = shift;
325 27 $self->_register_trace;
326 27 my $opts = pop;
327 27 my ($day, $month, $year) = @_;
328 27 ▶ if ($self->_valid_date(year => $year, month => $month, day => $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_date("year", $year, "month", $month, "day", $day)) 27 0 27
329 27 $self->_set(
330 year => $year,
331 month => $month,
332 day => $day,
333 );
334 }
335 }
336  
337 sub _count_weekday_from_now
338 42 ▶{
Subroutine _count_weekday_from_now: called CC=2 SCAR=7.0
339 42 my $self = shift;
340 42 $self->_register_trace;
341 42 my $opts = pop;
342 42 my ($count, $day) = @_;
343 42 my $wday = $self->{datetime}->wday;
344 42 ▶ $self->_add(day => ($count - 1) * 7 +
Branch: 1 branch
BranchTrueFalseTotal
$wday < $day ? : 0 42 42
345 (($wday < $day)
346 ? $day - $wday
347 : (7 - $wday) + $day)
348 );
349 }
350  
351 sub _final_weekday_in_month
352 60 ▶{
Subroutine _final_weekday_in_month: called CC=2 SCAR=6.9
353 60 my $self = shift;
354 60 $self->_register_trace;
355 60 my $opts = pop;
356 60 my ($wday, $month) = @_;
357 60 my $days = $self->_Days_in_Month($self->{datetime}->year, $month);
358 60 my ($year, $day);
359 ($year, $month, $day) = $self->_Nth_Weekday_of_Month_Year(
360 $self->{datetime}->year,
361 60 $month,
362 $wday,
363 1,
364 );
365 60 ▶ while ($day <= $days - 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day <= $days - 7) 186 60 246
366 186 $day += 7;
367 }
368 $self->_set(
369 60 year => $year,
370 month => $month,
371 day => $day,
372 );
373 }
374  
375 sub _first_last_day_unit
376 78 ▶{
Subroutine _first_last_day_unit: called CC=4 SCAR=13.9
377 78 my $self = shift;
378 78 $self->_register_trace;
379 78 my $opts = pop;
380 78 my ($year, $month, $day) = do {
381 78 ▶ @_ >= 3 ? @_ : (undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 3 ? : 30 48 78
382 };
383 78 ▶ $year ||= $self->{datetime}->year;
Condition66%2/3
$year ||= $self->{"datetime"}->year
l!l&&r!l&&!r
30480
MC/DC0%0/2
$year ||= $self->{"datetime"}->year
$year $self->{"datetime"}->year
Truth table66%2/3
A ||= B
A= $yearB= $self->{"datetime"}->year
ABresult
1X1
011
000
384 78 ▶ unless (defined $day) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $day) 39 39 78
385 39 $day = $self->_Days_in_Month($year, $month);
386 }
387 $self->_set(
388 78 year => $year,
389 month => $month,
390 day => $day,
391 );
392 }
393  
394 sub _variant_last_month
395 12 ▶{
Subroutine _variant_last_month: called CC=2 SCAR=6.9
396 12 my $self = shift;
397 12 $self->_register_trace;
398 12 my $opts = pop;
399 12 my ($day) = @_;
400 12 $self->_subtract(month => 1);
401 12 ▶ unless (defined $day) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $day) 6 6 12
402 6 $day = $self->_Days_in_Month($self->{datetime}->year, $self->{datetime}->month);
403 }
404 12 $self->_set(day => $day);
405 }
406  
407 sub _variant_quarter
408 18 ▶{
Subroutine _variant_quarter: called CC=1 SCAR=0.0
409 18 my $self = shift;
410 18 $self->_register_trace;
411 18 my $opts = pop;
412 18 my ($when) = @_;
413 18 $self->_subtract(day => $self->{datetime}->day_of_quarter - 1);
414 $self->_add_or_subtract({
415 when => $when,
416 unit => $opts->{unit},
417 18 value => 3,
418 });
419 }
420  
421 sub _begin_end_month
422 36 ▶{
Subroutine _begin_end_month: called CC=2 SCAR=6.9
423 36 my $self = shift;
424 36 $self->_register_trace;
425 36 my $opts = pop;
426 36 my ($day) = @_;
427 36 ▶ unless (defined $day) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $day) 18 18 36
428 18 $day = $self->_Days_in_Month($self->{datetime}->year, $self->{datetime}->month);
429 }
430 36 $self->_set(day => $day);
431 }
432  
433 sub _christmas_new_year
434 72 ▶{
Subroutine _christmas_new_year: called CC=2 SCAR=6.9
435 72 my $self = shift;
436 72 $self->_register_trace;
437 72 my $opts = pop;
438 72 my ($when) = @_;
439 72 my %holidays = %{$self->{data}->{holidays}};
440 72 my $calendar = DateTime::Format::Natural::Calendar::_init($self->{Calendar_class});
441 72 my $year = $self->{datetime}->year;
442 72 my ($month, $day) = @{$holidays{$calendar->{type}}{$opts->{type}}}{qw(month day)};
443 72 $self->{datetime}->set(hour => 0, minute => 0, second => 0, nanosecond => 0);
444 my $code = sub
445 72 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
446 72 my ($year, $month, $day, $opts) = @_;
447 72 ▶ $$year++ if $opts->{type} eq 'new_year';
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"type"} eq "new_year" 48 24 72
448 72 };
449 72 ▶ if ($calendar->_can_convert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calendar->_can_convert) { } 36 36 72
450 36 ($year, $month, $day) = $calendar->_to_gregorian($year, $month, $day, $opts, $code);
451 }
452 else {
453 36 $code->(\$year, \$month, \$day, $opts);
454 }
455 72 $self->_set(
456 year => $year,
457 month => $month,
458 day => $day,
459 );
460 72 $self->_add_or_subtract({
461 when => $when,
462 unit => 'hour',
463 value => 4,
464 });
465 }
466  
467 27 1;
468 __END__
« Natural/Utils.pm ↑ Summary Natural/Wrappers.pm »