Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Canadian Holidays #41

Merged
merged 6 commits into from
Jan 23, 2024
Merged
61 changes: 61 additions & 0 deletions src/Countries/Canada.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

class Canada extends Country
{
public function countryCode(): string
{
return 'ca';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge(
[
'New Year\'s Day' => new CarbonImmutable($year . "-01-01", 'America/Toronto'),
'Canada Day' => new CarbonImmutable($year . "-07-01", 'America/Toronto'),
'Civic Holiday' => new CarbonImmutable(
"first monday of August " . $year, 'America/Toronto'
),
'Labour Day' => new CarbonImmutable(
"first monday of September " . $year, 'America/Toronto'
),
'National Day for Truth and Reconciliation' => new CarbonImmutable(
$year . "-09-30",
'America/Toronto'
),
'Remembrance Day' => new CarbonImmutable($year . "-11-11", 'America/Toronto'),
'Christmas Day' => new CarbonImmutable($year . "-12-25", 'America/Toronto'),
'Boxing Day' => new CarbonImmutable($year . '-12-26', 'America/Toronto'),
],
$this->variableHolidays($year)
);
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easterSunday = CarbonImmutable::createFromTimestamp(easter_date($year))
->setTimezone('America/Toronto');

$goodFriday = $easterSunday->subDays(2);
$easterMonday = $easterSunday->addDays(1);

$victoriaDay = new CarbonImmutable("last monday of May $year", 'America/Toronto');
if ($victoriaDay->day < 25) {
$victoriaDay = $victoriaDay->addWeek();
}

$thanksgiving = new CarbonImmutable("second monday of October $year", 'America/Toronto');
return [
'Victoria Day' => $victoriaDay,
'Good Friday' => $goodFriday,
'Easter Monday' => $easterMonday,
'Thanksgiving' => $thanksgiving,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Good Friday",
"date": "2024-03-29"
},
{
"name": "Easter Monday",
"date": "2024-04-01"
},
{
"name": "Victoria Day",
"date": "2024-05-27"
},
{
"name": "Canada Day",
"date": "2024-07-01"
},
{
"name": "Civic Holiday",
"date": "2024-08-05"
},
{
"name": "Labour Day",
"date": "2024-09-02"
},
{
"name": "National Day for Truth and Reconciliation",
"date": "2024-09-30"
},
{
"name": "Thanksgiving",
"date": "2024-10-14"
},
{
"name": "Remembrance Day",
"date": "2024-11-11"
},
{
"name": "Christmas Day",
"date": "2024-12-25"
},
{
"name": "Boxing Day",
"date": "2024-12-26"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/CanadaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

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

it('can calculate canadian holidays', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01');

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

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

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