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 holidays for South Africa #30

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Countries/SouthAfrica.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use function in_array;

class SouthAfrica extends Country
{
public function countryCode(): string
{
return 'za';
}

protected function allHolidays(int $year): array
{
// https://en.wikipedia.org/wiki/Public_holidays_in_South_Africa
// https://www.gov.za/about-sa/public-holidays
$holidays = [
'New Year\'s Day' => '01-01',
'Human Rights Day' => '03-21',
'Freedom Day' => '04-27',
'Workers\' Day' => '05-01',
'Youth Day' => '06-16',
'National Women\'s Day' => '08-09',
'Heritage Day' => '09-24',
'Day of Reconciliation' => '12-16',
'Christmas Day' => '12-25',
'Day of Goodwill' => '12-26',
];

foreach ($holidays as $name => $date) {
$holidayDate = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}");
assert($holidayDate instanceof CarbonImmutable);

// The Public Holidays Act (Act No 36 of 1994) states that whenever a public holiday falls on a Sunday, the Monday following it will be a public holiday.
// https://www.gov.za/documents/public-holidays-act
if (
$holidayDate->isSunday() &&
!in_array($holidayDate->addDay()->format('m-d'), $holidays, true) // Check that the Monday is not already a holiday
) {
$holidays[$name . ' Observed'] = $holidayDate->addDay();
}
}

return array_merge($holidays, $this->variableHolidays($year));
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easter = $this->easter($year);

return [
'Good Friday' => $easter->subDays(2),
'Family Day' => $easter->addDay(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Human Rights Day",
"date": "2024-03-21"
},
{
"name": "Good Friday",
"date": "2024-03-29"
},
{
"name": "Family Day",
"date": "2024-04-01"
},
{
"name": "Freedom Day",
"date": "2024-04-27"
},
{
"name": "Workers' Day",
"date": "2024-05-01"
},
{
"name": "Youth Day",
"date": "2024-06-16"
},
{
"name": "Youth Day Observed",
"date": "2024-06-17"
},
{
"name": "National Women's Day",
"date": "2024-08-09"
},
{
"name": "Heritage Day",
"date": "2024-09-24"
},
{
"name": "Day of Reconciliation",
"date": "2024-12-16"
},
{
"name": "Christmas Day",
"date": "2024-12-25"
},
{
"name": "Day of Goodwill",
"date": "2024-12-26"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/SouthAfricaTest.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 south africa holidays', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01');

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

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

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