Date-Holidays-CA

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        # procedural approach

        use Date::Holidays::CA qw(:all);

        my ($year, $month, $day) = (localtime)[ 5, 4, 3 ];
        $year  += 1900;
        $month += 1;

        print 'Woot!' if is_holiday($year, $month, $day, {province => 'BC'});

        my $calendar = holidays($year, {province => 'BC'});
        #returns a hash reference
        print $calendar->{'0701'};              # "Canada Day/Fête du Canada"


        # object-oriented approach

        use DateTime;
        use Date::Holidays::CA;

        my $dhc = Date::Holidays::CA->new({ province => 'QC' });

        print 'Woot!' if $dhc->is_holiday(DateTime->today);

        my $calendar = $dhc->holidays_dt(DateTime->today->year);
        print join keys %$calendar, "\n";       # lists holiday names for QC

DESCRIPTION
    Date::Holidays::CA determines public holidays for Canadian
    jurisdictions. Its interface is a superset of that provided by
    Date::Holidays -- read on for details.

NAME
    Date::Holidays::CA - Holidays for Canadian locales

FUNCTIONS / METHODS

README  view on Meta::CPAN


        $holiday_p = is_holiday($dt, {province => 'SK', language => 'EN'});

        $holiday_p = $dhc->is_holiday($dt);

   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, {province => 'MB', language => 'EN'});
        #returns a hash reference
        print $calendar->{'0701'};               # "Canada Day"

        my $calendar = $dhc->holidays($year);
        #returns a hash reference
        print $calendar->{'1111'};               # "Remembrance Day"

   ca_holidays()
    Same as holidays().

   holidays_dt()
    Similar to holidays(), after a fashion: returns a hashref with the
    holiday names as the keys and DateTime objects as the values.

        my $calendar = $dhc->holidays_dt($year);

SPECIFICATIONS
    The following holidays are recognized:

    *New Year's Day*
        January 1.

    *Islander Day*
        PE. Originally added in 2009 as the second Monday in February, this
        holiday will be revised to the third Monday in February starting in

lib/Date/Holidays/CA.pm  view on Meta::CPAN


    if (defined $options and defined $options->{province} ) {
        $self->{province} = $options->{province}
    }
    _assert_valid_date($year, $month, $day);

    unless (defined $self) {
        $self = Date::Holidays::CA->new($options);
    }

    my $calendar = $self->_generate_calendar($year);

    # assumption: there is only one holiday for any given day.
    while (my ($holiday_name, $holiday_dt) = each %$calendar) {
        if ($month == $holiday_dt->month and $day == $holiday_dt->day) {
            return $holiday_name;
        }
    }

    return;
}



lib/Date/Holidays/CA.pm  view on Meta::CPAN

    my @args = map {
        ref $_ eq 'DateTime' ? ($_->year, $_->month, $_->day) : $_
    } @_;

    return is_holiday(@args);
}



sub holidays {
    my $calendar = holidays_dt(@_);

    my %holidays = map {
       $calendar->{$_}->strftime('%m%d') => $_
    } keys %$calendar;

    return \%holidays;
}



sub ca_holidays {
    return holidays(@_);
}

lib/Date/Holidays/CA.pm  view on Meta::CPAN

    my $self;
    $self = shift if (ref $_[0]);               # invoked in OO style

    my $year     = shift;
    my $args_ref = shift;

    unless (defined $self) {
        $self = Date::Holidays::CA->new($args_ref);
    }

    return $self->_generate_calendar($year);
}



### internal functions

my @VALID_PROVINCES = qw{ CA AB BC MB NB NL NS NT NU ON PE QC SK YT };
my @VALID_LANGUAGES = qw{ EN/FR FR/EN EN FR };
my %VALUES_FOR = (
    'PROVINCE' => \@VALID_PROVINCES,

lib/Date/Holidays/CA.pm  view on Meta::CPAN

            \&_canada_day,               'Canada Day', 'Fête du Canada',
            \&_yt_discovery_day,         'Discovery Day', 'Jour du découverte',
            \&_labour_day,               'Labour Day', 'Fête du Travail',
            \&_thanksgiving_day,         'Thanksgiving Day', 'Action de Grâce',
            \&_remembrance_day,          'Remembrance Day', 'Jour du Souvenir',
            \&_christmas_day,            'Christmas Day', 'Noël',
    ],
);


