blib/lib/Date/Holidays/BY.pm

filter: Statementstmt 99.2% 132 / 133 Branchbran 83.8% 52 / 62 Conditioncond 54.6% 41 / 75 MC/DCmcdc 30.5% 11 / 36 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 78.9% 263 / 333 CC 61 SCAR 20.0
CC
61
Cov
83%
holidays
12.3
is_business_day
10.8
_check_date
10.7
CRAP 7.4
↑ Summary
1 package Date::Holidays::BY;
2 8 our $VERSION = '3.2027.0'; # VERSION
3  
4 =encoding utf8
5  
6 =head1 NAME
7  
8 Date::Holidays::BY - Determine public holidays and business days in Belarus.
9  
10 =head1 SYNOPSIS
11  
12 use Date::Holidays::BY;
13  
14 my $holidays = Date::Holidays::BY::holidays( 2024 );
15 # {
16 # "0101" => "New Year",
17 # ...
18 # "1225" => "Christmas (Catholic Christmas)"
19 # }
20  
21 if ( my $holidayname = Date::Holidays::BY::is_holiday( 2007, 1, 1 ) ) {
22 print "Is a holiday: $holidayname\n";
23 }
24  
25 if ( Date::Holidays::BY::is_business_day( 2012, 3, 11 ) ) {
26 print "2012-03-11 is a business day on a weekend\n";
27 }
28  
29 if ( Date::Holidays::BY::is_short_business_day( 2015, 4, 30 ) ) {
30 print "2015-04-30 is a short business day\n";
31 }
32  
33 my $short_days = Date::Holidays::BY::short_business_days( 2024 );
34 # {
35 # "0307" => "Pre-holiday working day",
36 # ...
37 # "1231" => "Pre-holiday working day"
38 # }
39  
40 =cut
41  
42 =head1 DESCRIPTION
43  
44 Date::Holidays::BY provides functions to check if a given date is a public holiday in Belarus. This module follows the standard holiday calendar observed in Belarus, including both national holidays and specific religious observances recognized in the country.
45  
46 Imports nothing by default.
47  
48 =cut
49  
50 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 8 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 8 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 8 our @EXPORT_OK = qw(
58 is_holiday
59 is_by_holiday
60 holidays
61 is_business_day
62 is_short_business_day
63 short_business_days
64 );
65  
66  
67 =head1 CONFIGURATION VARIABLES
68  
69 =head2 $Date::Holidays::BY::ref
70  
71 Hash reference containing all static holiday data, including holiday names for i18n support.
72 Dates are formatted as MMDD.
73  
74 =cut
75  
76 8 our $ref = {
77  
78 HOLIDAYS_VALID_SINCE => 1992,
79  
80 HOLIDAYS => {
81 '0101' => {
82 name => {
83 be => 'Новы год',
84 en => 'New Year',
85 ru => 'Новый год',
86 },
87 days => {
88 1992 => [ qw( 0101 ) ],
89 2020 => [ qw( 0101 0102 ) ],
90 },
91 },
92 '0308' => {
93 name => {
94 be => 'Дзень жанчын',
95 en => 'Women\'s Day',
96 ru => 'День женщин',
97 },
98 days => [ qw( 0308 ) ],
99 },
100 '0501' => {
101 name => {
102 be => 'Свята працы',
103 en => 'Labor Day',
104 ru => 'Праздник труда',
105 },
106 days => [ qw( 0501 ) ],
107 },
108 '0509' => {
109 name => {
110 be => 'Дзень Перамогі',
111 en => 'Victory Day',
112 ru => 'День Победы',
113 },
114 days => [ qw( 0509 ) ],
115 },
116 '0703' => {
117 name => {
118 be => 'Дзень незалежнасці Рэспублікі Беларусь',
119 en => 'Independence Day of the Republic of Belarus',
120 ru => 'День Независимости Республики Беларусь',
121 },
122 days => {
123 1991 => [ qw( 0727 ) ],
124 1997 => [ qw( 0703 ) ],
125 }
126 },
127 '1107' => {
128 name => {
129 be => 'Дзень Кастрычніцкай рэвалюцыі',
130 en => 'October Revolution Day',
131 ru => 'День Октябрьской революции',
132 },
133 days => [ qw( 1107 ) ],
134 },
135 '0107' => {
136 name => {
137 be => 'Раство Хрыстова (праваслаўнае Раство)',
138 en => 'Christmas (Orthodox Christmas)',
139 ru => 'Рождество Христово (православное Рождество)',
140 },
141 days => [ qw( 0107 ) ],
142 },
143 '1225' => {
144 name => {
145 be => 'Раство Хрыстова (каталіцкае Раство)',
146 en => 'Christmas (Catholic Christmas)',
147 ru => 'Рождество Христово (католическое Рождество)',
148 },
149 days => [ qw( 1225 ) ],
150 },
151 'rado' => {
152 name => {
153 be => 'Радаўніца',
154 en => 'Radunica',
155 ru => 'Радуница',
156 },
157 days => \&_radonitsa_mmdd,
158 },
159 'spec' => {
160 name => {
161 be => 'Перанос працоўнага дня',
162 en => 'Postponed working day',
163 ru => 'Перенос рабочего дня',
164 },
165 days => {
166 # ... TODO
167 1992 => [ qw( 0102 0106 0504 ) ],
168 1993 => [ qw( 0726 ) ],
169 1994 => [ qw( 0307 ) ],
170 1995 => [ qw( 0728 1106 ) ],
171 1996 => [ qw( 0422 0510 1108 ) ],
172 1997 => [ qw( 0106 0505 0704 1226 ) ],
173 1998 => [ qw( 0102 0427 ) ],
174 1999 => [ qw( 0108 0419 ) ],
175 2000 => [ qw( 0508 1106 ) ],
176 2001 => [ qw( 0102 0309 0423 0430 0702 1224 1231 ) ],
177 2002 => [ qw( 0102 0510 1108 ) ],
178 2003 => [ qw( 0106 0505 ) ],
179 2004 => [ qw( 0102 0105 0106 ) ],
180 2005 => [ qw( 0307 ) ],
181 2006 => [ qw( 0102 0508 1106 ) ],
182 2007 => [ qw( 0102 0309 0416 0430 0702 1224 1231 ) ],
183 2008 => [ qw( 0102 0505 0704 1226 ) ],
184 2009 => [ qw( 0102 0427 ) ],
185 2010 => [ qw( 0108 0412 0510 ) ],
186 2011 => [ qw( 0307 0502 ) ],
187 2012 => [ qw( 1224 1231 ) ],
188 2013 => [ qw( 0102 0510 ) ],
189 2014 => [ qw( 0102 0106 0430 0704 1226 ) ],
190 2015 => [ qw( 0102 0420 ) ],
191 2016 => [ qw( 0108 0307 ) ],
192 2017 => [ qw( 0102 0424 0508 1106 ) ],
193 2018 => [ qw( 0102 0309 0416 0430 0702 1224 1231 ) ],
194 2019 => [ qw( 0506 0508 1108 ) ],
195 2020 => [ qw( 0106 0427 ) ],
196 2021 => [ qw( 0108 0510 ) ],
197 2022 => [ qw( 0307 0502 ) ],
198 2023 => [ qw( 0424 0508 1106 ) ],
199 2024 => [ qw( 0513 1108 ) ],
200 2025 => [ qw( 0106 0428 0704 1226 ) ],
201 2026 => [ qw( 0420 ) ],
202 2027 => [ qw( 0510 ) ],
203 },
204 },
205 },
206  
207 BUSINESS_DAYS_ON_WEEKENDS => {
208 'all' => {
209 name => {
210 be => 'Працоўны дзень у выхадныя дні',
211 en => 'Working day on weekends',
212 ru => 'Рабочий день в выходные дни',
213 },
214 days => {
215 # ... TODO
216 1991 => [ qw( 1228 ) ],
217 1992 => [ qw( 0104 0516 ) ],
218 1993 => [ qw( 0724 ) ],
219 1994 => [ qw( 0305 ) ],
220 1995 => [ qw( 0805 1104 ) ],
221 1996 => [ qw( 0420 0504 1116 ) ],
222 1997 => [ qw( 0104 0503 0712 1220 ) ],
223 1998 => [ qw( 0110 0425 ) ],
224 1999 => [ qw( 0116 0417 ) ],
225 2000 => [ qw( 0513 1111 ) ],
226 2001 => [ qw( 0120 0303 0421 0428 0707 1222 1229 ) ],
227 2002 => [ qw( 0105 0518 1116 ) ],
228 2003 => [ qw( 0104 0503 ) ],
229 2004 => [ qw( 0110 0117 0131 ) ],
230 2005 => [ qw( 0312 ) ],
231 2006 => [ qw( 0128 0506 1104 1230 ) ],
232 2007 => [ qw( 0317 0414 0505 0707 1222 1229 ) ],
233 2008 => [ qw( 0112 0503 0628 1220 ) ],
234 2009 => [ qw( 0110 0425 ) ],
235 2010 => [ qw( 0123 0417 0515 ) ],
236 2011 => [ qw( 0312 0514 ) ],
237 2012 => [ qw( 1222 1229 ) ],
238 2013 => [ qw( 0105 0518 ) ],
239 2014 => [ qw( 0104 0111 0503 0712 1220 ) ],
240 2015 => [ qw( 0110 0425 ) ],
241 2016 => [ qw( 0116 0305 ) ],
242 2017 => [ qw( 0121 0429 0506 1104 ) ],
243 2018 => [ qw( 0120 0303 0414 0428 0707 1222 1229 ) ],
244 2019 => [ qw( 0504 0511 1116 ) ],
245 2020 => [ qw( 0104 0404 ) ],
246 2021 => [ qw( 0116 0515 ) ],
247 2022 => [ qw( 0312 0514 ) ],
248 2023 => [ qw( 0429 0513 1111 ) ],
249 2024 => [ qw( 0518 1116 ) ],
250 2025 => [ qw( 0111 0426 0712 1220 ) ],
251 2026 => [ qw( 0425 ) ],
252 2027 => [ qw( 0508 ) ],
253 },
254 },
255 },
256  
257  
258 SHORT_BUSINESS_DAYS => {
259 'all' => {
260 name => {
261 be => 'Перадсвяточны працоўны дзень',
262 en => 'Pre-holiday working day',
263 ru => 'Предпраздничный рабочий день',
264 },
265 },
266 },
267  
268 };
269  
270 28 ▶sub _radonitsa_mmdd {
Subroutine _radonitsa_mmdd: called CC=5 SCAR=16.7
271 28 my $year=$_[0];
272 0 ▶ if ($year < 1583) {croak "Module has limitation in counting Easter before 1583";}
Branch: 1 branch
BranchTrueFalseTotal
if ($year < 1583) 0 28 28
273 # Radonitsa is the Tuesday of the second week after Orthodox Easter: Easter + 9 days.
274 # Orthodox Easter is calculated by the Meeus algorithm in the Julian calendar,
275 # all dates are counted as the day number from March 1 (1 = March 1, 32 = April 1, 62 = May 1, 93 = June 1).
276  
277 # Paschal full moon: its date is March 21 (Julian) + $d days ($d is 0..29, depends on the year in the 19-year lunar cycle).
278 28 my $d = (19 * ($year % 19) + 15) % 30;
279  
280 # Days from the day after the full moon to the next Sunday ($e is 0..6, depends on the year in the 4-year leap cycle and 7-day week cycle).
281 28 my $e = (2 * ($year % 4) + 4 * ($year % 7) - $d + 34) % 7;
282  
283 # Orthodox Easter in the Julian calendar: March 22 + $d + $e days,
284 # i.e. day number 22 + $d + $e from March 1 (March 22 .. April 25, can be > 31 = April).
285 28 my $julian_easter = 22 + $d + $e;
286  
287 # The Julian calendar is behind the Gregorian one: 13 days in 1900-2099, 14 in 2100-2199 and so on.
288 28 my $gregorian_shift = int($year / 100) - int($year / 400) - 2;
289  
290 # Radonitsa in the Gregorian calendar as the day number from March 1
291 # (Easter in Julian calendar + shift between calendars + 9 days).
292 28 my $n = $julian_easter + $gregorian_shift + 9;
293  
294 # Day number from March 1 to month and day (March has 31 days, April 30, May 31).
295 28 ▶ return [ $n <= 31 ? sprintf('03%02d', $n)
Branch: 3 branches
BranchTrueFalseTotal
$n <= 31 ? : 0 28 28
$n <= 61 ? : 13 15 28
$n <= 92 ? : 15 0 15
296 : $n <= 61 ? sprintf('04%02d', $n - 31)
297 : $n <= 92 ? sprintf('05%02d', $n - 61)
298 : sprintf('06%02d', $n - 92) ];
299 }
300  
301  
302 =head3 HOLIDAYS_VALID_SINCE
303  
304 C<< $Date::Holidays::BY::ref->{'HOLIDAYS_VALID_SINCE'} >>
305  
306 The module is only relevant from this year onward. Throws an exception (croak) for dates before this year.
307  
308 =head3 INACCURATE_TIMES_BEFORE - INACCURATE_TIMES_SINCE
309  
310 C<< $Date::Holidays::BY::ref->{'INACCURATE_TIMES_BEFORE'} >>
311  
312 C<< $Date::Holidays::BY::ref->{'INACCURATE_TIMES_SINCE'} >>
313  
314 The regular public holidays are defined for all years, but the exact postponements of days off are known only within this period. Outside it, in strict mode (C<$Date::Holidays::BY::strict=1>) an exception is thrown (croak).
315  
316 =cut
317  
318 8 $ref->{'INACCURATE_TIMES_BEFORE'} = (sort keys %{$ref->{'HOLIDAYS'}->{'spec'}->{'days'}})[0];
319 8 $ref->{'INACCURATE_TIMES_SINCE'} = (reverse sort keys %{$ref->{'HOLIDAYS'}->{'spec'}->{'days'}})[0] + 1;
320  
321  
322 =head2 $Date::Holidays::BY::strict
323  
324 If true, throws an exception (croak) when the requested year is outside the period for which the data is defined.
325 Default is 0.
326  
327 =cut
328  
329 8 our $strict = 0;
330  
331  
332 =head2 $Date::Holidays::BY::lang
333  
334 The language is determined from the locale in C<$ENV{LC_ALL}>, C<$ENV{LC_MESSAGES}> or C<$ENV{LANG}> (in this order of priority). You can override it after loading the module.
335  
336 =cut
337  
338 8 ▶my $envlang = lc substr(( $ENV{LC_ALL} || $ENV{LC_MESSAGES} || $ENV{LANG} || '' ), 0, 2);
Condition37%3/8
$ENV{'LC_ALL'} || $ENV{'LC_MESSAGES'} || $ENV{'LANG'} || ''
l!l
08
$ENV{"LC_ALL"} or $ENV{"LC_MESSAGES"} or $ENV{"LANG"}
l!l&&r!l&&!r
008
$ENV{"LC_ALL"} or $ENV{"LC_MESSAGES"}
l!l&&r!l&&!r
008
MC/DC0%0/3
$ENV{'LC_ALL'} || $ENV{'LC_MESSAGES'} || $ENV{'LANG'} || ''
$ENV{"LC_ALL"} $ENV{"LC_MESSAGES"} $ENV{"LANG"}
Truth table25%1/4
A or B or C
A= $ENV{"LC_ALL"}B= $ENV{"LC_MESSAGES"}C= $ENV{"LANG"}
ABCresult
1XX1
01X1
0011
0000
339 24 ▶$envlang = (List::Util::first {/^\Q$envlang\E$/} qw(be en ru)) || 'en';
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&List::Util::first(sub { /^\Q$envlang\E$/; } , 'be', 'en', 'ru') || 'en'
l!l
08
MC/DC0%0/1
&List::Util::first(sub { /^\Q$envlang\E$/; } , 'be', 'en', 'ru') || 'en'
&List::Util::first(sub { /^\Q$envlang\E$/; } , 'be', 'en', 'ru')
Truth table50%1/2
A
A= &List::Util::first(sub { /^\Q$envlang\E$/; } , 'be', 'en', 'ru')
Aresult
11
00
340 8 ▶our $lang = $lang || $envlang;
Condition33%1/3
$Date::Holidays::BY::lang || $envlang
l!l&&r!l&&!r
080
MC/DC0%0/2
$Date::Holidays::BY::lang || $envlang
$Date::Holidays::BY::lang $envlang
Truth table33%1/3
A || B
A= $Date::Holidays::BY::langB= $envlang
ABresult
1X1
011
000
341  
342  
343 =head1 FUNCTIONS
344  
345 =head2 holidays( $year )
346  
347 Returns a hash reference of all holidays in the year.
348  
349 {
350 MMDD => 'name',
351 ...
352 }
353  
354 The result is cached for the selected language and year in C<$Date::Holidays::BY::ref-E<gt>{'cache'}>.
355  
356 The returned hash reference is the cache itself, not a copy. Changes made to it (for example, adding your own days) are visible to C<is_holiday()> and C<is_business_day()> until the cache is cleared (see L</I18N>).
357  
358 Throws an exception (croak) if the year is not valid.
359  
360 =cut
361  
362 65 ▶sub holidays {
Subroutine holidays: called CC=12 SCAR=25.1
Pod documented
363 65 ▶ my $year = shift or croak 'Bad year';
Branch: 1 branch
BranchTrueFalseTotal
my $year = shift() or croak("Bad year") 65 0 65
364  
365 65 ▶ return $ref->{'cache'}->{'HOLIDAYS'}->{$lang}->{$year} if $ref->{'cache'}->{'HOLIDAYS'}->{$lang}->{$year};
Branch: 1 branch
BranchTrueFalseTotal
if $Date::Holidays::BY::ref->{"cache"}{"HOLIDAYS"}{$Date::Holidays::BY::lang}{$year} 44 21 65
366  
367 21 ▶ croak "BY holidays is not valid before $ref->{'HOLIDAYS_VALID_SINCE'}" if $year < $ref->{'HOLIDAYS_VALID_SINCE'};
Branch: 1 branch
BranchTrueFalseTotal
if $year < $Date::Holidays::BY::ref->{"HOLIDAYS_VALID_SINCE"} 3 18 21
368 18 ▶ if ($strict && ($year < $ref->{'INACCURATE_TIMES_BEFORE'} || $year >= $ref->{'INACCURATE_TIMES_SINCE'} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Date::Holidays::BY::strict and $year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"} || $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}) 1 17 18
Condition50%3/6
$Date::Holidays::BY::strict and $year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"} || $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}
!ll&&!rl&&r
1701
$year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"} or $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}
l!l&&r!l&&!r
010
MC/DC33%1/3
$Date::Holidays::BY::strict and $year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"} || $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}
$Date::Holidays::BY::strict $year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"} $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}
Truth table50%2/4
A and B or C
A= $Date::Holidays::BY::strictB= $year < $Date::Holidays::BY::ref->{"INACCURATE_TIMES_BEFORE"}C= $year >= $Date::Holidays::BY::ref->{"INACCURATE_TIMES_SINCE"}
ABCresult
0XX0
1000
11X1
1011
369 1 croak "BY holidays are not valid outside the period @{[ $ref->{'INACCURATE_TIMES_BEFORE'} ]}-@{[ $ref->{'INACCURATE_TIMES_SINCE'} - 1 ]}";
370 }
371  
372 17 for my $key (keys %{$ref->{'HOLIDAYS'}}) {
373  
374 170 ▶ my $name = _resolve_yhash_value( $ref->{'HOLIDAYS'}->{$key}->{'name'}, $year )->{$lang} || croak "Name is not defined";
Condition50%1/2
_resolve_yhash_value($Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'name'}, $year)->{$Date::Holidays::BY::lang} || croak('Name is not defined')
l!l
1700
MC/DC0%0/1
_resolve_yhash_value($Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'name'}, $year)->{$Date::Holidays::BY::lang} || croak('Name is not defined')
_resolve_yhash_value($Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'name'}, $year)->{$Date::Holidays::BY::lang}
Truth table50%1/2
A
A= _resolve_yhash_value($Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'name'}, $year)->{$Date::Holidays::BY::lang}
Aresult
11
00
375  
376 # postponed days are defined only for the exact years, unlike periodic holidays
377 170 my $mds;
378 170 ▶ if ($key eq 'spec') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'spec') { } 17 153 170
379 17 ▶ $mds = $ref->{'HOLIDAYS'}->{$key}->{'days'}->{$year} || [];
Condition100%2/2
$Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'days'}{$year} || []
l!l
161
MC/DC100%1/1
$Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'days'}{$year} || []
$Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'days'}{$year}
Truth table100%2/2
A
A= $Date::Holidays::BY::ref->{'HOLIDAYS'}{$key}{'days'}{$year}
Aresult
11
00
380 } else {
381 153 $mds = _resolve_yhash_value( $ref->{'HOLIDAYS'}->{$key}->{'days'}, $year );
382 }
383  
384 170 for my $md (@{$mds}) {
385 207 $ref->{'cache'}->{'HOLIDAYS'}->{$lang}->{$year}->{$md} = $name;
386 }
387  
388 }
389  
390 17 return $ref->{'cache'}->{'HOLIDAYS'}->{$lang}->{$year};
391 }
392  
393 395 ▶sub _resolve_yhash_value {
Subroutine _resolve_yhash_value: called CC=6 SCAR=18.0
394 395 my ($value, $year) = @_;
395 395 ▶ return $value->($year) if ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "CODE" 25 370 395
396 370 ▶ return $value if ref $value ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "HASH" 150 220 370
397 220 my @keys = keys %{$value};
398 220 ▶ return $value if $keys[0] !~ /^\d\d\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $keys[0] =~ /^\d\d\d\d$/ 170 50 220
399 50 ▶ my $ykey = List::Util::first { $year >= $_ } reverse sort @keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
400 50 ▶ return [] if !$ykey;
Branch: 1 branch
BranchTrueFalseTotal
unless $ykey 0 50 50
401 50 ▶ return $value->{$ykey}->($year) if ref $value->{$ykey} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value->{$ykey} eq "CODE" 0 50 50
402 50 return $value->{$ykey};
403 }
404  
405  
406 =head2 is_holiday( $year, $month, $day )
407  
408 Determines whether the date is a public holiday in Belarus. Returns the holiday name or undef.
409  
410 Throws an exception (croak) if the date is invalid.
411  
412 =cut
413  
414 67 ▶sub is_holiday {
Subroutine is_holiday: called CC=4 SCAR=13.9
Pod documented
415 67 my ( $year, $month, $day ) = @_;
416 67 ▶ croak 'Bad params' unless $year && $month && $day;
Branch: 1 branch
BranchTrueFalseTotal
unless $year and $month and $day 2 65 67
Condition83%5/6
$year and $month and $day
!ll&&!rl&&r
2065
$year and $month
!ll&&!rl&&r
1165
MC/DC66%2/3
$year and $month and $day
$year $month $day
Truth table75%3/4
A and B and C
A= $yearB= $monthC= $day
ABCresult
0XX0
10X0
1100
1111
417 65 _check_date( $year, $month, $day );
418  
419 61 return holidays( $year )->{ _get_date_key($month, $day) };
420 }
421  
422  
423 =head2 is_by_holiday( $year, $month, $day )
424  
425 Alias for is_holiday().
426  
427 =cut
428  
429 1 ▶sub is_by_holiday {
Subroutine is_by_holiday: called CC=1 SCAR=0.0
Pod documented
430 1 goto &is_holiday;
431 }
432  
433  
434 =head2 is_business_day( $year, $month, $day )
435  
436 Returns true if the date is a business day in Belarus, taking holidays, weekends and working days on weekends into account.
437  
438 Throws an exception (croak) if the date is invalid.
439  
440 =cut
441  
442 49 ▶sub is_business_day {
Subroutine is_business_day: called CC=10 SCAR=23.8
Pod documented
443 49 my ( $year, $month, $day ) = @_;
444 49 ▶ croak 'Bad params' unless $year && $month && $day;
Branch: 1 branch
BranchTrueFalseTotal
unless $year and $month and $day 0 49 49
Condition33%2/6
$year and $month and $day
!ll&&!rl&&r
0049
$year and $month
!ll&&!rl&&r
0049
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
445 49 _check_date( $year, $month, $day );
446  
447 45 ▶ return 0 if is_holiday( $year, $month, $day );
Branch: 1 branch
BranchTrueFalseTotal
if is_holiday($year, $month, $day) 8 37 45
448  
449 # check if date is a weekend
450 37 my $wday = _day_of_week( $year, $month, $day );
451 37 ▶ return 1 unless $wday == 0 || $wday == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless $wday == 0 or $wday == 6 25 12 37
Condition100%3/3
$wday == 0 or $wday == 6
l!l&&r!l&&!r
5725
MC/DC100%2/2
$wday == 0 or $wday == 6
$wday == 0 $wday == 6
Truth table100%3/3
A or B
A= $wday == 0B= $wday == 6
ABresult
1X1
011
000
452  
453 # check if date is a business day on weekend
454 # working days on weekends are defined only for the exact years
455 12 ▶ for my $md (@{$ref->{'BUSINESS_DAYS_ON_WEEKENDS'}->{'all'}->{'days'}->{$year} || []}) {
Condition50%1/2
$Date::Holidays::BY::ref->{'BUSINESS_DAYS_ON_WEEKENDS'}{'all'}{'days'}{$year} || []
l!l
120
MC/DC0%0/1
$Date::Holidays::BY::ref->{'BUSINESS_DAYS_ON_WEEKENDS'}{'all'}{'days'}{$year} || []
$Date::Holidays::BY::ref->{'BUSINESS_DAYS_ON_WEEKENDS'}{'all'}{'days'}{$year}
Truth table50%1/2
A
A= $Date::Holidays::BY::ref->{'BUSINESS_DAYS_ON_WEEKENDS'}{'all'}{'days'}{$year}
Aresult
11
00
456 37 ▶ if ($md eq _get_date_key($month, $day)) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($md eq _get_date_key($month, $day)) 1 36 37
457 }
458  
459 11 return 0;
460 }
461  
462  
463 =head2 short_business_days( $year )
464  
465 Returns hash ref of all shortened business days in the year.
466  
467 {
468 MMDD => 'name',
469 ...
470 }
471  
472 A shortened business day is a business day immediately preceding a public holiday (Labor Code of the Republic of Belarus, art. 116), its length is reduced by one hour. The days are calculated by this rule.
473  
474 The result is cached in C<$Date::Holidays::BY::ref-E<gt>{'cache'}> like in C<holidays()>.
475  
476 =cut
477  
478 10 ▶sub short_business_days {
Subroutine short_business_days: called CC=5 SCAR=16.4
Pod documented
479 10 ▶ my $year = shift or croak 'Bad year';
Branch: 1 branch
BranchTrueFalseTotal
my $year = shift() or croak("Bad year") 10 0 10
480  
481 10 ▶ return $ref->{'cache'}->{'SHORT_BUSINESS_DAYS'}->{$lang}->{$year} if $ref->{'cache'}->{'SHORT_BUSINESS_DAYS'}->{$lang}->{$year};
Branch: 1 branch
BranchTrueFalseTotal
if $Date::Holidays::BY::ref->{"cache"}{"SHORT_BUSINESS_DAYS"}{$Date::Holidays::BY::lang}{$year} 6 4 10
482  
483 4 ▶ croak "BY holidays is not valid before $ref->{'HOLIDAYS_VALID_SINCE'}" if $year < $ref->{'HOLIDAYS_VALID_SINCE'};
Branch: 1 branch
BranchTrueFalseTotal
if $year < $Date::Holidays::BY::ref->{"HOLIDAYS_VALID_SINCE"} 0 4 4
484  
485 4 my $mds = _calc_short_business_days( $year );
486 4 ▶ my $name = $ref->{'SHORT_BUSINESS_DAYS'}->{'all'}->{'name'}->{$lang} || croak "Name is not defined";
Condition50%1/2
$Date::Holidays::BY::ref->{'SHORT_BUSINESS_DAYS'}{'all'}{'name'}{$Date::Holidays::BY::lang} || croak('Name is not defined')
l!l
40
MC/DC0%0/1
$Date::Holidays::BY::ref->{'SHORT_BUSINESS_DAYS'}{'all'}{'name'}{$Date::Holidays::BY::lang} || croak('Name is not defined')
$Date::Holidays::BY::ref->{'SHORT_BUSINESS_DAYS'}{'all'}{'name'}{$Date::Holidays::BY::lang}
Truth table50%1/2
A
A= $Date::Holidays::BY::ref->{'SHORT_BUSINESS_DAYS'}{'all'}{'name'}{$Date::Holidays::BY::lang}
Aresult
11
00
487  
488 4 return $ref->{'cache'}->{'SHORT_BUSINESS_DAYS'}->{$lang}->{$year} = { map { $_ => $name } @{$mds} };
489 }
490  
491 # Business days immediately preceding a public holiday (without postponed days).
492 4 ▶sub _calc_short_business_days {
Subroutine _calc_short_business_days: called CC=9 SCAR=22.0
493 4 my $year = shift;
494  
495 4 my %holiday;
496 4 for my $y ( $year, $year + 1 ) {
497 8 for my $key ( grep { $_ ne 'spec' } keys %{$ref->{'HOLIDAYS'}} ) {
498 72 $holiday{$y . $_} = 1 for @{_resolve_yhash_value( $ref->{'HOLIDAYS'}->{$key}->{'days'}, $y )};
499 }
500 }
501  
502 4 my @mds;
503 4 for my $date ( keys %holiday ) {
504 76 my ( $y, $m, $d ) = unpack 'A4 A2 A2', $date;
505 # the day before the holiday, for January 1 it is December 31 of the previous year
506 76 ▶ if ( $d > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d > 1) { } 60 16 76
elsif ($m > 1) { } 8 8 16
507 60 $d--;
508 } elsif ( $m > 1 ) {
509 8 $m--;
510 8 $d = _days_in_month( $y, $m );
511 } else {
512 8 $y--;
513 8 $m = 12;
514 8 $d = 31;
515 }
516 76 ▶ next unless $y == $year && is_business_day( $year, $m, $d );
Branch: 1 branch
BranchTrueFalseTotal
unless $y == $year and is_business_day($year, $m, $d) 53 23 76
Condition100%3/3
$y == $year and is_business_day($year, $m, $d)
!ll&&!rl&&r
381523
MC/DC100%2/2
$y == $year and is_business_day($year, $m, $d)
$y == $year is_business_day($year, $m, $d)
Truth table100%3/3
A and B
A= $y == $yearB= is_business_day($year, $m, $d)
ABresult
0X0
100
111
517 23 push @mds, _get_date_key( $m, $d );
518 }
519 4 @mds = sort @mds;
520  
521 4 return \@mds;
522 }
523  
524  
525 =head2 is_short_business_day( $year, $month, $day )
526  
527 Returns true if date is a shortened business day. See C<short_business_days()>.
528  
529 Throws an exception (croak) if the date is invalid.
530  
531 =cut
532  
533 13 ▶sub is_short_business_day {
Subroutine is_short_business_day: called CC=5 SCAR=18.1
Pod documented
534 13 my ( $year, $month, $day ) = @_;
535 13 ▶ croak 'Bad params' unless $year && $month && $day;
Branch: 1 branch
BranchTrueFalseTotal
unless $year and $month and $day 0 13 13
Condition33%2/6
$year and $month and $day
!ll&&!rl&&r
0013
$year and $month
!ll&&!rl&&r
0013
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
536 13 _check_date( $year, $month, $day );
537  
538 9 ▶ return short_business_days( $year )->{ _get_date_key($month, $day) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
short_business_days($year)->{_get_date_key($month, $day)} ? : 3 6 9
539 }
540  
541  
542 132 ▶sub _days_in_month {
Subroutine _days_in_month: called CC=5 SCAR=16.5
543 132 my ( $year, $month ) = @_;
544 132 my @mdays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
545 132 ▶ return 29 if $month == 2 && $year % 4 == 0 && ( $year % 100 != 0 || $year % 400 == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $month == 2 and $year % 4 == 0 and $year % 100 != 0 || $year % 400 == 0 11 121 132
Condition66%6/9
$month == 2 and $year % 4 == 0 and $year % 100 != 0 || $year % 400 == 0
!ll&&!rl&&r
121011
$month == 2 and $year % 4 == 0
!ll&&!rl&&r
118311
$year % 100 != 0 or $year % 400 == 0
l!l&&r!l&&!r
1100
MC/DC50%2/4
$month == 2 and $year % 4 == 0 and $year % 100 != 0 || $year % 400 == 0
$month == 2 $year % 4 == 0 $year % 100 != 0 $year % 400 == 0
Truth table60%3/5
A and B and C or D
A= $month == 2B= $year % 4 == 0C= $year % 100 != 0D= $year % 400 == 0
ABCDresult
0XXX0
10XX0
11000
111X1
11011
546 121 return $mdays[ $month - 1 ];
547 }
548  
549  
550 127 ▶sub _check_date {
Subroutine _check_date: called CC=8 SCAR=23.7
551 127 my ( $year, $month, $day ) = @_;
552 127 ▶ croak "Bad date: $year-$month-$day"
Branch: 1 branch
BranchTrueFalseTotal
unless $year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/ and $month >= 1 and $month <= 12 and $day >= 1 3 124 127
Condition46%7/15
$year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/ and $month >= 1 and $month <= 12 and $day >= 1
!ll&&!rl&&r
30124
$year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/ and $month >= 1 and $month <= 12
!ll&&!rl&&r
03124
$year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/ and $month >= 1
!ll&&!rl&&r
00127
$year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/
!ll&&!rl&&r
00127
$year =~ /^\d+$/ and $month =~ /^\d+$/
!ll&&!rl&&r
00127
MC/DC16%1/6
$year =~ /^\d+$/ and $month =~ /^\d+$/ and $day =~ /^\d+$/ and $month >= 1 and $month <= 12 and $day >= 1
$year =~ /^\d+$/ $month =~ /^\d+$/ $day =~ /^\d+$/ $month >= 1 $month <= 12 $day >= 1
Truth table28%2/7
A and B and C and D and E and F
A= $year =~ /^\d+$/B= $month =~ /^\d+$/C= $day =~ /^\d+$/D= $month >= 1E= $month <= 12F= $day >= 1
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
553 unless $year =~ /^\d+$/ && $month =~ /^\d+$/ && $day =~ /^\d+$/
554 && $month >= 1 && $month <= 12 && $day >= 1;
555 124 ▶ croak "Bad date: $year-$month-$day" if $day > _days_in_month( $year, $month );
Branch: 1 branch
BranchTrueFalseTotal
if $day > _days_in_month($year, $month) 9 115 124
556 115 return;
557 }
558  
559  
560 # Day of the week Sakamoto's algorithm: 0 = Sunday, ..., 6 = Saturday (Gregorian calendar).
561 37 ▶sub _day_of_week {
Subroutine _day_of_week: called CC=2 SCAR=6.9
562 37 my ( $year, $month, $day ) = @_;
563 37 my @shift = ( 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 );
564 37 ▶ $year-- if $month < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 3 8 29 37
565 37 return ( $year + int( $year / 4 ) - int( $year / 100 ) + int( $year / 400 ) + $shift[ $month - 1 ] + $day ) % 7;
566 }
567  
568  
569 126 ▶sub _get_date_key {
Subroutine _get_date_key: called CC=1 SCAR=0.0
570 126 my ($month, $day) = @_;
571 126 return sprintf '%02d%02d', $month, $day;
572 }
573  
574  
575 =head1 I18N
576  
577 Translations are available in Belarusian (be), English (en), and Russian (ru), with the language selected from the locale in C<$ENV{LC_ALL}>, C<$ENV{LC_MESSAGES}> or C<$ENV{LANG}> (in this order of priority). If the locale is not one of these, "en" is used. See C<$Date::Holidays::BY::lang>.
578  
579 Holiday names can be redefined in C<$Date::Holidays::BY::ref>:
580  
581 use Date::Holidays::BY;
582  
583 $Date::Holidays::BY::ref->{'HOLIDAYS'}->{'0308'}->{'name'}->{'en'} = 'name1';
584  
585 say Date::Holidays::BY::holidays(2024)->{'0308'}; # name1
586  
587 $Date::Holidays::BY::ref->{'HOLIDAYS'}->{'0308'}->{'name'}->{'en'} = 'name2';
588 $Date::Holidays::BY::ref->{'cache'} = undef;
589  
590 say Date::Holidays::BY::holidays(2024)->{'0308'}; # name2
591  
592 =cut
593  
594 =head1 LICENSE
595  
596 This software is copyright (c) 2026 by Vladimir Varlamov.
597  
598 This is free software; you can redistribute it and/or modify it under
599 the same terms as the Perl 5 programming language system itself.
600  
601 Terms of the Perl programming language system itself
602  
603 a) the GNU General Public License as published by the Free
604 Software Foundation; either version 1, or (at your option) any
605 later version, or
606 b) the "Artistic License"
607  
608 =cut
609  
610  
611 =head1 AUTHOR
612  
613 Vladimir Varlamov, C<< <bes.internal@gmail.com> >>
614  
615 =cut
616  
617  
618  
619 8 1;
↑ Summary