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

Enhanced holiday dates in Ghana #80

Merged
merged 20 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
115 changes: 115 additions & 0 deletions src/Countries/Ghana.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

class Ghana extends Country
{

protected string $timezone = 'Africa/Accra';

public function countryCode(): string
{
return 'gh';
}

/**
* @return array<string, CarbonImmutable>
*/
protected function christmasDay(int $year): array
{
$christmasDay = new CarbonImmutable($year . "-12-25", $this->timezone);
Ikpeba4ll marked this conversation as resolved.
Show resolved Hide resolved
$key = 'Christmas Day';

if ($christmasDay->isSaturday()) {
$key .= ' (substitute day)';
$christmasDay = $christmasDay->next('monday');
}

if ($christmasDay->isSunday()) {
$key .= ' (substitute day)';
$christmasDay = $christmasDay->next('tuesday');
}

return [$key => $christmasDay];
}

/**
* @return array<string, CarbonImmutable>
*/
protected function boxingDay(int $year): array
{
$christmasDay = new CarbonImmutable($year . "-12-25", $this->timezone);
Ikpeba4ll marked this conversation as resolved.
Show resolved Hide resolved
$boxingDay = new CarbonImmutable($year . "-12-26", $this->timezone);
$key = 'Boxing Day';

if ($christmasDay->isFriday()) {
$key .= ' (substitute day)';
$boxingDay = $boxingDay->next('monday');
}

if ($christmasDay->isSaturday()) {
$key .= ' (substitute day)';
$boxingDay = $boxingDay->next('tuesday');
}

return [$key => $boxingDay];
}

/**
* Get holiday
*
* If it falls on a weekend, the new day to be observed is the next monday
*
* @return array<string, CarbonImmutable>
*
*/
protected function getHoliday(string $nameOfHoliday, int $year, string $monthAndDay): array
{
$newYearsDay = new CarbonImmutable($year . "-" . $monthAndDay, $this->timezone);
$key = $nameOfHoliday;

if ($newYearsDay->isWeekend()) {
$key .= ' (substitute day)';
$newYearsDay = $newYearsDay->next('monday');
}

return [$key => $newYearsDay];
}

protected function allHolidays(int $year): array
{
return array_merge(
$this->getHoliday('New Year Day', $year, "01-01"),
Ikpeba4ll marked this conversation as resolved.
Show resolved Hide resolved
$this->getHoliday('Constitution Day', $year, "01-07"),
$this->getHoliday('Independence Day', $year, "03-06"),
$this->getHoliday('May Day', $year, "05-01"),
$this->getHoliday('Founders Day', $year, "08-04"),
$this->getHoliday('Kwame Nkrumah Memorial Day', $year, "09-21"),
$this->christmasDay($year),
$this->boxingDay($year),
$this->variableHolidays($year)
);
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easter = CarbonImmutable::createFromTimestamp(easter_date($year))
Ikpeba4ll marked this conversation as resolved.
Show resolved Hide resolved
->setTimezone($this->timezone);

$farmersDay = (new CarbonImmutable('first friday of December ' . $year, $this->timezone));
Ikpeba4ll marked this conversation as resolved.
Show resolved Hide resolved

return [
'Farmers Day' => $farmersDay,
'Good Friday' => $easter->subDays(2),
'Easter Monday' => $easter->addDay(),

// NB: *** There are no fixed dates for the Eid-Ul-Fitr and Eid-Ul-Adha because they are movable feasts.
// The dates for their observation are provided by the Office of the Chief Imam in the course of the year.
// 'Eid-Ul-Fitr' => "",
// 'Eid-Ul-Adha' => "",
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"name": "New Year Day",
"date": "2024-01-01"
},
{
"name": "Constitution Day (substitute day)",
"date": "2024-01-08"
},
{
"name": "Independence Day",
"date": "2024-03-06"
},
{
"name": "Good Friday",
"date": "2024-03-29"
},
{
"name": "Easter Monday",
"date": "2024-04-01"
},
{
"name": "May Day",
"date": "2024-05-01"
},
{
"name": "Founders Day (substitute day)",
"date": "2024-08-05"
},
{
"name": "Kwame Nkrumah Memorial Day (substitute day)",
"date": "2024-09-23"
},
{
"name": "Farmers Day",
"date": "2024-12-06"
},
{
"name": "Christmas Day",
"date": "2024-12-25"
},
{
"name": "Boxing Day",
"date": "2024-12-26"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/GhanaTest.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 ghana holidays', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01');

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

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

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