blib/lib/DateTimeX/Fiscal/Fiscal5253.pm

filter: Statementstmt 93.8% 244 / 260 Branchbran 83.9% 99 / 118 Conditioncond 71.1% 42 / 59 MC/DCmcdc 43.2% 16 / 37 Subroutinesub 97.2% 35 / 36 Podpod 83.3% 5 / 6 totaltotal 85.4% 441 / 516 CC 86 SCAR 20.7
CC
86
Cov
88%
_build_periods
19.2
__ANON__
15.2
contains
11.1
CRAP 7.9
↑ Summary
1 package DateTimeX::Fiscal::Fiscal5253;
2  
3 6 our $VERSION = '2.01';
4  
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 my @periodmonths = (
13 qw(
14 January
15 February
16 March
17 April
18 May
19 June
20 July
21 August
22 September
23 October
24 November
25 December
26 )
27 );
28  
29 # Figure out if the epoch is a 32- or 64-bit value and use DT if needed
30 161 ▶my $_use_dt = sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
31 161 my $year = shift;
32  
33 # test for 32- or 64-bit time values. This is in an eval because there
34 # is apparently a bug in Perl 5.10.0 on a Win32 v5 build that causes
35 # gmtime to return undefs when the epoch rolls over. This of course
36 # will throw an uninitialized error with "use warnings FATAL => 'all'"
37 # in effect.
38 161 my $is_32 = eval {
39 161 my @tdata = gmtime(2147483651); # This is 4 sec past the rollover
40 161 return ( $tdata[5] == 138 );
41 };
42  
43 161 ▶ return $is_32 && ( $year < 1903 || $year > 2037 );
Condition33%2/6
$is_32 && ($year < 1903 || $year > 2037)
!ll&&!rl&&r
01610
$year < 1903 or $year > 2037
l!l&&r!l&&!r
00161
MC/DC0%0/3
$is_32 && ($year < 1903 || $year > 2037)
$is_32 $year < 1903 $year > 2037
Truth table25%1/4
A && B or C
A= $is_32B= $year < 1903C= $year > 2037
ABCresult
0XX0
1000
11X1
1011
44 6 };
45  
46 # Utility function to convert a date string to a DT object
47 649 ▶my $_str2dt = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
48 649 my $date = shift;
49  
50 649 ▶ return $date if ref($date);
Branch: 1 branch
BranchTrueFalseTotal
if ref $date 369 280 649
51  
52 # convert date param to DT object
53 280 my ( $y, $m, $d );
54 280 ▶ if ( $date =~ m{^(\d{4})-(\d{1,2})-(\d{1,2})(?:$|\D+)} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($date =~ /^(\d{4})-(\d{1,2})-(\d{1,2})(?:$|\D+)/) { } 260 20 280
elsif ($date =~ m[^(\d{1,2})/(\d{1,2})/(\d{4})(?:$|\D+)]) { } 8 12 20
55 260 $y = $1, $m = $2, $d = $3;
56 }
57 elsif ( $date =~ m{^(\d{1,2})/(\d{1,2})/(\d{4})(?:$|\D+)} ) {
58 8 $y = $3, $m = $1, $d = $2;
59 }
60 else {
61 12 croak "Unable to parse date string: $date";
62 }
63 268 ▶ eval { $date = DateTime->new( year => $y, month => $m, day => $d ); }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $date = "DateTime"->new("year", $y, "month", $m, "day", $d) } } or croak("Invalid date: $date") 264 4 268
64 or croak "Invalid date: $date";
65  
66 264 return $date;
67 6 };
68  
69 # Utility function to validate values supplied as a calendar style.
70 847 ▶my $_valid_cal_style = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
71 847 ▶ my $style = shift || 'fiscal';
Condition100%2/2
shift() || 'fiscal'
l!l
8461
MC/DC100%1/1
shift() || 'fiscal'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
72  
73 847 $style =~ tr/A-Z/a-z/;
74 847 ▶ croak "Invalid calendar style specified: $style"
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /^(fiscal|restated|truncated)$/ 11 836 847
75 unless $style =~ /^(fiscal|restated|truncated)$/;
76  
77 836 return $style;
78 6 };
79  
80 # Define attributes and psuedo-attributes
81 has end_month => (
82 is => 'ro',
83 175 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 175 ▶ croak "Invalid value for param end_month: $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^(?:1[0-2]|[1-9])\z/ 10 165 175
85 unless $_[0] =~ /^(?:1[0-2]|[1-9])\z/;
86 },
87 6 default => 12,
88 );
89  
90 has end_dow => (
91 is => 'ro',
92 187 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
93 187 ▶ croak "Invalid value for param end_dow: $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[1-7]\z/ 12 175 187
94 unless $_[0] =~ /^[1-7]\z/;
95 },
96 6 default => 6,
97 );
98  
99 has end_type => (
100 is => 'ro',
101 165 ▶ coerce => sub { my $tmp = $_[0]; $tmp =~ tr[A-Z][a-z]; return $tmp; },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 165 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
103 165 ▶ croak "Invalid value for param end_type: $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^(?:last|closest)$/ 2 163 165
104 unless $_[0] =~ /^(?:last|closest)$/;
105 },
106 6 default => 'last',
107 );
108  
109 has leap_period => (
110 is => 'ro',
111 163 ▶ coerce => sub { my $tmp = $_[0]; $tmp =~ tr[A-Z][a-z]; return $tmp; },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 163 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
113 163 ▶ croak "Invalid value for param leap_period: $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^(?:first|last)$/ 2 161 163
114 unless $_[0] =~ /^(?:first|last)$/;
115 },
116 6 default => 'last',
117 );
118  
119 6 has year => (
120 is => 'rwp',
121 builder => 1,
122 lazy => 1,
123 );
124  
125 has _date => (
126 is => 'rw',
127 init_arg => 'date',
128 coerce => $_str2dt,
129 67 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
130 67 ▶ croak 'Object in "date" parameter is not a member of DateTime'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->isa("DateTime") 2 65 67
131 unless $_[0]->isa('DateTime');
132 },
133 6 );
134  
135 6 has _start_ymd => (
136 is => 'rw',
137 init_arg => undef,
138 reader => 'start',
139 );
140  
141 6 has _end_ymd => (
142 is => 'rw',
143 init_arg => undef,
144 reader => 'end',
145 );
146  
147 6 has _weeks => (
148 is => 'rw',
149 init_arg => undef,
150 reader => 'weeks',
151 );
152  
153 has style => (
154 is => 'rw',
155 init_arg => undef,
156 168 ▶ coerce => sub { my $tmp = $_[0]; $tmp =~ tr[A-Z][a-z]; return $tmp; },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 6 isa => $_valid_cal_style,
158 default => 'fiscal',
159 );
160  
161 209 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
162 209 my $orig = shift;
163 209 my $class = shift;
164 209 my %args = @_;
165  
166 # which one would be correct?
167 croak 'Mutually exclusive parameters "year" and "date" are present'
168 209 ▶ if $args{year} && $args{date};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"year"} and $args{"date"} 2 207 209
Condition100%3/3
$args{"year"} and $args{"date"}
!ll&&!rl&&r
851222
MC/DC100%2/2
$args{"year"} and $args{"date"}
$args{"year"} $args{"date"}
Truth table100%3/3
A and B
A= $args{"year"}B= $args{"date"}
ABresult
0X0
100
111
169  
170 # clone the DT object if present
171 $args{date} = $args{date}->clone
172 207 ▶ if ref( $args{date} ) && $args{date}->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"date"} and $args{"date"}->isa("DateTime") 2 205 207
Condition100%3/3
ref $args{"date"} and $args{"date"}->isa("DateTime")
!ll&&!rl&&r
20322
MC/DC100%2/2
ref $args{"date"} and $args{"date"}->isa("DateTime")
ref $args{"date"} $args{"date"}->isa("DateTime")
Truth table100%3/3
A and B
A= ref $args{"date"}B= $args{"date"}->isa("DateTime")
ABresult
0X0
100
111
173  
174 # supply a default date if needed
175 207 ▶ $args{date} = DateTime->today() unless $args{date} || $args{year};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"date"} or $args{"year"} 51 156 207
Condition100%3/3
$args{"date"} or $args{"year"}
l!l&&r!l&&!r
3412251
MC/DC100%2/2
$args{"date"} or $args{"year"}
$args{"date"} $args{"year"}
Truth table100%3/3
A or B
A= $args{"date"}B= $args{"year"}
ABresult
1X1
011
000
176  
177 207 return $class->$orig(%args);
178 6 };
179  
180 # One of the few cases where modifying "new" is the right thing to do.
181 # NOTE! testing reveals that this B<must not> come before "BUILDARGS".
182 210 ▶before new => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
183 210 ▶ croak 'Must be called as a class method only' if ref( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 1 209 210
184 6 };
185  
186 # Initialize the internal structures now that we know our params are good.
187 161 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
188 161 my $self = shift;
189  
190 161 $self->{_fiscal} = undef;
191 161 $self->{_restated} = undef;
192 161 $self->{_truncated} = undef;
193  
194 161 $self->{_start} = $self->_start5253;
195 161 $self->{_start_ymd} = $self->{_start}->ymd;
196 161 $self->{_end} = $self->_end5253;
197 161 $self->{_end_ymd} = $self->{_end}->ymd;
198  
199 $self->{_weeks} =
200 161 ▶ $self->{_start}->clone->add( days => 367 ) > $self->{_end} ? 52 : 53;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_start'}->clone->add('days', 367) > $self->{'_end'} ? : 135 26 161
201  
202 161 $self->_build_weeks;
203 161 $self->_build_periods('fiscal');
204  
205 161 ▶ if ( $self->{_weeks} == 53 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_weeks"} == 53) 26 135 161
206 26 $self->_build_periods('restated');
207 26 $self->_build_periods('truncated');
208 }
209  
210 161 return;
211 }
212  
213 39 ▶sub _build_year {
Subroutine _build_year: called CC=1 SCAR=0.0
214 39 my $self = shift;
215  
216 # we *should be* guaranteed that _date contains a DateTime object
217 # if this is reached because no value was supplied for 'year'.
218 39 $self->{_date}->truncate( to => 'day' )->set_time_zone('floating');
219  
220 39 return $self->_find5253;
221 }
222  
223 # Build the week array once, then manipulate as needed.
224 # Using epoch math is more than an order of magnitude faster
225 # than DT, but the size of the epoch value must be tested.
226 161 ▶sub _build_weeks {
Subroutine _build_weeks: called CC=6 SCAR=20.3
227 161 my $self = shift;
228  
229 161 my $weeks = {};
230 161 ▶ if ( &{$_use_dt}( $self->{year} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&{$_use_dt;}($self->{'year'})) { } 0 161 161
231 0 my $wstart = $self->{_start}->clone;
232 0 my $wend = $self->{_start}->clone->add( days => 6 );
233  
234 0 for ( 1 .. $self->{_weeks} ) {
235 0 $weeks->{$_} = {
236 start => $wstart->ymd,
237 end => $wend->ymd,
238 };
239  
240 # skip the last step so the ending values are preserved
241 # if needed for something else in the future.
242 0 ▶ last if $_ == $self->{_weeks};
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $self->{"_weeks"} 0 0 0
243  
244 0 $wstart->add( days => 7 );
245 0 $wend->add( days => 7 );
246 }
247 }
248 else {
249 161 my $daysecs = ( 60 * 60 * 24 );
250 161 my $weeksecs = $daysecs * 7;
251  
252 161 my $wstart = $self->{_start}->epoch + ( $daysecs / 2 );
253 161 my $wend = $wstart + ( $daysecs * 6 );
254  
255 161 for ( 1 .. $self->{_weeks} ) {
256 8398 $weeks->{$_} = {
257 start => strftime( '%Y-%m-%d', localtime($wstart) ),
258 end => strftime( '%Y-%m-%d', localtime($wend) ),
259 };
260  
261 # skip the last step so the ending values are preserved
262 # if needed for something else in the future.
263 8398 ▶ last if $_ == $self->{_weeks};
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $self->{"_weeks"} 161 8237 8398
264  
265 8237 $wstart += $weeksecs;
266 8237 $wend += $weeksecs;
267 }
268 }
269  
270 161 $self->{_weeks_raw} = $weeks;
271  
272 161 return;
273 }
274  
275 # Build the basic calendar structures as needed.
276 213 ▶sub _build_periods {
Subroutine _build_periods: called CC=19 SCAR=29.5
277 213 my $self = shift;
278 213 ▶ my $style = shift || $self->{style};
Condition33%1/3
shift() || $self->{'style'}
l!l&&r!l&&!r
21300
MC/DC0%0/2
shift() || $self->{'style'}
shift() $self->{'style'}
Truth table33%1/3
A || B
A= shift()B= $self->{'style'}
ABresult
1X1
011
000
279  
280 # not strictly needed, but makes for easier to read code
281 213 ▶ my $restate = $style eq 'restated' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$style eq 'restated' ? : 26 187 213
282 213 ▶ my $truncate = $style eq 'truncated' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$style eq 'truncated' ? : 26 187 213
283  
284 # Avoid re-builds when possible.
285 213 ▶ return if $restate && defined( $self->{_restated} );
Branch: 1 branch
BranchTrueFalseTotal
if $restate and defined $self->{"_restated"} 0 213 213
Condition66%2/3
$restate and defined $self->{"_restated"}
!ll&&!rl&&r
187260
MC/DC0%0/2
$restate and defined $self->{"_restated"}
$restate defined $self->{"_restated"}
Truth table66%2/3
A and B
A= $restateB= defined $self->{"_restated"}
ABresult
0X0
100
111
286 213 ▶ return if $truncate && defined( $self->{_truncated} );
Branch: 1 branch
BranchTrueFalseTotal
if $truncate and defined $self->{"_truncated"} 0 213 213
Condition66%2/3
$truncate and defined $self->{"_truncated"}
!ll&&!rl&&r
187260
MC/DC0%0/2
$truncate and defined $self->{"_truncated"}
$truncate defined $self->{"_truncated"}
Truth table66%2/3
A and B
A= $truncateB= defined $self->{"_truncated"}
ABresult
0X0
100
111
287  
288 # Disabled this for now, becomes problematic for various
289 # methods such as "contains" in normal years.
290 # return if ($restate || $truncate) && $self->{_weeks} == 52;
291  
292 213 my $pstart = $self->{_start}->clone;
293  
294 # This value is confusing only because it is 0-based unlike
295 # the other month values.
296 213 ▶ my $p1month = $self->{end_month} == 12 ? 0 : $self->{end_month};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'end_month'} == 12 ? : 119 94 213
297 213 my @pweeks = ( 4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 5 );
298 213 my $wkcnt = 52;
299  
300 # a truncated structure ignores the last week in a 53 week year
301 213 ▶ if ( $self->{_weeks} == 53 && !$truncate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_weeks"} == 53 and not $truncate) 52 161 213
Condition100%3/3
$self->{"_weeks"} == 53 and not $truncate
!ll&&!rl&&r
1352652
MC/DC100%2/2
$self->{"_weeks"} == 53 and not $truncate
$self->{"_weeks"} == 53 not $truncate
Truth table100%3/3
A and not(B)
A= $self->{"_weeks"} == 53B= not $truncate
ABresult
0X0
100
111
302 52 ▶ if ($restate) {
Branch: 2 branches
BranchTrueFalseTotal
if ($restate) { } 26 26 52
elsif ($self->{'leap_period'} eq 'first') { } 4 22 26
303  
304 # ignore the fist week and treat as any other 52 week year
305 26 $pstart->add( days => 7 );
306 }
307 elsif ( $self->{leap_period} eq 'first' ) {
308 4 $pweeks[$p1month] += 1;
309 4 $wkcnt = 53;
310 }
311 else {
312 22 $pweeks[ $self->{end_month} - 1 ] += 1;
313 22 $wkcnt = 53;
314 }
315 }
316  
317 my $pdata = {
318 summary => {
319 style => $style,
320 year => $self->{year},
321 end_month => $self->{end_month},
322 end_dow => $self->{end_dow},
323 end_type => $self->{end_type},
324 leap_period => $self->{leap_period},
325 213 weeks => $wkcnt,
326 start => $pstart->ymd,
327 end => undef, # this is set after the cache is built
328 }
329 };
330  
331 213 my $wdata = {};
332 213 my $wkcntr = 1;
333 213 for ( 0 .. 11 ) {
334 2556 my $p_index = ( $p1month + $_ ) % 12;
335  
336 2556 my $pinfo = {
337 period => $_ + 1,
338 weeks => $pweeks[$p_index],
339 month => $periodmonths[$p_index]
340 };
341  
342 2556 for my $pw ( 1 .. $pweeks[$p_index] ) {
343 11102 ▶ my $wksrc = $restate ? $wkcntr + 1 : $wkcntr;
Branch: 1 branch
BranchTrueFalseTotal
$restate ? : 1352 9750 11102
344 my $winfo = {
345 week => $wkcntr,
346 period => $_ + 1,
347 period_week => $pw,
348 start => $self->{_weeks_raw}->{$wksrc}->{start},
349 end => $self->{_weeks_raw}->{$wksrc}->{end},
350 11102 };
351 11102 ▶ $pinfo->{start} = $winfo->{start} if $pw == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $pw == 1 2556 8546 11102
352 11102 ▶ $pinfo->{end} = $winfo->{end} if $pw == $pweeks[$p_index];
Branch: 1 branch
BranchTrueFalseTotal
if $pw == $pweeks[$p_index] 2556 8546 11102
353 11102 $wdata->{$wkcntr} = $winfo;
354 11102 $wkcntr++;
355 }
356  
357 2556 $pdata->{ $_ + 1 } = $pinfo;
358 }
359 213 $pdata->{summary}->{end} = $pdata->{12}->{end};
360  
361 213 ▶ if ( $self->{_weeks} == 52 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_weeks'} == 52) { } 135 78 213
362  
363 # Set style to 'fiscal' and assign the structure to all
364 # three calendar types in a normal year to save time and space.
365 135 $pdata->{summary}->{style} = 'fiscal';
366 135 $self->{_fiscal} = $self->{_restated} = $self->{_truncated} = $pdata;
367 135 $self->{_fiscal_weeks} = $wdata;
368 135 $self->{_restated_weeks} = $wdata;
369 135 $self->{_truncated_weeks} = $wdata;
370 }
371 else {
372 78 $self->{"_$style"} = $pdata;
373 78 $self->{"_${style}_weeks"} = $wdata;
374 }
375  
376 213 return;
377 }
378  
379 # The end day for a specified year is trivial to determine. In normal
380 # accounting use, a fiscal year is named for the calendar year it ends in,
381 # not the year it begins.
382 394 ▶sub _end5253 {
Subroutine _end5253: called CC=5 SCAR=16.1
383 394 my $self = shift;
384  
385 my $dt = DateTime->last_day_of_month(
386 year => $self->{year},
387 month => $self->{end_month},
388 394 time_zone => 'floating'
389 );
390  
391 394 my $eom_day = $dt->day;
392 394 my $dt_dow = $dt->dow;
393  
394 394 ▶ if ( $dt_dow > $self->{end_dow} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dt_dow > $self->{'end_dow'}) { } 56 338 394
elsif ($dt_dow < $self->{'end_dow'}) { } 269 69 338
395 56 $dt->subtract( days => $dt_dow - $self->{end_dow} );
396 }
397 elsif ( $dt_dow < $self->{end_dow} ) {
398 269 $dt->subtract( days => ( $dt_dow + 7 ) - $self->{end_dow} );
399 }
400 $dt->add( weeks => 1 )
401 394 ▶ if $self->{end_type} eq 'closest' && $eom_day - $dt->day > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"end_type"} eq "closest" and $eom_day - $dt->day > 3 23 371 394
Condition100%3/3
$self->{"end_type"} eq "closest" and $eom_day - $dt->day > 3
!ll&&!rl&&r
3601123
MC/DC100%2/2
$self->{"end_type"} eq "closest" and $eom_day - $dt->day > 3
$self->{"end_type"} eq "closest" $eom_day - $dt->day > 3
Truth table100%3/3
A and B
A= $self->{"end_type"} eq "closest"B= $eom_day - $dt->day > 3
ABresult
0X0
100
111
402  
403 394 return $dt;
404 }
405  
406 # Finding the starting day for a specified year is easy. Simply find
407 # the last day of the preceding year since the year is defined by
408 # the ending day and add 1 day to that. This avoids calendar year and month
409 # boundary issues.
410 194 ▶sub _start5253 {
Subroutine _start5253: called CC=1 SCAR=0.0
411 194 my $self = shift;
412  
413 # do not assume it is safe to change the year attribute
414 194 local $self->{year} = $self->year - 1;
415 194 my $dt = $self->_end5253->add( days => 1 );
416  
417 194 return $dt;
418 }
419  
420 # Determine the correct fiscal year for any given date
421 39 ▶sub _find5253 {
Subroutine _find5253: called CC=3 SCAR=11.0
422 39 my $self = shift;
423  
424 39 my $y1 = $self->{_date}->year;
425  
426 # do not assume it is safe to change the year attribute
427 39 local $self->{year} = $y1;
428  
429 39 my $e1 = $self->_end5253;
430 39 ▶ return $y1 + 1 if $e1 < $self->{_date};
Branch: 1 branch
BranchTrueFalseTotal
if $e1 < $self->{"_date"} 6 33 39
431  
432 33 my $s1 = $self->_start5253;
433 33 ▶ return $y1 - 1 if $s1 > $self->{_date};
Branch: 1 branch
BranchTrueFalseTotal
if $s1 > $self->{"_date"} 0 33 33
434  
435 33 return $y1;
436 }
437  
438 117 ▶sub has_leap_week {
Subroutine has_leap_week: called CC=2 SCAR=6.9
Pod documented
439 117 my $self = shift;
440  
441 117 ▶ return ( $self->{_weeks} == 53 ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_weeks'} == 53 ? : 22 95 117
442 }
443  
444 # return summary data about a calendar.
445 6 ▶sub summary {
Subroutine summary: called CC=6 SCAR=18.2
Pod documented
446 6 my $self = shift;
447 6 ▶ my %args = @_ == 1 ? ( style => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 6 6
448  
449 6 ▶ $args{style} ||= $self->{style};
Condition66%2/3
$args{"style"} ||= $self->{"style"}
l!l&&r!l&&!r
420
MC/DC0%0/2
$args{"style"} ||= $self->{"style"}
$args{"style"} $self->{"style"}
Truth table66%2/3
A ||= B
A= $args{"style"}B= $self->{"style"}
ABresult
1X1
011
000
450 6 ▶ croak 'Unknown parameter present' if scalar( keys(%args) ) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args > 1 1 5 6
451  
452 5 my $cal = &{$_valid_cal_style}( $args{style} );
453  
454 4 my %cdata;
455 4 for (qw( style year start end weeks )) {
456 20 $cdata{$_} = $self->{"_$cal"}->{summary}->{$_};
457 }
458  
459 4 ▶ return wantarray ? %cdata : \%cdata;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
460 }
461  
462 568 ▶sub contains {
Subroutine contains: called CC=11 SCAR=24.0
Pod documented
463 568 my $self = shift;
464 568 ▶ my %args = @_ == 1 ? ( date => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 8 560 568
465  
466 568 ▶ $args{date} ||= 'today';
Condition100%2/2
$args{"date"} ||= "today"
l!l
5626
MC/DC100%1/1
$args{"date"} ||= "today"
$args{"date"}
Truth table100%2/2
A
A= $args{"date"}
Aresult
11
00
467 568 ▶ $args{style} ||= $self->{style};
Condition66%2/3
$args{"style"} ||= $self->{"style"}
l!l&&r!l&&!r
914770
MC/DC0%0/2
$args{"style"} ||= $self->{"style"}
$args{"style"} $self->{"style"}
Truth table66%2/3
A ||= B
A= $args{"style"}B= $self->{"style"}
ABresult
1X1
011
000
468  
469 568 ▶ croak 'Unknown parameter present' if scalar( keys(%args) ) > 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args > 2 1 567 568
470  
471 567 my $cal = &{$_valid_cal_style}( $args{style} );
472  
473 # Yes, a DT object set to "today" would work, but this is faster.
474 # NOTE! This will break in 2038 on 32-bit builds!
475 $args{date} = strftime( "%Y-%m-%d", localtime() )
476 566 ▶ if ( lc( $args{date} ) eq 'today' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $args{"date"} eq "today" 6 560 566
477  
478 # _str2dt will croak on error
479 566 my $date = &{$_str2dt}( $args{date} )->ymd;
480  
481 566 my $whash = $self->{"_${cal}_weeks"};
482 566 my $cdata = $self->{"_$cal"}->{summary};
483  
484 # it is NOT an error if the date isn't in the calendar,
485 # so return undef to differentiate this from an error condition
486 566 ▶ return if $date lt $cdata->{start} || $date gt $cdata->{end};
Branch: 1 branch
BranchTrueFalseTotal
if $date lt $cdata->{"start"} or $date gt $cdata->{"end"} 276 290 566
Condition100%3/3
$date lt $cdata->{"start"} or $date gt $cdata->{"end"}
l!l&&r!l&&!r
138138290
MC/DC100%2/2
$date lt $cdata->{"start"} or $date gt $cdata->{"end"}
$date lt $cdata->{"start"} $date gt $cdata->{"end"}
Truth table100%3/3
A or B
A= $date lt $cdata->{"start"}B= $date gt $cdata->{"end"}
ABresult
1X1
011
000
487  
488 # since the date is in the calendar, let's return it's week,
489 # and optionally, a structure with period and week number.
490  
491 290 my $w;
492 290 ▶ for ( $w = 1 ; $date gt $whash->{$w}->{end} ; $w++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date gt $whash->{$w}{"end"}) 7423 290 7713
493  
494 # this should NEVER fire!
495 7423 ▶ croak 'FATAL ERROR! RAN OUT OF WEEKS' if $w > $cdata->{weeks};
Branch: 1 branch
BranchTrueFalseTotal
if $w > $cdata->{"weeks"} 0 7423 7423
496 }
497 290 my $p = $whash->{$w}->{period};
498  
499 290 ▶ return wantarray ? ( period => $p, week => $w ) : $w;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 290 290
500 }
501  
502 # Utiliy routine, hidden from public use, to prevent duplicate code in
503 # the period attribute accessors.
504 113 ▶my $_period_attr = sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
505 113 my $self = shift;
506 113 my $attr = shift;
507 113 ▶ my %args = @_ == 1 ? ( period => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 109 113
508  
509 113 ▶ $args{period} ||= 0;
Condition100%2/2
$args{"period"} ||= 0
l!l
1067
MC/DC100%1/1
$args{"period"} ||= 0
$args{"period"}
Truth table100%2/2
A
A= $args{"period"}
Aresult
11
00
510 113 ▶ $args{style} ||= $self->{style};
Condition66%2/3
$args{"style"} ||= $self->{"style"}
l!l&&r!l&&!r
101030
MC/DC0%0/2
$args{"style"} ||= $self->{"style"}
$args{"style"} $self->{"style"}
Truth table66%2/3
A ||= B
A= $args{"style"}B= $self->{"style"}
ABresult
1X1
011
000
511  
512 113 ▶ croak 'Unknown parameter present' if scalar( keys(%args) ) > 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args > 2 7 106 113
513  
514 106 my $cal = &{$_valid_cal_style}( $args{style} );
515  
516 99 ▶ if ( $args{period} < 1 || $args{period} > 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"period"} < 1 or $args{"period"} > 12) 0 99 99
Condition33%1/3
$args{"period"} < 1 or $args{"period"} > 12
l!l&&r!l&&!r
0099
MC/DC0%0/2
$args{"period"} < 1 or $args{"period"} > 12
$args{"period"} < 1 $args{"period"} > 12
Truth table33%1/3
A or B
A= $args{"period"} < 1B= $args{"period"} > 12
ABresult
1X1
011
000
517 0 croak "Invalid period specified: $args{period}";
518 }
519  
520 # return a copy so the guts hopefully can't be changed
521 99 my %phash = %{ $self->{"_$cal"}->{ $args{period} } };
522  
523 99 ▶ return $attr eq 'period' ? %phash : $phash{$attr};
Branch: 1 branch
BranchTrueFalseTotal
$attr eq 'period' ? : 7 92 99
524 6 };
525  
526 # Automate creating period attribute mehtods
527 6 for my $p_attr (qw( month start end weeks )) {
528 24 my $method = join( '::', __PACKAGE__, "period_${p_attr}" );
529 {
530 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
531 104 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
532 104 my $self = shift;
533  
534 104 return $self->$_period_attr( $p_attr, @_ );
535 }
536 24 }
537 }
538  
539 9 ▶sub period {
Subroutine period: called CC=3 SCAR=11.0
Pod documented
540 9 my $self = shift;
541 9 ▶ my %args = @_ == 1 ? ( period => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 7 9
542  
543 9 my %phash = $self->$_period_attr( 'period', %args );
544  
545 7 ▶ return wantarray ? %phash : \%phash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 5 7
546 }
547  
548 # Utiliy routine, hidden from public use, to prevent duplicate code in
549 # the week attribute accessors.
550 2 ▶my $_week_attr = sub {
Subroutine __ANON__: called CC=8 SCAR=27.2
551 2 my $self = shift;
552 2 my $attr = shift;
553 2 ▶ my %args = @_ == 1 ? ( week => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 2 2
554  
555 2 ▶ $args{week} ||= 0;
Condition100%2/2
$args{"week"} ||= 0
l!l
11
MC/DC100%1/1
$args{"week"} ||= 0
$args{"week"}
Truth table100%2/2
A
A= $args{"week"}
Aresult
11
00
556 2 ▶ $args{style} ||= $self->{style};
Condition66%2/3
$args{"style"} ||= $self->{"style"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$args{"style"} ||= $self->{"style"}
$args{"style"} $self->{"style"}
Truth table66%2/3
A ||= B
A= $args{"style"}B= $self->{"style"}
ABresult
1X1
011
000
557  
558 2 ▶ croak 'Unknown parameter present' if scalar( keys(%args) ) > 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args > 2 1 1 2
559  
560 1 my $cal = &{$_valid_cal_style}( $args{style} );
561  
562 0 ▶ if ( $args{week} < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"week"} < 1 or $args{"week"} > $$self{"_$cal"}{"summary"}{"weeks"}) 0 0 0
Condition0%0/3
$args{"week"} < 1 or $args{"week"} > $$self{"_$cal"}{"summary"}{"weeks"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"week"} < 1 or $args{"week"} > $$self{"_$cal"}{"summary"}{"weeks"}
$args{"week"} < 1 $args{"week"} > $$self{"_$cal"}{"summary"}{"weeks"}
Truth table0%0/3
A or B
A= $args{"week"} < 1B= $args{"week"} > $$self{"_$cal"}{"summary"}{"weeks"}
ABresult
1X1
011
000
563 || $args{week} > $self->{"_$cal"}->{summary}->{weeks} )
564 {
565 0 croak "Invalid week specified: $args{week}";
566 }
567  
568 # make a copy so the outside (hopefully) won't change the guts
569 0 my %whash = %{ $self->{"_${cal}_weeks"}->{ $args{week} } };
570  
571 0 ▶ return $attr eq 'week' ? %whash : $whash{$attr};
Branch: 1 branch
BranchTrueFalseTotal
$attr eq 'week' ? : 0 0 0
572 6 };
573  
574 2 ▶sub week {
Subroutine week: called CC=3 SCAR=14.2
Pod documented
575 2 my $self = shift;
576 2 ▶ my %args = @_ == 1 ? ( week => shift ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 2 2
577  
578 2 my %whash = $self->$_week_attr( 'week', %args );
579  
580 0 ▶ return wantarray ? %whash : \%whash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
581 }
582  
583 # Automate creating week attribute mehtods
584 6 for my $w_attr (qw( period period_week start end )) {
585 24 my $method = join( '::', __PACKAGE__, "week_${w_attr}" );
586 {
587 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
588 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
589 0 my $self = shift;
590  
591 0 return $self->$_week_attr( $w_attr, @_ );
592 }
593 24 }
594 }
595  
596 6 1;
597  
598 __END__
↑ Summary