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 Jamaica #185

Merged
merged 6 commits into from
Jan 31, 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
84 changes: 84 additions & 0 deletions src/Countries/Jamaica.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;

class Jamaica extends Country
{
public function countryCode(): string
{
return 'jm';
}

protected function allHolidays(int $year): array
{

$holidays = array_merge(
$this->fixedHolidays(),
$this->variableHolidays($year),
$this->observedHolidays($year)
);

return $holidays;
}

/** @return array<string, string> */
protected function fixedHolidays(): array
{
$holidays = [
'New Year\'s Day' => '01-01',
'Labour Day' => '05-23',
'Emancipation Day' => '08-01',
'Independence Day' => '08-06',
'Christmas Day' => '12-25',
'Boxing Day' => '12-26',
];

return $holidays;
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easter = $this->easter($year);
$heroesDay = new CarbonImmutable("third monday of October $year");

return [
'Ash Wednesday' => $easter->subDays(46),
'Good Friday' => $easter->subDays(2),
'Easter Monday' => $easter->addDay(),
'National Heroes Day' => $heroesDay,
];
}

/** @return array<string, CarbonImmutable> */
protected function observedHolidays(int $year): array
{

$observedHolidays = [];

foreach ($this->fixedHolidays() as $name => $date) {
$date = CarbonImmutable::parse("$year-$date");

// If any holiday falls on a Sunday, then it is observed on Monday
if($date->dayOfWeek === 0) {
$observedHolidays["{$name} Observed"] = $date->next(CarbonImmutable::MONDAY);
}

// If Labour Day falls on a Saturday, then it is observed on Monday
if($name == 'Labour Day' && $date->dayOfWeek === 6) {
$observedHolidays["{$name} Observed"] = $date->next(CarbonImmutable::MONDAY);
}

// If Boxing Day falls on a Monday, then it is observed on Tuesday (Christmas Day is observed on Monday)
// https://jis.gov.jm/observance-public-holidays-christmas-day-monday-december-26th-boxing-day-tuesday-december-27th/
if ($name == 'Boxing Day' && $date->dayOfWeek === 1) {
$observedHolidays["{$name} Observed"] = $date->next(CarbonImmutable::TUESDAY);
}
}

return $observedHolidays;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Ash Wednesday",
"date": "2024-02-14"
},
{
"name": "Good Friday",
"date": "2024-03-29"
},
{
"name": "Easter Monday",
"date": "2024-04-01"
},
{
"name": "Labour Day",
"date": "2024-05-23"
},
{
"name": "Emancipation Day",
"date": "2024-08-01"
},
{
"name": "Independence Day",
"date": "2024-08-06"
},
{
"name": "National Heroes Day",
"date": "2024-10-21"
},
{
"name": "Christmas Day",
"date": "2024-12-25"
},
{
"name": "Boxing Day",
"date": "2024-12-26"
}
]
34 changes: 34 additions & 0 deletions tests/Countries/JamaicaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

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

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

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

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

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

it('can calculate observed holidays based on year', function () {

// Check for Observed New Year's Day
$holidays = Holidays::for(country: 'jm', year: 2023)->get();
expect(array_search('New Year\'s Day Observed', array_column($holidays, 'name')))->toBeInt();

// Check for Observed Labour Day
$holidays = Holidays::for(country: 'jm', year: 2020)->get();
expect(array_search('Labour Day Observed', array_column($holidays, 'name')))->toBeInt();

// Check that there is no Observerd New Year's Day
$holidays = Holidays::for(country: 'jm', year: 2024)->get();
expect(array_search('Labour Day Observed', array_column($holidays, 'name')))->toBeFalse();

});