# _generate_calendar
#
# accepts: numeric year
# returns: hashref (string $holiday_name => DateTime $holiday_dt)
#
# generate a holiday calendar for the specified year -- a hash mapping
# holiday names to datetime objects.
sub _generate_calendar {
    my $self = shift;
    my $year = shift;
    my $calendar = {};

    my @holiday_list = @{ $HOLIDAYS_FOR{$self->{'province'}} };

    while(@holiday_list) {
        my $holiday_dt = (shift @holiday_list)->($year);  # fn invokation
        my $name_en    = shift @holiday_list;
        my $name_fr    = shift @holiday_list;

        my $holiday_name =
              $self->{'language'} eq 'EN'    ? $name_en
            : $self->{'language'} eq 'FR'    ? $name_fr
            : $self->{'language'} eq 'EN/FR' ? "$name_en/$name_fr"
            : $self->{'language'} eq 'FR/EN' ? "$name_fr/$name_en"
            : "$name_en/$name_fr";  # sane default, should never get here

        $calendar->{$holiday_name} = $holiday_dt;
    }

    return $calendar;
}

### toolkit functions

# _nth_monday
#
# accepts:  year, month, ordinal of which monday to find
# returns:  numeric date of the requested monday
#
# find the day of week for the first day of the month,

lib/Date/Holidays/CA.pm  view on Meta::CPAN

    # procedural approach

    use Date::Holidays::CA qw(:all);

    my ($year, $month, $day) = (localtime)[ 5, 4, 3 ];
    $year  += 1900;
    $month += 1;

    print 'Woot!' if is_holiday($year, $month, $day, {province => 'BC'});

    my $calendar = holidays($year, {province => 'BC'});
    #returns a hash reference
    print $calendar->{'0701'};              # "Canada Day/Fête du Canada"


    # object-oriented approach

    use DateTime;
    use Date::Holidays::CA;

    my $dhc = Date::Holidays::CA->new({ province => 'QC' });

    print 'Woot!' if $dhc->is_holiday(DateTime->today);

    my $calendar = $dhc->holidays_dt(DateTime->today->year);
    print join keys %$calendar, "\n";       # lists holiday names for QC

=head1 DESCRIPTION

Date::Holidays::CA determines public holidays for Canadian jurisdictions.
Its interface is a superset of that provided by Date::Holidays -- read
on for details.

=head1 NAME

Date::Holidays::CA - Holidays for Canadian locales

lib/Date/Holidays/CA.pm  view on Meta::CPAN

    $holiday_p = is_holiday($dt, {province => 'SK', language => 'EN'});

    $holiday_p = $dhc->is_holiday($dt);

=head3 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, {province => 'MB', language => 'EN'});
    #returns a hash reference
    print $calendar->{'0701'};               # "Canada Day"

    my $calendar = $dhc->holidays($year);
    #returns a hash reference
    print $calendar->{'1111'};               # "Remembrance Day"

=head3 ca_holidays()

Same as C<holidays()>.

=head3 holidays_dt()

Similar to C<holidays()>, after a fashion: returns a hashref with the
holiday names as the keys and DateTime objects as the values.

    my $calendar = $dhc->holidays_dt($year);

=head1 SPECIFICATIONS

The following holidays are recognized:

=over

=item I<New Year's Day>

January 1.

t/01usage.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More qw(no_plan);
use Test::Exception;

BEGIN { use_ok('Date::Holidays::CA', qw(:all)) };



INSTANTIATION: {
    my $calendar = Date::Holidays::CA->new();
    isa_ok($calendar, 'Date::Holidays::CA');
}

DEFAULTS: {
    my $calendar = Date::Holidays::CA->new();
    is($calendar->get('province'), 'CA',
        'Default province set to CA'
    );
    is($calendar->get('language'), 'EN/FR',
        'Default language set to EN/FR'
    );
}

CASE_INSENSITIVITY: {
    my $ON_calendar = Date::Holidays::CA->new(
        {province => 'ON', language => 'EN'}
    );

    my $on_calendar = Date::Holidays::CA->new(
        {province => 'on', language => 'en'}
    );

    is_deeply(
        $ON_calendar, $on_calendar,
        'Province and language names are case-insensitive'
    );
}




# test usage of each function -- both object-oriented and procedural.
# the results must be the same!
# is_holiday and is_ca_holiday must return the exact same thing;

