Skip to content

Commit

Permalink
Add holidays for South Africa (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup authored Jan 23, 2024
1 parent 7a09493 commit a5b467a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
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();
});

0 comments on commit a5b467a

Please sign in to comment.