Date-Holidays-TW
view release on metacpan or search on metacpan
lib/Date/Holidays/TW.pm view on Meta::CPAN
}
sub is_holiday {
my (undef, $year, $month, $day) = @_;
return is_tw_holiday($year, $month, $day);
}
my %_reified;
sub tw_holidays {
my ($year) = @_;
$year = sprintf('%04d', $year);
unless ($_reified{$year}) {
my %holidays = %NATIONAL;
my $dt = DateTime->new( year => $year, month => 1, day => 1, time_zone => 'Asia/Taipei' );
while ($dt->year == $year) {
my $h = __is_tw_holiday($dt);
if (defined($h)) {
my $mmdd = $dt->strftime('%m%d');
$holidays{$mmdd} = $h;
}
$dt->add(days => 1);
}
$_reified{$year} = \%holidays;
}
return $_reified{$year};
}
sub is_tw_holiday {
my ($year, $month, $day) = @_;
return __is_tw_holiday(
DateTime->new(
year => $year,
month => $month,
day => $day,
time_zone => 'Asia/Taipei',
)
);
}
sub __is_tw_holiday {
my ($dt) = @_;
my $mmdd = $dt->strftime('%m%d');
my $year = $dt->year;
return $CAL{$year}{$mmdd} // $NATIONAL{$mmdd} // __is_qingming($dt) // __is_tw_lunar_holiday($dt)
}
sub __is_tw_lunar_holiday {
my ($dt) = @_;
my $lunar_date = DateTime::Calendar::Chinese->from_object(object => $dt);
return undef if $lunar_date->leap_month;
my $lunar_mmdd = sprintf('%02d%02d', $lunar_date->month, $lunar_date->day);
return $FOLK_LUNAR{$lunar_mmdd};
}
sub __is_qingming {
# Thanks Wei-Hon Chen for the formula.
my $dt = $_[0];
return undef unless $dt->month == 4 && 3 < $dt->day && $dt->day < 6;
my $year = $dt->year;
die "Unsupported" if $year < 1901 || 2100 < $year;
my $Y = ($year % 100);
my $C = (1901 <= $year && $year < 2001) ? 5.59 : 4.81;
my $n = int($Y * 0.2422 + 4.81) - int($Y / 4);
return $dt->day == $n ? 'æ°ææå¢ç¯' : undef;
}
1;
__END__
=head1 NAME
Date::Holidays::TW - Determine whether it is Taiwan Holidays or not.
=head1 SYNOPSIS
This module can be used by itself:
use Date::Holidays::TW qw(is_tw_holiday);
if ( is_tw_holiday(2020, 6, 25) ) {
...
}
Or via C<Date::Holidays>
my $dh = Date::Holidays->new( countrycode => 'TW' );
if ($dh->is_holiday( 2020, 6, 25 )) {
...
}
=head1 DESCRIPTION
This module provides functions to look into Taiwan holiday calendars
for known holidays. It could be used by itself, or under via
L<Date::Holidays> module.
Caveat: Due to the rule of weekend-compensation and the fact that the
majority of holidays are defined by Chinese calendar (Lunar), it
requires some non-trivial amount of computation to correctly determine
whether the given date is an holiday or not -- which is not
implemented at this version.
The current implementation includes all known holidays from year 2013
and 2024 in a lookup table and should therefore correctly determine
holidays in those years. It should also determine most of the future
holidays correctly with some basic compuation, except for the ones
generated by the weekend-compensation rules, which is somewhat
ambiguous.
Conventionally the holiday calendar for the next year is announcend at
the end of June and we could start to mix the new information into the
lookup table in this module.
Generally speaking, queries for far future should be avoided.
=head1 EXPORTABLE FUNCTIONS
( run in 1.676 second using v1.01-cache-2.11-cpan-a5162978ef8 )