File Coverage

blib/lib/Date/Holidays/USA.pm
Criterion Covered Total %
statement 51 53 96.2
branch 8 12 66.6
condition 2 6 33.3
subroutine 11 11 100.0
pod 4 4 100.0
total 76 86 88.3


line stmt bran cond sub pod time code
1             package Date::Holidays::USA;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: DEPRECATED - Provides United States of America holidays
5              
6 2     2   811327 use warnings;
  2         3  
  2         153  
7 2     2   11 use strict;
  2         3  
  2         51  
8              
9 2     2   1070 use utf8;
  2         498  
  2         11  
10 2     2   1263 use Date::Easter qw(easter);
  2         6886  
  2         240  
11 2     2   2543 use DateTime ();
  2         1481896  
  2         138  
12 2     2   25 use Exporter qw(import);
  2         4  
  2         1868  
13              
14             our @EXPORT = qw(is_holiday holidays);
15              
16             our $VERSION = '0.0208_1';
17              
18              
19             sub new {
20 1     1 1 921 my $self = shift;
21 1         3 bless \$self => $self;
22             }
23              
24              
25             sub is_holiday {
26 372     372 1 24992 my ($self, $year, $month, $day) = @_;
27 372 50 33     3073 return undef unless $year && $month && $day;
      33        
28 372         1100 my $holidays = $self->holidays($year);
29 372         1438 my $str = sprintf '%02d%02d', $month, $day;
30 372 100       3619 return $holidays->{$str} ? $holidays->{$str} : undef;
31             }
32              
33              
34             sub us_holidays {
35 375     375 1 10831 my ($self, $year) = @_;
36 375 50       1032 unless ($year) {
37 0         0 $year = (localtime)[5];
38 0         0 $year += 1900;
39             }
40 375         1061 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         18484 $dom{thanksgiving} => 'Thanksgiving',
87             },
88             12 => {
89             24 => 'Christmas Eve',
90             25 => 'Christmas',
91             31 => "New Year's Eve",
92             },
93             );
94 375         2157 my ($month, $day) = easter($year);
95 375         12044 $holidays{$month}->{$day} = 'Easter';
96 375         1752 return \%holidays;
97             }
98              
99              
100             sub holidays {
101 373     373 1 3023 my ($self, $year) = @_;
102 373         1353 my $holidays = $self->us_holidays($year);
103 373         771 my %rtn;
104 373         3019 for my $month (sort { $a <=> $b } keys %$holidays) {
  11662         18935  
105 4476         6550 for my $day (sort { $a <=> $b } keys %{ $holidays->{$month} }) {
  4844         10365  
  4476         12475  
106             $rtn{ sprintf '%02d%02d', $month, $day } = $holidays->{$month}->{$day}
107 7833 50       31261 if $holidays->{$month}->{$day};
108             }
109             }
110 373         4502 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             sub _nth_day_of_month {
118 2250     2250   32392 my ($nth, $dow, $year, $month) = @_;
119              
120 2250         4421 my ($date, $delta);
121 2250 100       4968 if ($nth > 0) {
122             # For 1st etc. we want the last day of that week (i.e. 7, 14, 21, 28, "35")
123 1875         5661 $date = DateTime->new(year => $year, month => $month, day => 1);
124 1875         683431 $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         1761 $date = DateTime->last_day_of_month(year => $year, month => $month);
128 375         148628 $delta = 7 * ($nth + 1); # $nth is negative
129             }
130              
131             # Back up to the first $dow on or before $date + $delta
132 2250         6955 $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 50       2663264 return (($date->month == $month) ? $date->day : undef);
136             }
137              
138             1;
139              
140             __END__
141              
142             =pod
143              
144             =encoding utf-8
145              
146             =head1 NAME
147              
148             Date::Holidays::USA - DEPRECATED - Provides United States of America holidays
149              
150             =head1 VERSION
151              
152             version 0.0208_1
153              
154             =head1 SYNOPSIS
155              
156             # Using with the Date::Holidays module:
157             use Date::Holidays ();
158             my $dh = Date::Holidays->new(countrycode => 'USA', nocheck => 1);
159             print $dh->is_holiday(year => 2024, month => 1, day => 1), "\n";
160             my $h = $dh->holidays;
161              
162             # Using the Date::Holidays::USA module directly:
163             use Date::Holidays::USA ();
164             $dh = Date::Holidays::USA->new;
165             print $dh->is_holiday(2024, 1, 1), "\n";
166             $h = $dh->holidays;
167             $h = $dh->us_holidays(2032);
168              
169             =head1 DESCRIPTION
170              
171             C<Date::Holidays::USA> is a now deprecated module that provided United
172             States of America holidays.
173              
174             =head1 METHODS
175              
176             =head2 new
177              
178             $dh = Date::Holidays::USA->new;
179              
180             Return a new C<Date::Holidays::USA> object.
181              
182             =head2 is_holiday
183              
184             $holiday = $dh->is_holiday($year, $month, $day);
185              
186             Takes three arguments:
187              
188             year: four digits
189             month: between 1-12
190             day: between 1-31
191              
192             Returns the name of the holiday, if one exists on that day.
193              
194             =head2 us_holidays
195              
196             $holidays = $dh->us_holidays;
197             $holidays = $dh->us_holidays($year);
198              
199             Returns a hash reference of holiday names, where the keys are by month
200             and day.
201              
202             =head2 holidays
203              
204             $holidays = $dh->holidays;
205             $holidays = $dh->holidays($year);
206              
207             Returns a hash reference of holiday names, where the keys are 4 digit
208             strings month and day.
209              
210             =head1 SEE ALSO
211              
212             L<Date::Holidays>
213              
214             L<Date::Holidays::Adapter>
215              
216             L<Date::Holidays::Adapter::USA>
217              
218             =head1 AUTHOR
219              
220             Gene Boggs <gene@cpan.org>
221              
222             =head1 COPYRIGHT AND LICENSE
223              
224             This software is Copyright (c) 2024 by Gene Boggs.
225              
226             This is free software, licensed under:
227              
228             The Artistic License 2.0 (GPL Compatible)
229              
230             =cut