blib/lib/Date/Holidays/USExtended.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 86.0% 80 / 93 CC 11 SCAR 11.4
CC
11
Cov
87%
is_holiday
5.9
holidays
4.0
_nth_day_of_month
3.0
CRAP 3.1
↑ Summary
1 package Date::Holidays::USExtended;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Provides an extended set of United States holidays
5  
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Date::Easter qw(easter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DateTime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our @EXPORT = qw(is_holiday holidays);
15  
16 2 our $VERSION = '0.0300';
17  
18  
19 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 1 my $self = shift;
21 1 bless \$self => $self;
22 }
23  
24  
25 372 ▶sub is_holiday {
Subroutine is_holiday: called CC=5 SCAR=17.8
Pod documented
26 372 my ($self, $year, $month, $day) = @_;
27 372 ▶ return undef unless $year && $month && $day;
Branch: 1 branch
BranchTrueFalseTotal
unless $year and $month and $day 0 372 372
Condition33%2/6
$year and $month and $day
!ll&&!rl&&r
00372
$year and $month
!ll&&!rl&&r
00372
MC/DC0%0/3
$year and $month and $day
$year $month $day
Truth table25%1/4
A and B and C
A= $yearB= $monthC= $day
ABCresult
0XX0
10X0
1100
1111
28 372 my $holidays = $self->holidays($year);
29 372 my $str = sprintf '%02d%02d', $month, $day;
30 372 ▶ return $holidays->{$str} ? $holidays->{$str} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$holidays->{$str} ? : 21 351 372
31 }
32  
33  
34 375 ▶sub us_holidays {
Subroutine us_holidays: called CC=2 SCAR=7.3
Pod documented
35 375 my ($self, $year) = @_;
36 375 ▶ unless ($year) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($year) 0 375 375
37 0 $year = (localtime)[5];
38 0 $year += 1900;
39 }
40 375 my %dom = (
41 memorial => _nth_day_of_month(-1, 1, $year, 5),
42 mothers => _nth_day_of_month(2, 7, $year, 5),
43 fathers => _nth_day_of_month(3, 7, $year, 6),
44 labor => _nth_day_of_month(1, 1, $year, 9),
45 columbus => _nth_day_of_month(2, 1, $year, 10),
46 thanksgiving => _nth_day_of_month(4, 4, $year, 11),
47 );
48 my %holidays = (
49 1 => {
50 1 => "New Year's Day",
51 15 => 'Martin Luther King Jr.',
52 },
53 2 => {
54 14 => "Valentine's Day",
55 19 => "President's Day",
56 },
57 3 => {
58 17 => "St. Patrick's Day",
59 },
60 4 => {
61 },
62 5 => {
63 5 => 'Cinco de Mayo',
64 $dom{mothers} => "Mother's Day",
65 $dom{memorial} => 'Memorial Day',
66 },
67 6 => {
68 14 => 'Flag Day',
69 $dom{fathers} => "Father's Day",
70 19 => 'Juneteenth',
71 },
72 7 => {
73 4 => 'Independence Day',
74 },
75 8 => {
76 },
77 9 => {
78 $dom{labor} => 'Labor Day',
79 },
80 10 => {
81 $dom{columbus} => "Columbus; Indigenous Peoples' Day",
82 31 => 'Halloween'
83 },
84 11 => {
85 11 => "Veteran's Day",
86 375 $dom{thanksgiving} => 'Thanksgiving',
87 },
88 12 => {
89 24 => 'Christmas Eve',
90 25 => 'Christmas',
91 31 => "New Year's Eve",
92 },
93 );
94 375 my ($month, $day) = easter($year);
95 375 $holidays{$month}->{$day} = 'Easter';
96 375 return \%holidays;
97 }
98  
99  
100 373 ▶sub holidays {
Subroutine holidays: called CC=4 SCAR=13.9
Pod documented
101 373 my ($self, $year) = @_;
102 373 my $holidays = $self->us_holidays($year);
103 373 my %rtn;
104 373 for my $month (sort { $a <=> $b } keys %$holidays) {
105 4476 for my $day (sort { $a <=> $b } keys %{ $holidays->{$month} }) {
106 $rtn{ sprintf '%02d%02d', $month, $day } = $holidays->{$month}->{$day}
107 7833 ▶ if $holidays->{$month}->{$day};
Branch: 1 branch
BranchTrueFalseTotal
if $holidays->{$month}{$day} 7833 0 7833
108 }
109 }
110 373 return \%rtn;
111 }
112  
113 # https://stackoverflow.com/questions/18908238/perl-datetime-module-calculating-first-second-third-fouth-last-sunday-monda
114 # Here $nth is 1, 2, 3... for first, second, third, etc.
115 # Or -1, -2, -3... for last, next-to-last, etc.
116 # $dow is 1-7 for Monday-Sunday. $month is 1-12
117 2250 ▶sub _nth_day_of_month {
Subroutine _nth_day_of_month: called CC=3 SCAR=11.0
118 2250 my ($nth, $dow, $year, $month) = @_;
119  
120 2250 my ($date, $delta);
121 2250 ▶ if ($nth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth > 0) { } 1875 375 2250
122 # For 1st etc. we want the last day of that week (i.e. 7, 14, 21, 28, "35")
123 1875 $date = DateTime->new(year => $year, month => $month, day => 1);
124 1875 $delta = $nth * 7 - 1;
125 } else {
126 # For last etc. we want the last day of the month (minus a week if next-to-last, etc)
127 375 $date = DateTime->last_day_of_month(year => $year, month => $month);
128 375 $delta = 7 * ($nth + 1); # $nth is negative
129 }
130  
131 # Back up to the first $dow on or before $date + $delta
132 2250 $date->add(days => $delta - ($date->day_of_week + $delta - $dow) % 7);
133  
134 # If we're not in the right month, then that month doesn't have the specified date
135 2250 ▶ return (($date->month == $month) ? $date->day : undef);
Branch: 1 branch
BranchTrueFalseTotal
$date->month == $month ? : 2250 0 2250
136 }
137  
138 2 1;
139  
140 __END__
↑ Summary