Skip to content

Commit

Permalink
Add holidays for South Africa
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Jan 17, 2024
1 parent 9927780 commit 23ec0b1
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Countries/SouthAfrica.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

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

protected function allHolidays(int $year): array
{
$holidays = [
'Nuwe Jaar' => '01-01',
'Mense Regte Dag' => '03-21',
'Vryhuids Dag' => '04-27',
'Werkers Dag' => '05-01',
'Jeug Dag' => '06-16',
'Vroue Dag' => '08-09',
'Erfenis Dag' => '09-24',
'Versoenings Dag' => '12-16',
'Kersfees' => '12-25',
'Dag van Welwillendheid' => '12-26',
];

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

if ($holidayDate->isSunday()) {
$holidays[$name . ' (Waarneming)'] = $holidayDate->addDay()->format('m-d');
}
}

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

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

return [
'Goeie Vrydag' => $easter->subDays(2),
'Familie Dag' => $easter->addDay(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"name": "Nuwe Jaar",
"date": "2024-01-01"
},
{
"name": "Mense Regte Dag",
"date": "2024-03-21"
},
{
"name": "Goeie Vrydag",
"date": "2024-03-29"
},
{
"name": "Familie Dag",
"date": "2024-04-01"
},
{
"name": "Vryhuidsdag",
"date": "2024-04-27"
},
{
"name": "Werkers Dag",
"date": "2024-05-01"
},
{
"name": "Jeug Dag",
"date": "2024-06-16"
},
{
"name": "Jeug Dag (Waarneming)",
"date": "2024-06-17"
},
{
"name": "Vroue Dag",
"date": "2024-08-09"
},
{
"name": "Erfenis Dag",
"date": "2024-09-24"
},
{
"name": "Versoenings Dag",
"date": "2024-12-16"
},
{
"name": "Kersfees",
"date": "2024-12-25"
},
{
"name": "Dag van Welwillendheid",
"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 23ec0b1

Please sign in to comment.