jonasbn / perl-date-holidays

Date::Holidays - national holidays adapter/aggregator for all your holiday needs
https://jonasbn.github.io/perl-date-holidays/
5 stars 6 forks source link

Date::Holidays::US to replace Date::Holidays::USFederal #71

Open mrdvt92 opened 1 year ago

mrdvt92 commented 1 year ago

Issue Template

Description

I could not get in touch with the developer of Date::Holidays::USFederal and it does not support the new Juneteenth holiday nor historical holidays. So, I wrote Date::Holidays::US. It works as a standalone module and I believe it is in the correct format to support your API.

Bugs and Issues

I was hoping that you might update Date::Holidays::Adapter::US to search for Date::Holidays::US and if it is installed then use it over Date::Holidays::USFederal.

The below code appears to work but I don't really understand your API so it might not be the logic you would want to implement.

package Date::Holidays::Adapter::US;

use strict;
use warnings;
use vars qw($VERSION);
use Carp; # croak
use Data::Dumper;

use base 'Date::Holidays::Adapter';

$VERSION = '1.31';

sub new {
    my ( $class, %params ) = @_;

    local $@;
    eval 'require Date::Holidays::US';
    my $error = $@;

    if ($error) {
        $params{countrycode} = 'USFederal';
    } else {
        $params{countrycode} = 'US';
    }
    $params{nocheck}     = 1;

    my $self = $class->SUPER::new(%params);
    return $self;
}

sub holidays {
    my ($self, %params) = @_;
    if ($self->{_adaptee} eq 'Date::Holidays::US') {
        my $year = $params{'year'} or die("Method holidays required parameter year");
        return Date::Holidays::US::holidays($year);
    } else {
        croak "holidays is unimplemented for ".__PACKAGE__;
    }
}

sub is_holiday {
    my ($self, %params) = @_;

    $self->_load($self->{_adaptee});

    if ($self->{_adaptee} eq 'Date::Holidays::US') {
        return Date::Holidays::US::is_holiday(
            $params{'year'}, $params{'month'}, $params{'day'}
        );
    } else {
        return Date::Holidays::USFederal::is_usfed_holiday(
            $params{'year'}, $params{'month'}, $params{'day'}
        );
    }
}

1;

Here's an example script.

perl -e '
use lib qw{lib};
use strict;
use warnings;
use Date::Holidays;

my $dh = Date::Holidays->new(countrycode => "us");

my $holidayname = $dh->is_holiday(year => 2022, month => 7, day => 4);

print "$holidayname\n";

my $dates = $dh->holidays(year=>2022);
foreach my $key (sort keys %$dates) {
  my $name = $dates->{$key};
  print "$key $name\n";
}
'
Independence Day
0101 New Year's Day
0117 Birthday of Martin Luther King, Jr.
0221 Washington's Birthday
0530 Memorial Day
0619 Juneteenth National Independence Day
0620 Juneteenth National Independence Day Observed
0704 Independence Day
0905 Labor Day
1010 Columbus Day
1111 Veterans Day
1124 Thanksgiving Day
1225 Christmas Day
1226 Christmas Day Observed
jonasbn commented 1 year ago

Hi @mrdvt92

I will evaluate your proposal, based on what you write I think we can perhaps exchange Date::Holidays::USFederal for Date::Holidays::US

bpschuck commented 1 year ago

Aside from Juneteenth, would it make sense to have a separate module for US Stock Exchange holidays? While many of them overlap, one example of a difference is that the US stock markets are closed on Good Friday every year. Bruce S.

mrdvt92 commented 1 year ago

separate module for US Stock Exchange holidays?

Based on my research, the NYSE and Nasdaq both add Good Friday but they are open Columbus Day and Veteran's Day. I could not find a policy for "in lieu of" days. The bond markets (SIFMA) appear to stick to Federal Holidays except for they are open on Veteran's Day. Banks appear to follow the Federal holidays but are not required to. States are not required the follow Federal Holidays either but they have renamed some of the holidays.

So, to do it right we really need a US (Federal), NYSE, Nasdaq, SIFMA, and then 50+ for each state, district, territory, etc.

ref: https://www.aarp.org/money/investing/info-2023/stock-market-holidays.html ref: https://www.aarp.org/money/investing/info-2023/federal-bank-holidays.html