Skip to content

Commit

Permalink
Add US Holidays (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcdigital authored Jan 24, 2024
1 parent 964e741 commit 95f9772
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Countries/UnitedStates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

class UnitedStates extends Country
{
public function countryCode(): string
{
return 'us';
}

protected function allHolidays(int $year): array
{
$holidays = array_merge([
'New Year\'s Day' => '01-01',
'Independence Day' => '07-04',
'Veterans Day' => '11-11',
'Christmas' => '12-25',
], $this->variableHolidays($year));

if ($year >= 2021) {
$holidays['Juneteenth National Independence Day'] = '06-19';
}

return $holidays;
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$martinLutherKingDay = new CarbonImmutable("third monday of January $year", 'America/Los_Angeles');
$presidentsDay = new CarbonImmutable("third monday of February $year", 'America/Los_Angeles');
$memorialDay = new CarbonImmutable("last monday of May $year", 'America/Los_Angeles');
$laborDay = new CarbonImmutable("first monday of September $year", 'America/Los_Angeles');
$columbusDay = new CarbonImmutable("second monday of October $year", 'America/Los_Angeles');
$thanksgiving = new CarbonImmutable("fourth thursday of November $year", 'America/Los_Angeles');

return [
'Martin Luther King Day' => $martinLutherKingDay,
'Presidents\' Day' => $presidentsDay,
'Memorial Day' => $memorialDay,
'Labor Day' => $laborDay,
'Columbus Day' => $columbusDay,
'Thanksgiving' => $thanksgiving,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Martin Luther King Day",
"date": "2024-01-15"
},
{
"name": "Presidents' Day",
"date": "2024-02-19"
},
{
"name": "Memorial Day",
"date": "2024-05-27"
},
{
"name": "Juneteenth National Independence Day",
"date": "2024-06-19"
},
{
"name": "Independence Day",
"date": "2024-07-04"
},
{
"name": "Labor Day",
"date": "2024-09-02"
},
{
"name": "Columbus Day",
"date": "2024-10-14"
},
{
"name": "Veterans Day",
"date": "2024-11-11"
},
{
"name": "Thanksgiving",
"date": "2024-11-28"
},
{
"name": "Christmas",
"date": "2024-12-25"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"name": "New Year's Day",
"date": "2020-01-01"
},
{
"name": "Martin Luther King Day",
"date": "2020-01-20"
},
{
"name": "Presidents' Day",
"date": "2020-02-17"
},
{
"name": "Memorial Day",
"date": "2020-05-25"
},
{
"name": "Independence Day",
"date": "2020-07-04"
},
{
"name": "Labor Day",
"date": "2020-09-07"
},
{
"name": "Columbus Day",
"date": "2020-10-12"
},
{
"name": "Veterans Day",
"date": "2020-11-11"
},
{
"name": "Thanksgiving",
"date": "2020-11-26"
},
{
"name": "Christmas",
"date": "2020-12-25"
}
]
32 changes: 32 additions & 0 deletions tests/Countries/UnitedStatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Holidays;

it('can calculate united states holidays after 2021', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01');

$holidays = Holidays::for(country: 'us')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

expect(formatDates($holidays))->toMatchSnapshot();

});

it('can calculate united states holidays before 2021', function () {
CarbonImmutable::setTestNowAndTimezone('2020-01-01');

$holidays = Holidays::for(country: 'us')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

expect(formatDates($holidays))->toMatchSnapshot();

});

0 comments on commit 95f9772

Please sign in to comment.