Gregorian/Business.pm

filter: Statementstmt 100% 308 / 308 Branchbran 100% 116 / 116 Conditioncond 98.3% 60 / 61 MC/DCmcdc 96.9% 32 / 33 Subroutinesub 100% 42 / 42 Podpod 92.8% 13 / 14 totaltotal 99.4% 571 / 574 CC 81 SCAR 19.4
CC
81
Cov
100%
add_businessdays
18.0
__ANON__
11.0
set_next_businessday
10.0
CRAP 7.0
↑ Summary Gregorian.pm »
1 # Copyright (c) 2005-2019 by Martin Becker, Blaubeuren.
2 # This package is free software; you can distribute it and/or modify it
3 # under the terms of the Artistic License 2.0 (see LICENSE file).
4  
5 package Date::Gregorian::Business;
6  
7 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Date::Gregorian qw(:weekdays);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @ISA = qw(Date::Gregorian);
14 2 our $VERSION = '0.13';
15  
16 # ----- object definition -----
17  
18 # ............. index .............. # .......... value ..........
19 2 ▶use constant F_OFFSET => Date::Gregorian::NFIELDS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use constant F_ALIGNMENT => F_OFFSET+0; # 0 = morning, 1 = evening
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant F_MAKE_CAL => F_OFFSET+1; # sub (date, year) => [calendar]
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant F_YEAR => F_OFFSET+2; # currently initialized year
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant F_CALENDAR => F_OFFSET+3; # list of: 1 = biz, 0 = holiday
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use constant NFIELDS => F_OFFSET+4;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # ----- other constants -----
27  
28 # index into calendar definition
29 2 ▶use constant _WEEKLY => 0; # array of non-biz weekdays
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use constant _YEARLY => 1; # array of holidays per year
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # index into single holiday per year definition
33 2 ▶use constant _D_MONTH => 0; # month number or 0 for easter
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use constant _D_DAY => 1; # day number or easter difference
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant _D_DELTA => 2; # array of deltas per weekday
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant _D_YEARS => 3; # array of first and last year
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # ----- predefined variables -----
39  
40 # elements of default biz calendars
41 2 my $skip_weekend = [ 0, 0, 0, 0, 0, 2, 1]; # Sat, Sun -> Mon
42 2 my $avoid_weekend = [ 0, 0, 0, 0, 0, -1, 1]; # Sat -> Fri, Sun -> Mon
43 2 my $next_monday = [ 0, 6, 5, 4, 3, 2, 1]; # set_weekday(Mon, ">=")
44 2 my $prev_monday = [-7, -1, -2, -3, -4, -5, -6]; # set_weekday(Mon, "<")
45 2 my $next_wednesday = [ 2, 1, 0, 6, 5, 4, 3]; # set_weekday(Wed, ">=")
46 2 my $next_thursday = [ 3, 2, 1, 0, 6, 5, 4]; # set_weekday(Thu, ">=")
47 2 my $saturday_sunday = [SATURDAY, SUNDAY];
48  
49 # some biz calendars known by default
50 2 my %samples = (
51 'us' => [
52 $saturday_sunday,
53 [
54 [ 1, 1, $skip_weekend], # New Year's day
55 [ 1, 15, $next_monday], # Martin Luther King
56 [ 2, 15, $next_monday], # President's day
57 [ 6, 1, $prev_monday], # Memorial day
58 [ 7, 4, $avoid_weekend], # Independence day
59 [ 9, 1, $next_monday], # Labor day
60 [10, 8, $next_monday], # Columbus day
61 [11, 11, $avoid_weekend], # Veteran's day
62 [11, 22, $next_thursday], # Thanksgiving day
63 [12, 25, $skip_weekend], # Christmas day
64 ],
65 ],
66 'de' => [ # Germany
67 $saturday_sunday,
68 [
69 [ 1, 1], # New Year's day
70 [ 0, -2], # Good Friday
71 [ 0, 1], # Easter Monday
72 [ 5, 1], # Labour day
73 [ 0, 39], # Ascension day
74 [ 0, 50], # Pentecost Monday
75 [ 6, 17, undef, [1954, 1990]], # German Unity
76 [10, 3, undef, [1990, undef]], # German Unity
77 [10, 31, undef, [2017, 2017]], # Reformation day
78 [11, 16, $next_wednesday, [undef, 1994]], # Penitence day
79 [12, 25], # Christmas day
80 [12, 26], # 2nd day of Christmas
81 ],
82 ],
83 'dd' => [ # Germany, new states
84 $saturday_sunday,
85 [
86 [ 1, 1, ], # New Year's day
87 [ 0, -2, ], # Good Friday
88 [ 0, 1, undef, [undef, 1967]], # Easter Monday
89 [ 0, 1, undef, [1990, undef]], # Easter Monday
90 [ 5, 1, ], # Labour day
91 [ 5, 8, undef, [undef, 1967]], # Liberation day
92 [ 5, 8, undef, [1985, 1985]], # Liberation day
93 [ 5, 9, undef, [1975, 1975]], # Victory day
94 [ 0, 39, undef, [undef, 1967]], # Ascension day
95 [ 0, 39, undef, [1990, undef]], # Ascension day
96 [ 0, 50, ], # Pentecost Monday
97 [10, 3, undef, [1990, undef]], # German Unity
98 [10, 7, undef, [undef, 1989]], # Republic day
99 [10, 31, undef, [undef, 1966]], # Reformation day
100 [10, 31, undef, [1990, undef]], # Reformation day
101 [11, 16, $next_wednesday, [undef, 1966]], # Penitence day
102 [11, 16, $next_wednesday, [1990, 1994]], # Penitence day
103 [12, 25, ], # Christmas day
104 [12, 26, ], # 2nd day of Christmas
105 ],
106 ],
107 );
108 $samples{'de_BW'} = [ # Baden-Wuerttemberg
109 $saturday_sunday,
110 [
111 2 @{$samples{'de'}->[_YEARLY]},
112 [ 1, 6], # Epiphany
113 [ 0, 60], # Corpus Christi
114 [11, 1], # All Saints day
115 ]
116 ];
117 $samples{'de_BY'} = [ # Bayern
118 $saturday_sunday,
119 [
120 2 @{$samples{'de_BW'}->[_YEARLY]},
121 [ 8, 15], # Assumption day
122 ]
123 ];
124 $samples{'de_Augsburg'} = [ # Stadt Augsburg
125 $saturday_sunday,
126 [
127 2 @{$samples{'de_BY'}->[_YEARLY]},
128 [ 8, 1], # Peace day
129 ]
130 ];
131 $samples{'de_BE'} = [ # Berlin
132 $saturday_sunday,
133 [
134 2 @{$samples{'de'}->[_YEARLY]},
135 [ 1, 8, undef, [2019, undef]], # Women's day
136 ]
137 ];
138 $samples{'de_BB'} = [ # Brandenburg
139 $saturday_sunday,
140 [
141 2 @{$samples{'dd'}->[_YEARLY]},
142 ]
143 ];
144 $samples{'de_HB'} = [ # Bremen
145 $saturday_sunday,
146 [
147 2 @{$samples{'de'}->[_YEARLY]},
148 [10, 31], # Reformation day
149 ]
150 ];
151 $samples{'de_HH'} = [ # Hamburg
152 $saturday_sunday,
153 [
154 2 @{$samples{'de_HB'}->[_YEARLY]},
155 ]
156 ];
157 $samples{'de_HE'} = [ # Hessen
158 $saturday_sunday,
159 [
160 2 @{$samples{'de'}->[_YEARLY]},
161 [ 0, 60], # Corpus Christi
162 ]
163 ];
164 $samples{'de_MV'} = [ # Mecklenburg-Vorpommern
165 $saturday_sunday,
166 [
167 2 @{$samples{'dd'}->[_YEARLY]},
168 ]
169 ];
170 $samples{'de_NI'} = [ # Niedersachsen
171 $saturday_sunday,
172 [
173 2 @{$samples{'de_HB'}->[_YEARLY]},
174 ]
175 ];
176 $samples{'de_NW'} = [ # Nordrhein-Westfalen
177 $saturday_sunday,
178 [
179 2 @{$samples{'de_HE'}->[_YEARLY]},
180 [11, 1], # All Saints day
181 ]
182 ];
183 $samples{'de_RP'} = [ # Rheinland-Pfalz
184 $saturday_sunday,
185 [
186 2 @{$samples{'de_NW'}->[_YEARLY]},
187 ]
188 ];
189 $samples{'de_SL'} = [ # Saarland
190 $saturday_sunday,
191 [
192 2 @{$samples{'de_NW'}->[_YEARLY]},
193 [ 8, 15], # Assumption day
194 ]
195 ];
196 $samples{'de_SN'} = [ # Sachsen
197 $saturday_sunday,
198 [
199 2 @{$samples{'dd'}->[_YEARLY]},
200 [11, 16, $next_wednesday, [1995, undef]], # Penitence day
201 ]
202 ];
203 $samples{'de_ST'} = [ # Sachsen-Anhalt
204 $saturday_sunday,
205 [
206 2 @{$samples{'dd'}->[_YEARLY]},
207 [ 1, 6, undef, [1990, undef]], # Epiphany
208 ]
209 ];
210 $samples{'de_SH'} = [ # Schleswig-Holstein
211 $saturday_sunday,
212 [
213 2 @{$samples{'de_HB'}->[_YEARLY]},
214 ]
215 ];
216 $samples{'de_TH'} = [ # Thueringen
217 $saturday_sunday,
218 [
219 2 @{$samples{'dd'}->[_YEARLY]},
220 [ 9, 20, undef, [2019, undef]], # Children's day
221 ]
222 ];
223  
224 2 my $default_configuration = 'us';
225  
226 # ----- private functions and methods -----
227  
228 # check whether a given year is in a range or general selection
229 223 ▶sub _select_year {
Subroutine _select_year: called CC=6 SCAR=17.9
230 223 my ($self, $day, $year) = @_;
231 223 my $selection = $day->[3];
232 223 ▶ if (!ref $selection) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $selection) 39 184 223
233 39 return $year == $selection;
234 }
235 184 ▶ if ('CODE' eq ref $selection) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $selection) 39 145 184
236 39 return $selection->($self, $year, @{$day}[0, 1]);
237 }
238 return
239 145 ▶ (!defined($selection->[0]) || $selection->[0] <= $year) &&
Condition100%9/9
(!defined($selection->[0]) || $selection->[0] <= $year) && (!defined($selection->[1]) || $year <= $selection->[1])
!ll&&!rl&&r
374068
not defined $selection->[0] or $selection->[0] <= $year
l!l&&r!l&&!r
466237
not defined $selection->[1] or $year <= $selection->[1]
l!l&&r!l&&!r
254340
MC/DC100%4/4
(!defined($selection->[0]) || $selection->[0] <= $year) && (!defined($selection->[1]) || $year <= $selection->[1])
not defined $selection->[0] $selection->[0] <= $year not defined $selection->[1] $year <= $selection->[1]
Truth table85%6/7
not(A) or B && not(C) or D
A= not defined $selection->[0]B= $selection->[0] <= $yearC= not defined $selection->[1]D= $year <= $selection->[1]
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
240 (!defined($selection->[1]) || $year <= $selection->[1]);
241 }
242  
243 # make_cal factory, generating a calendar generator enclosing a configuration
244 23 ▶sub _make_make_cal {
Subroutine _make_make_cal: called CC=1 SCAR=0.0
245 23 my ($weekly, $yearly) = @_;
246  
247 165 ▶ return sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
248 165 my ($date, $year) = @_;
249 165 my $firstday = $date->new->set_yd($year, 1, 1);
250 165 my $first_wd = $firstday->get_weekday;
251 165 ▶ my $someday = @$yearly && $firstday->new;
Condition66%2/3
@$yearly && $firstday->new
!ll&&!rl&&r
30162
MC/DC50%1/2
@$yearly && $firstday->new
@$yearly $firstday->new
Truth table66%2/3
A && B
A= @$yearlyB= $firstday->new
ABresult
0X0
100
111
252 165 my $easter = undef;
253 165 my $index;
254 165 my $calendar = $firstday->get_empty_calendar($year, $weekly);
255 165 foreach my $day (@$yearly) {
256 1484 ▶ if (!defined($day->[3]) || _select_year($someday, $day, $year)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $day->[3] or _select_year($someday, $day, $year)) 1355 129 1484
Condition100%3/3
not defined $day->[3] or _select_year($someday, $day, $year)
l!l&&r!l&&!r
126194129
MC/DC100%2/2
not defined $day->[3] or _select_year($someday, $day, $year)
not defined $day->[3] _select_year($someday, $day, $year)
Truth table100%3/3
not(A) or B
A= not defined $day->[3]B= _select_year($someday, $day, $year)
ABresult
1X1
011
000
257 1355 ▶ if ($day->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day->[0]) { } 1291 64 1355
258 $index =
259 1291 $someday->set_ymd($year, @{$day}[0, 1])
260 ->get_days_since($firstday);
261 1291 ▶ $index += $day->[2]->[$someday->get_weekday] if $day->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $day->[2] 1120 171 1291
262 }
263 else {
264 64 ▶ if (!defined $easter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $easter) 15 49 64
265 15 $easter =
266 $someday->set_easter($year)
267 ->get_days_since($firstday);
268 }
269 64 $index = $easter + $day->[1];
270 64 ▶ $index += $day->[2]->[(496 + $day->[1]) % 7] if $day->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $day->[2] 16 48 64
271 }
272 1355 ▶ $calendar->[$index] = 0 if 0 <= $index && $index < @$calendar;
Branch: 1 branch
BranchTrueFalseTotal
if 0 <= $index and $index < @$calendar 1343 12 1355
Condition100%3/3
0 <= $index and $index < @$calendar
!ll&&!rl&&r
661343
MC/DC100%2/2
0 <= $index and $index < @$calendar
0 <= $index $index < @$calendar
Truth table100%3/3
A and B
A= 0 <= $indexB= $index < @$calendar
ABresult
0X0
100
111
273 }
274 }
275 165 return $calendar;
276 23 };
277 }
278  
279 # fetch biz calendar for given year, initializing it if necessary
280 5562 ▶sub _calendar {
Subroutine _calendar: called CC=3 SCAR=11.0
281 5562 my ($self, $year) = @_;
282  
283 5562 ▶ if (!defined($self->[F_YEAR]) || $year != $self->[F_YEAR]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->[8] or $year != $self->[8]) 181 5381 5562
Condition100%3/3
not defined $self->[8] or $year != $self->[8]
l!l&&r!l&&!r
461355381
MC/DC100%2/2
not defined $self->[8] or $year != $self->[8]
not defined $self->[8] $year != $self->[8]
Truth table100%3/3
not(A) or B
A= not defined $self->[8]B= $year != $self->[8]
ABresult
1X1
011
000
284 181 $self->[F_YEAR] = $year;
285 181 $self->[F_CALENDAR] = $self->[F_MAKE_CAL]->($self, $year);
286 }
287 5562 return $self->[F_CALENDAR];
288 }
289  
290 # ----- public methods -----
291  
292 183 ▶sub get_empty_calendar {
Subroutine get_empty_calendar: called CC=3 SCAR=11.0
Pod documented
293 183 my ($date, $year, $weekly_nonbiz) = @_;
294  
295 183 my $firstday = $date->new->set_yd($year, 1);
296 183 my $days = $firstday->get_days_in_year($year);
297 183 my $first_wd = $firstday->get_weekday;
298  
299 183 my @week = (1) x 7;
300 183 foreach my $day (@$weekly_nonbiz) {
301 283 $week[$day] = 0;
302 }
303 183 ▶ @week = @week[$first_wd .. 6, 0 .. $first_wd-1] if $first_wd;
Branch: 1 branch
BranchTrueFalseTotal
if $first_wd 173 10 183
304  
305 183 my @calendar = ((@week) x ($days / 7), @week[0 .. ($days % 7)-1]);
306 183 return \@calendar;
307 }
308  
309 9 ▶sub define_configuration {
Subroutine define_configuration: called CC=6 SCAR=17.9
Pod documented
310 9 my ($class, $name, $configuration) = @_;
311 9 ▶ my $type = defined($configuration)? ref($configuration): '!';
Branch: 1 branch
BranchTrueFalseTotal
defined $configuration ? : 8 1 9
312  
313 9 ▶ if (!$type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 5 4 9
elsif ('ARRAY' ne $type and 'CODE' ne $type) { } 2 3 5
314 5 ▶ return undef if !exists $samples{$configuration};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $samples{$configuration} 2 3 5
315 3 $configuration = $samples{$configuration};
316 }
317 ▶ elsif ('ARRAY' ne $type && 'CODE' ne $type) {
Condition100%3/3
"ARRAY" ne $type and "CODE" ne $type
!ll&&!rl&&r
112
MC/DC100%2/2
"ARRAY" ne $type and "CODE" ne $type
"ARRAY" ne $type "CODE" ne $type
Truth table100%3/3
A and B
A= "ARRAY" ne $typeB= "CODE" ne $type
ABresult
0X0
100
111
318 2 return undef;
319 }
320 5 $samples{$name} = $configuration;
321 5 return $class;
322 }
323  
324 36 ▶sub configure_business {
Subroutine configure_business: called CC=9 SCAR=22.0
Pod documented
325 36 my ($self, $configuration) = @_;
326 36 ▶ my $type = defined($configuration)? ref($configuration): '!';
Branch: 1 branch
BranchTrueFalseTotal
defined $configuration ? : 35 1 36
327  
328 36 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 22 14 36
329 22 ▶ return undef if !exists $samples{$configuration};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $samples{$configuration} 4 18 22
330 18 $configuration = $samples{$configuration};
331 18 $type = ref $configuration;
332 }
333 32 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 28 4 32
334 # instance method: configure this object
335 28 ▶ if ('CODE' eq $type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('CODE' eq $type) { } 4 24 28
elsif ('ARRAY' eq $type) { } 23 1 24
336 4 $self->[F_MAKE_CAL] = $configuration;
337 }
338 elsif ('ARRAY' eq $type) {
339 23 $self->[F_MAKE_CAL] = _make_make_cal(@$configuration);
340 }
341 else {
342 1 return undef;
343 }
344 27 $self->[F_YEAR] = $self->[F_CALENDAR] = undef;
345 }
346 else {
347 # class method: configure default
348 4 ▶ if ('ARRAY' ne $type && 'CODE' ne $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne $type and "CODE" ne $type) 2 2 4
Condition100%3/3
"ARRAY" ne $type and "CODE" ne $type
!ll&&!rl&&r
112
MC/DC100%2/2
"ARRAY" ne $type and "CODE" ne $type
"ARRAY" ne $type "CODE" ne $type
Truth table100%3/3
A and B
A= "ARRAY" ne $typeB= "CODE" ne $type
ABresult
0X0
100
111
349 2 return undef;
350 }
351 2 $default_configuration = $configuration;
352 }
353  
354 29 return $self;
355 }
356  
357 703 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
358 703 my ($class_or_object, $configuration) = @_;
359 703 my $self = $class_or_object->SUPER::new;
360  
361 703 ▶ if (!ref $class_or_object) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $class_or_object) 16 687 703
362 16 $self->[F_ALIGNMENT] = 0;
363 }
364 703 ▶ if (defined $configuration) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $configuration) { } 10 693 703
elsif (not ref $class_or_object) { } 6 687 693
365 10 return $self->configure_business($configuration);
366 }
367 elsif (!ref $class_or_object) {
368 6 return $self->configure_business($default_configuration);
369 }
370 687 return $self;
371 }
372  
373 96 ▶sub align {
Subroutine align: called CC=2 SCAR=6.9
Pod documented
374 96 my ($self, $alignment) = @_;
375 96 ▶ $self->[F_ALIGNMENT] = $alignment? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$alignment ? : 47 49 96
376 96 return $self;
377 }
378  
379 195 ▶sub get_alignment {
Subroutine get_alignment: called CC=1 SCAR=0.0
Pod documented
380 195 my $self = $_[0];
381 195 return $self->[F_ALIGNMENT];
382 }
383  
384 # tweak super class to provide default alignment
385 32 ▶sub Date::Gregorian::get_alignment {
Subroutine get_alignment: called CC=1 SCAR=0.0
Pod undocumented
386 32 return 0;
387 }
388  
389 5264 ▶sub is_businessday {
Subroutine is_businessday: called CC=1 SCAR=0.0
Pod documented
390 5264 my ($self) = @_;
391 5264 my ($year, $day) = $self->get_yd;
392  
393 5264 return $self->_calendar($year)->[$day-1];
394 }
395  
396 # count business days, proceeding into the future
397 # $days gives the interval measured in real days (positive)
398 # alignment tells where to start: 0 = at current day, 1 = the day after
399 # 0 <= result <= $days
400 63 ▶sub _count_businessdays_up {
Subroutine _count_businessdays_up: called CC=4 SCAR=13.9
401 63 my ($self, $days) = @_;
402 63 my ($year, $day) = $self->get_yd;
403 63 my $calendar = $self->_calendar($year);
404 63 my $result = 0;
405  
406 63 ▶ --$day if !$self->[F_ALIGNMENT];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[6] 37 26 63
407 63 ▶ while (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 3199 63 3262
408 3199 ▶ while (@$calendar <= $day) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calendar <= $day) 6 3199 3205
409 6 $calendar = $self->_calendar(++$year);
410 6 $day = 0;
411 }
412 3199 do {
413 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 3199 $result += $calendar->[$day];
415 };
416 3199 ++$day;
417 3199 --$days;
418 }
419 63 return $result;
420 }
421  
422 # count business days, proceeding into the past
423 # $days gives the interval measured in real days (positive)
424 # alignment tells where to start: 1 = at current day, 0 = the day before
425 # 0 <= result <= $days
426 56 ▶sub _count_businessdays_down {
Subroutine _count_businessdays_down: called CC=4 SCAR=13.9
427 56 my ($self, $days) = @_;
428 56 my ($year, $day) = $self->get_yd;
429 56 my $calendar = $self->_calendar($year);
430 56 my $result = 0;
431  
432 56 ▶ --$day if !$self->[F_ALIGNMENT];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[6] 29 27 56
433 56 ▶ while (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 1081 56 1137
434 1081 --$day;
435 1081 --$days;
436 1081 ▶ while ($day < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day < 0) 8 1081 1089
437 8 $calendar = $self->_calendar(--$year);
438 8 $day = $#$calendar;
439 }
440 1081 do {
441 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
442 1081 $result += $calendar->[$day];
443 };
444 }
445 56 return $result;
446 }
447  
448 # Alignments and results Now:0 Now:1 Now:0 Now:1
449 # b--(H)--b---b---b--(H)--b---b Then:0 Then:1 Then:1 Then:0
450 # Then Now 3 3 3 3
451 # Then Now 3 2 2 3
452 # Then Now 3 4 3 4
453 # Then Now 3 3 2 4
454 # b--(H)--b---b---b--(H)--b---b
455 # Now Then -3 -3 -3 -3
456 # Now Then -3 -4 -4 -3
457 # Now Then -3 -2 -3 -2
458 # Now Then -3 -3 -4 -2
459 # b--(H)--b---b---b--(H)--b---b
460  
461 58 ▶sub get_businessdays_since {
Subroutine get_businessdays_since: called CC=3 SCAR=11.0
Pod documented
462 58 my ($self, $then) = @_;
463 58 my $delta =
464 $self->get_days_since($then) +
465 $self->[F_ALIGNMENT] - $then->get_alignment;
466 58 ▶ if ($delta > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 0) 29 29 58
467 29 return $self->_count_businessdays_down($delta);
468 }
469 29 ▶ if ($delta < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 0) 27 2 29
470 27 return -$self->_count_businessdays_up(-$delta);
471 }
472 2 return 0;
473 }
474  
475 65 ▶sub get_businessdays_until {
Subroutine get_businessdays_until: called CC=3 SCAR=11.0
Pod documented
476 65 my ($self, $then) = @_;
477 65 my $delta =
478 $self->get_days_since($then) +
479 $self->[F_ALIGNMENT] - $then->get_alignment;
480 65 ▶ if ($delta > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 0) 27 38 65
481 27 return -$self->_count_businessdays_down($delta);
482 }
483 38 ▶ if ($delta < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 0) 36 2 38
484 36 return $self->_count_businessdays_up(-$delta);
485 }
486 2 return 0;
487 }
488  
489 40 ▶sub set_next_businessday {
Subroutine set_next_businessday: called CC=10 SCAR=23.0
Pod documented
490 40 my ($self, $relation) = @_;
491 40 my ($year, $day) = $self->get_yd;
492 40 my $calendar = $self->_calendar($year);
493  
494 40 --$day;
495 40 ▶ return $self if '<' ne $relation && '>' ne $relation && $calendar->[$day];
Branch: 1 branch
BranchTrueFalseTotal
if "<" ne $relation and ">" ne $relation and $calendar->[$day] 8 32 40
Condition100%6/6
"<" ne $relation and ">" ne $relation and $calendar->[$day]
!ll&&!rl&&r
20128
"<" ne $relation and ">" ne $relation
!ll&&!rl&&r
101020
MC/DC100%3/3
"<" ne $relation and ">" ne $relation and $calendar->[$day]
"<" ne $relation ">" ne $relation $calendar->[$day]
Truth table100%4/4
A and B and C
A= "<" ne $relationB= ">" ne $relationC= $calendar->[$day]
ABCresult
0XX0
10X0
1100
1111
496 32 ▶ if ('<' eq $relation || '<=' eq $relation) {
Branch: 1 branch
BranchTrueFalseTotal
if ('<' eq $relation or '<=' eq $relation) { } 16 16 32
Condition100%3/3
"<" eq $relation or "<=" eq $relation
l!l&&r!l&&!r
10616
MC/DC100%2/2
"<" eq $relation or "<=" eq $relation
"<" eq $relation "<=" eq $relation
Truth table100%3/3
A or B
A= "<" eq $relationB= "<=" eq $relation
ABresult
1X1
011
000
497 16 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$calendar->[$day] or do { do { --$day; while ($day < 0) { $calendar = $self->_calendar(--$year); $day = $calendar->$#*; } } } 16 18 34
498 34 --$day;
499 34 ▶ while ($day < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day < 0) 14 34 48
500 14 $calendar = $self->_calendar(--$year);
501 14 $day = $#$calendar;
502 }
503 }
504 while (!$calendar->[$day]);
505 }
506 else {
507 16 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$calendar->[$day] or do { do { ++$day; while (@$calendar <= $day) { $calendar = $self->_calendar(++$year); $day = 0; } } } 16 18 34
508 34 ++$day;
509 34 ▶ while (@$calendar <= $day) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calendar <= $day) 2 34 36
510 2 $calendar = $self->_calendar(++$year);
511 2 $day = 0;
512 }
513 }
514 while (!$calendar->[$day]);
515 }
516 32 return $self->set_yd($year, $day+1);
517 }
518  
519 22 ▶sub iterate_businessdays_upto {
Subroutine iterate_businessdays_upto: called CC=2 SCAR=6.9
Pod documented
520 22 my ($self, $limit, $rel) = @_;
521 22 my $days = ($rel eq '<=') - $self->get_days_since($limit);
522 22 my ($year, $day, $calendar);
523 22 ▶ if (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 18 4 22
524 18 ($year, $day) = $self->get_yd;
525 18 --$day;
526 18 $calendar = $self->_calendar($year);
527 }
528 70 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
529 70 ▶ while (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 89 22 111
530 89 ▶ while (@$calendar <= $day) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calendar <= $day) 2 89 91
531 2 $calendar = $self->_calendar(++$year);
532 2 $day = 0;
533 }
534 89 --$days;
535 89 ▶ if ($calendar->[$day++]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$calendar[$day++]) 48 41 89
536 48 return $self->set_yd($year, $day);
537 }
538 }
539 22 return undef;
540 22 };
541 }
542  
543 10 ▶sub iterate_businessdays_downto {
Subroutine iterate_businessdays_downto: called CC=2 SCAR=6.9
Pod documented
544 10 my ($self, $limit, $rel) = @_;
545 10 my $days = $self->get_days_since($limit) + ($rel ne '>');
546 10 my ($year, $day, $calendar);
547 10 ▶ if (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 6 4 10
548 6 ($year, $day) = $self->get_yd;
549 6 --$day;
550 6 $calendar = $self->_calendar($year);
551 }
552 34 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
553 34 ▶ while (0 < $days) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days) 51 10 61
554 51 ▶ while ($day < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day < 0) 1 51 52
555 1 $calendar = $self->_calendar(--$year);
556 1 $day = $#$calendar;
557 }
558 51 --$days;
559 51 ▶ if ($calendar->[$day--]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$calendar[$day--]) 24 27 51
560 24 return $self->set_yd($year, $day+2);
561 }
562 }
563 10 return undef;
564 10 };
565 }
566  
567 # -b----H----b----b----H----b-
568 # ^ ^ ^ ^
569 # 0 0 1 1 2 2
570  
571 72 ▶sub add_businessdays {
Subroutine add_businessdays: called CC=18 SCAR=28.9
Pod documented
572 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
573 72 my ($self, $days, $new_alignment) = @_;
574 72 my ($year, $day) = $self->get_yd;
575 72 -- $day;
576 72 my $calendar = $self->_calendar($year);
577 72 my $alignment = $self->[F_ALIGNMENT];
578  
579 # handle alignment change
580 72 ▶ if (defined($new_alignment) && ($alignment xor $new_alignment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_alignment and $alignment ^^ $new_alignment) 20 52 72
Condition100%7/7
defined $new_alignment and $alignment ^^ $new_alignment
!ll&&!rl&&r
322020
$alignment xor $new_alignment
l&&rl&&!r!l&&r!l&&!r
10101010
MC/DC100%3/3
defined $new_alignment and $alignment ^^ $new_alignment
defined $new_alignment $alignment $new_alignment
Truth table100%5/5
A and B xor C
A= defined $new_alignmentB= $alignmentC= $new_alignment
ABCresult
0XX0
1110
1000
1101
1011
581 20 ▶ if ($new_alignment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_alignment) { } 10 10 20
582 10 $alignment = $self->[F_ALIGNMENT] = 1;
583 10 $days -= $calendar->[$day];
584 }
585 else {
586 10 $alignment = $self->[F_ALIGNMENT] = 0;
587 10 $days += $calendar->[$day];
588 }
589 }
590  
591 72 ▶ if (0 < $days || !$days && !$alignment) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days or not $days || $alignment) { } 42 30 72
Condition100%6/6
0 < $days or not $days || $alignment
l!l&&r!l&&!r
321030
$days || $alignment
l!l&&r!l&&!r
201010
MC/DC100%3/3
0 < $days or not $days || $alignment
0 < $days $days $alignment
Truth table100%4/4
A or not(B || C)
A= 0 < $daysB= $daysC= $alignment
ABCresult
1XX1
0001
01X0
0010
592 # move forward in time
593 42 ▶ $days -= $calendar->[$day] if !$alignment;
Branch: 1 branch
BranchTrueFalseTotal
unless $alignment 33 9 42
594 42 ▶ while (0 < $days || !$days && !$alignment) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $days or not $days || $alignment) 131 42 173
Condition100%6/6
0 < $days or not $days || $alignment
l!l&&r!l&&!r
874442
$days || $alignment
l!l&&r!l&&!r
39344
MC/DC100%3/3
0 < $days or not $days || $alignment
0 < $days $days $alignment
Truth table100%4/4
A or not(B || C)
A= 0 < $daysB= $daysC= $alignment
ABCresult
1XX1
0001
01X0
0010
595 131 ++$day;
596 131 ▶ while (@$calendar <= $day) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calendar <= $day) 3 131 134
597 3 $calendar = $self->_calendar(++$year);
598 3 $day = 0;
599 }
600 131 $days -= $calendar->[$day];
601 }
602 }
603 else {
604 # move backwards in time
605 30 ▶ $days += $calendar->[$day] if $alignment;
Branch: 1 branch
BranchTrueFalseTotal
if $alignment 21 9 30
606 30 ▶ while ($days < 0 || !$days && $alignment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($days < 0 or not $days and $alignment) 107 30 137
Condition100%6/6
$days < 0 or not $days and $alignment
l!l&&r!l&&!r
782930
not $days and $alignment
!ll&&!rl&&r
27329
MC/DC100%3/3
$days < 0 or not $days and $alignment
$days < 0 not $days $alignment
Truth table100%4/4
A or not(B) and C
A= $days < 0B= not $daysC= $alignment
ABCresult
1XX1
0111
00X0
0100
607 107 --$day;
608 107 ▶ while ($day < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day < 0) 7 107 114
609 7 $calendar = $self->_calendar(--$year);
610 7 $day = $#$calendar;
611 }
612 107 $days += $calendar->[$day];
613 }
614 }
615  
616 72 return $self->set_yd($year, $day+1);
617 }
618  
619 2 1;
620  
621 __END__
↑ Summary Gregorian.pm »