t/02stress.t  view on Meta::CPAN

use Test::More qw(no_plan);
use Test::Exception;

BEGIN { use_ok('Date::Holidays::CA', qw(:all)) };

# break stuff here.
# if we feed any of the functions nonsensical data, will they barf?


GET_NO_FIELD: {
    my $calendar = Date::Holidays::CA->new();
    dies_ok { $calendar->get(); } 'must specify which field to get()';
}

GET_NONEXISTENT_FIELD: {
    my $calendar = Date::Holidays::CA->new();
    dies_ok {
        $calendar->get('tim_hortons');
    } 'Exception thrown when invalid field passed to get()';
}

SET_NONEXISTENT_FIELD: {
    my $calendar = Date::Holidays::CA->new();
    dies_ok {
        $calendar->set({ poutine => 'bien sur' });
    } 'Exception thrown when invalid field passed to set()';
}

SET_INVALID_PROVINCE: {
    my $calendar = Date::Holidays::CA->new();
    dies_ok {
        $calendar->set({ province => 'mi' });
    } 'Exception thrown when invalid province given to set()';
}

NEW_NONEXISTENT_FIELD: {
    dies_ok {
        my $calendar = Date::Holidays::CA->new({
            doughnut => 'honey cruller',
        });
    } 'Exception thrown when invalid field name given to new()';
}

NEW_INVALID_PROVINCE: {
    dies_ok {
        my $calendar = Date::Holidays::CA->new({
            province => 'ZZ',
        });
    } 'Exception thrown when invalid province given to new()';
}

# russian not yet implemented =)
NEW_INVALID_LANGUAGE: {
    dies_ok {
        my $calendar = Date::Holidays::CA->new({
            language => 'RU',
        });
    } 'Exception thrown when invalid language given to new()';
}

t/04correctness.t  view on Meta::CPAN

#     do we get back the right holidays for the province we give?
#     do we get back the holiday names in the right language?

# load up a table of representative years and see if the module's
# output matches.

my %CANONICAL_HOLIDAYS_FOR  = canonical_holidays();
my %SUBSTITUTE_HOLIDAYS_FOR = substitute_holidays();

foreach my $year (keys %CANONICAL_HOLIDAYS_FOR) {
    my $calendar = Date::Holidays::CA->new(
        {language => 'EN', province => 'CA'}
    );
    my $holidays_ref = $calendar->holidays($year);

    # first check that we've generated the correct number of holidays
    is (
         scalar keys %{$holidays_ref},
         scalar keys %{$CANONICAL_HOLIDAYS_FOR{$year}},
         "correct number of holidays for $year"
    );

    # now check the individual dates
    foreach my $holiday_name (keys %{$CANONICAL_HOLIDAYS_FOR{$year}}) {

t/06-issue-rt106793.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More qw(no_plan);

BEGIN { use_ok('Date::Holidays::CA', qw(:all)) };

{
    my $ca = Date::Holidays::CA->new({province => 'NL'});
    ok($ca->is_holiday(2015, 3, 16), "Specify province in initalization");
    ok(!$ca->is_holiday(2015, 2, 16), "Specify province in initalization");
    my $calendar = holidays(2015, {province => 'NL', language => 'EN'});
    like($calendar->{'0316'}, qr/St Patrick's/, "St Patrick's Day");

}
{
    my $ca = Date::Holidays::CA->new();
    ok($ca->is_holiday(2015, 3, 16, {province => 'NL'}), "Specify province in is_holiday");
    ok(!$ca->is_holiday(2015, 2, 16), "Specify province in initalization");
    my $calendar = holidays(2015, {province => 'NL', language => 'EN'});
    like($calendar->{'0316'}, qr/St Patrick's/, "St Patrick's Day");

}

done_testing;

t/07-issue-rt77533.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More qw(no_plan);

BEGIN { use_ok('Date::Holidays::CA', qw(:all)) };

{
    my $ca = Date::Holidays::CA->new({province => 'BC'});
    ok($ca->is_holiday(2024, 2, 19), "Specify province in initalization");
    my $calendar = holidays(2024, {province => 'BC', language => 'EN'});
    like($calendar->{'0219'}, qr/Family Day/, "Family Day - British Columbia");
}

done_testing;



( run in 0.343 second using v1.01-cache-2.11-cpan-5dc5da66d9d )