Date-Holidays-UA
view release on metacpan or search on metacpan
# procedural approach
use Date::Holidays::UA qw(:all);
my ($year, $month, $day) = (localtime)[5, 4, 3];
$year += 1900;
$month += 1;
print 'Holiday!' if is_holiday($year, $month, $day);
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'};
# object-oriented approach
use DateTime;
use Date::Holidays::UA;
my $ua = Date::Holidays::UA->new({ language => 'en' });
print 'Holiday!' if $ua->is_holiday_dt(DateTime->today);
my $calendar = $ua->holidays(DateTime->today->year);
print join("\n", value(%$calendar)); # list of holiday names for Ukraine
SUBROUTINES/METHODS
new()
Create a new Date::Holidays::UA object. Parameters should be given as a
hashref of key-value pairs.
my $ua = Date::Holidays::UA->new();
specified date if there is one; undef if there isn't.
print $ua->is_ua_holiday(2020, 1, 1); # "New Year"
holidays()
For the given year, return a hashref containing all the holidays for
that year. The keys are the date of the holiday in mmdd format (eg
'1225' for December 25); the values are the holiday names.
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'}; # "Independence Day"
my $calendar = $ua->holidays($year);
print $calendar->{'0628'}; # "Constitution Day"
ua_holidays()
Same as holidays().
holidays_dt()
Similar to holidays(), The keys are the date of the holiday in mmdd
format (eg '1225' for December 25); and DateTime objects as the values.
my $calendar = $ua->holidays_dt($year);
AUTHOR
Denis Boyun, <denisboyun at gmail.com>
BUGS
Please report any bugs or feature requests to bug-date-holidays-ua at
rt.cpan.org, or through the web interface at
https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Holidays-UA. I
# procedural approach
use Date::Holidays::UA qw(:all);
my ($year, $month, $day) = (localtime)[5, 4, 3];
$year += 1900;
$month += 1;
print 'Holiday!' if is_holiday($year, $month, $day);
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'};
# object-oriented approach
use DateTime;
use Date::Holidays::UA;
my $ua = Date::Holidays::UA->new({ language => 'en' });
print 'Holiday!' if $ua->is_holiday_dt(DateTime->today);
my $calendar = $ua->holidays(DateTime->today->year);
print join("\n", value(%$calendar)); # list of holiday names for Ukraine
```
## Subroutines/Methods
### new()
Create a new Date::Holidays::UA object. Parameters should be given as
a hashref of key-value pairs.
```perl
print $ua->is_ua_holiday(2020, 1, 1); # "New Year"
```
### holidays()
For the given year, return a hashref containing all the holidays for
that year. The keys are the date of the holiday in `mmdd` format
(eg '1225' for December 25); the values are the holiday names.
```perl
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'}; # "Independence Day"
my $calendar = $ua->holidays($year);
print $calendar->{'0628'}; # "Constitution Day"
```
### ua\_holidays()
Same as `holidays()`.
### holidays\_dt()
Similar to `holidays()`, The keys are the date of the holiday in `mmdd` format
(eg '1225' for December 25); and DateTime objects as the values.
```perl
my $calendar = $ua->holidays_dt($year);
```
## Author
Denis Boyun, `<denisboyun at gmail.com>`
## Bugs
Please report any bugs or feature requests to `bug-date-holidays-ua at rt.cpan.org`, or through
the web interface at [https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Holidays-UA](https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Holidays-UA). I will be notified, and then you'll
lib/Date/Holidays/UA.pm view on Meta::CPAN
# procedural approach
use Date::Holidays::UA qw(:all);
my ($year, $month, $day) = (localtime)[5, 4, 3];
$year += 1900;
$month += 1;
print 'Holiday!' if is_holiday($year, $month, $day);
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'};
# object-oriented approach
use DateTime;
use Date::Holidays::UA;
my $ua = Date::Holidays::UA->new({ language => 'en' });
print 'Holiday!' if $ua->is_holiday_dt(DateTime->today);
my $calendar = $ua->holidays(DateTime->today->year);
print join("\n", value(%$calendar)); # list of holiday names for Ukraine
=head1 SUBROUTINES/METHODS
=head2 new()
Create a new Date::Holidays::UA object. Parameters should be given as
a hashref of key-value pairs.
my $ua = Date::Holidays::UA->new();
lib/Date/Holidays/UA.pm view on Meta::CPAN
my $day = shift;
my $opt = shift;
_assert_valid_date($year, $month, $day);
if (!defined($self)) {
$self = __PACKAGE__->new($opt);
}
my $holiday_name = undef;
my $calendar = $self->_generate_calendar($year);
for my $holiday(@{$calendar || []}) {
my $holiday_dt = $holiday->{dt};
if (($holiday_dt->month == $month) && ($holiday_dt->day == $day)) {
$holiday_name = $self->_get_holiday_name($holiday);
last;
}
}
return $holiday_name;
}
=head2 holidays()
For the given year, return a hashref containing all the holidays for
that year. The keys are the date of the holiday in C<mmdd> format
(eg '1225' for December 25); the values are the holiday names.
my $calendar = holidays($year, {language => 'en'});
print $calendar->{'0824'}; # "Independence Day"
my $calendar = $ua->holidays($year);
print $calendar->{'0628'}; # "Constitution Day"
=cut
sub holidays {
my $self;
$self = shift if (ref $_[0]);
my $year = shift;
my $args_ref = shift;
unless (defined $self) {
$self = __PACKAGE__->new($args_ref);
}
my $calendar = $self->_generate_calendar($year);
my %holidays = map {
$_->{dt}->strftime('%m%d') => $self->_get_holiday_name($_)
}@{$calendar || []};
return \%holidays;
}
=head2 ua_holidays()
Same as C<holidays()>.
=cut
sub ua_holidays {
return holidays(@_);
}
=head2 holidays_dt()
Similar to C<holidays()>, The keys are the date of the holiday in C<mmdd> format
(eg '1225' for December 25); and DateTime objects as the values.
my $calendar = $ua->holidays_dt($year);
=cut
sub holidays_dt {
my $self;
$self = shift if (ref $_[0]);
my $year = shift;
my $args_ref = shift;
unless (defined $self) {
$self = __PACKAGE__->new($args_ref);
}
my $calendar = $self->_generate_calendar($year);
my %holidays = map {
$_->{dt}->strftime('%m%d') => $_->{dt}
}@{$calendar || []};
return \%holidays;
}
# _get_holiday_name
#
# accepts: holiday item
# returns: holiday name
#
# generate a holiday calendar for the specified year
sub _get_holiday_name {
my $self = shift;
my $holiday = shift;
croak('Missing or wrong holiday item') if (!$holiday || !keys(%{$holiday || {}}));
my $holiday_name = (lc($self->{language}) eq lc(DEFAULT_LANG)) ? $holiday->{local_name} : $holiday->{name};
return $holiday_name;
}
# _generate_calendar
#
# accepts: numeric year
# returns: arrayref of hashref
#
# generate a holiday calendar for the specified year
sub _generate_calendar {
my $self = shift;
my $year = shift;
my $calendar = [];
croak('Missing year parameter') if (!$year);
for my $holiday_rule(@{${\HOLIDAY_RULES}}) {
next if ($holiday_rule->{start_year} && ($year <= $holiday_rule->{start_year}));
next if ($holiday_rule->{end_year} && ($year >= $holiday_rule->{end_year}));
if ($holiday_rule->{is_easter_depend}) {
my $dt = DateTime->new(year => $year);
my $easter = DateTime::Event::Easter->new(easter => "eastern");
my $easter_offset_day = $holiday_rule->{easter_offset_day};
push @{$calendar}, {
name => $holiday_rule->{name},
local_name => $holiday_rule->{local_name},
dt => $easter->following($dt)->add(days => $easter_offset_day)
};
}
else {
my $dt = DateTime->new(
year => $year,
month => $holiday_rule->{month},
day => $holiday_rule->{day}
);
push @{$calendar}, {name => $holiday_rule->{name}, local_name => $holiday_rule->{local_name}, dt => $dt};
}
}
return _spread_on_weekend($calendar);
}
# _spread_on_weekend
#
# accepts: calendar of holidays
# returns: arrayref of hashref
#
# spread weekend holidays on other non-weekend days
sub _spread_on_weekend {
my $calendar = shift;
croak('Missing calendar') if (!scalar(@{$calendar || []}));
my $calc = [];
for my $holiday(@{$calendar || []}) {
next if (!$holiday->{dt});
push(@{$calc}, $holiday);
my $dt = $holiday->{dt}->clone();
my $is_weekend = WEEKEND_MAP->{$dt->day_of_week()} ? 1 : 0;
if ($is_weekend) {
for (my $offset_day = 1; $offset_day <= 2; $offset_day++) {
my $dt_next = $dt->clone()->add(days => $offset_day);
( run in 1.250 second using v1.01-cache-2.11-cpan-d7f47b0818f )