From 3424d466cf1f276071f5c47b384baed1966a215b Mon Sep 17 00:00:00 2001 From: Orane Edwards Date: Thu, 25 Jan 2024 00:18:48 -0500 Subject: [PATCH 1/5] Add Jamaican holidays --- src/Countries/Jamaica.php | 41 ++++++++++++++++++ .../it_can_calculate_jamaican_holidays.snap | 42 +++++++++++++++++++ tests/Countries/JamaicaTest.php | 18 ++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/Countries/Jamaica.php create mode 100644 tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap create mode 100644 tests/Countries/JamaicaTest.php diff --git a/src/Countries/Jamaica.php b/src/Countries/Jamaica.php new file mode 100644 index 000000000..4c54fc8fe --- /dev/null +++ b/src/Countries/Jamaica.php @@ -0,0 +1,41 @@ + '01-01', + 'Labour Day' => '05-23', + 'Emancipation Day' => '08-01', + 'Independence Day' => '08-06', + 'National Heroes Day' => '10-18', + 'Christmas Day' => '12-25', + 'Boxing Day' => '12-26', + ], $this->variableHolidays($year)); + + return $holidays; + } + + /** @return array */ + protected function variableHolidays(int $year): array + { + + $easter = $this->easter($year); + + return [ + 'Ash Wednesday' => $easter->subDays(46), + 'Good Friday' => $easter->subDays(2), + 'Easter Monday' => $easter->addDay(), + ]; + } +} diff --git a/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap b/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap new file mode 100644 index 000000000..c6f28a4a2 --- /dev/null +++ b/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap @@ -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-18" + }, + { + "name": "Christmas Day", + "date": "2024-12-25" + }, + { + "name": "Boxing Day", + "date": "2024-12-26" + } +] \ No newline at end of file diff --git a/tests/Countries/JamaicaTest.php b/tests/Countries/JamaicaTest.php new file mode 100644 index 000000000..cacf35182 --- /dev/null +++ b/tests/Countries/JamaicaTest.php @@ -0,0 +1,18 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); +}); From 941a387b10cb26986ffef30cad825ef9dc114bdc Mon Sep 17 00:00:00 2001 From: Orane Edwards Date: Sat, 27 Jan 2024 10:44:08 -0500 Subject: [PATCH 2/5] Add observed holidays for Jamaica. --- src/Countries/Jamaica.php | 50 +++++++++++++++++-- .../it_can_calculate_jamaican_holidays.snap | 2 +- tests/Countries/JamaicaTest.php | 26 ++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/Countries/Jamaica.php b/src/Countries/Jamaica.php index 4c54fc8fe..2b8e77afe 100644 --- a/src/Countries/Jamaica.php +++ b/src/Countries/Jamaica.php @@ -13,15 +13,27 @@ public function countryCode(): string protected function allHolidays(int $year): array { - $holidays = array_merge([ + + $holidays = array_merge( + $this->fixedHolidays(), + $this->variableHolidays($year), + $this->observedHolidays($year) + ); + + return $holidays; + } + + /** @return array */ + protected function fixedHolidays(): array + { + $holidays = [ 'New Year\'s Day' => '01-01', 'Labour Day' => '05-23', 'Emancipation Day' => '08-01', 'Independence Day' => '08-06', - 'National Heroes Day' => '10-18', 'Christmas Day' => '12-25', 'Boxing Day' => '12-26', - ], $this->variableHolidays($year)); + ]; return $holidays; } @@ -29,13 +41,43 @@ protected function allHolidays(int $year): array /** @return array */ protected function variableHolidays(int $year): array { - $easter = $this->easter($year); + $heroesDay = new CarbonImmutable("third monday of October $year", 'America/Jamaica'); return [ 'Ash Wednesday' => $easter->subDays(46), 'Good Friday' => $easter->subDays(2), 'Easter Monday' => $easter->addDay(), + 'National Heroes Day' => $heroesDay, ]; } + + /** @return array */ + protected function observedHolidays(int $year): array + { + + $observedHolidays = []; + + foreach ($this->fixedHolidays() as $name => $date) { + $date = CarbonImmutable::createFromFormat('Y-m-d', "{$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; + } } diff --git a/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap b/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap index c6f28a4a2..3d71e806c 100644 --- a/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap +++ b/tests/.pest/snapshots/Countries/JamaicaTest/it_can_calculate_jamaican_holidays.snap @@ -29,7 +29,7 @@ }, { "name": "National Heroes Day", - "date": "2024-10-18" + "date": "2024-10-21" }, { "name": "Christmas Day", diff --git a/tests/Countries/JamaicaTest.php b/tests/Countries/JamaicaTest.php index cacf35182..d19359e94 100644 --- a/tests/Countries/JamaicaTest.php +++ b/tests/Countries/JamaicaTest.php @@ -16,3 +16,29 @@ expect(formatDates($holidays))->toMatchSnapshot(); }); + +it('can calculate correct day of week for ash wendesday', function ($year) { + CarbonImmutable::setTestNowAndTimezone($year.'-01-01'); + + $holidays = Holidays::for(country: 'jm')->get(); + $ashWednesday = $holidays[array_search('Ash Wednesday', array_column($holidays, 'name'))]; + + expect($ashWednesday['date']->dayOfWeek)->toBe(3); + +})->with([2023, 2024]); + +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(); + +}); From 8242261e815ad13bd57c34e50f8def26c309a9c3 Mon Sep 17 00:00:00 2001 From: Orane Edwards Date: Sat, 27 Jan 2024 19:26:20 -0500 Subject: [PATCH 3/5] Php Stan flagged error. Changed how date is created. --- src/Countries/Jamaica.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Countries/Jamaica.php b/src/Countries/Jamaica.php index 2b8e77afe..010df1957 100644 --- a/src/Countries/Jamaica.php +++ b/src/Countries/Jamaica.php @@ -3,6 +3,7 @@ namespace Spatie\Holidays\Countries; use Carbon\CarbonImmutable; +use Carbon\CarbonInterface; class Jamaica extends Country { @@ -59,7 +60,8 @@ protected function observedHolidays(int $year): array $observedHolidays = []; foreach ($this->fixedHolidays() as $name => $date) { - $date = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}"); + $date = CarbonImmutable::parse("$year-$date") + ->setTimeZone('America/Jamaica'); // If any holiday falls on a Sunday, then it is observed on Monday if($date->dayOfWeek === 0) { From 74751b02fa7b8439d3353fc57b74cf93dbc31056 Mon Sep 17 00:00:00 2001 From: Orane Edwards Date: Sat, 27 Jan 2024 19:53:19 -0500 Subject: [PATCH 4/5] Remove unnecessary test for Ash Wednesday. --- tests/Countries/JamaicaTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/Countries/JamaicaTest.php b/tests/Countries/JamaicaTest.php index d19359e94..1ea7b0237 100644 --- a/tests/Countries/JamaicaTest.php +++ b/tests/Countries/JamaicaTest.php @@ -17,16 +17,6 @@ expect(formatDates($holidays))->toMatchSnapshot(); }); -it('can calculate correct day of week for ash wendesday', function ($year) { - CarbonImmutable::setTestNowAndTimezone($year.'-01-01'); - - $holidays = Holidays::for(country: 'jm')->get(); - $ashWednesday = $holidays[array_search('Ash Wednesday', array_column($holidays, 'name'))]; - - expect($ashWednesday['date']->dayOfWeek)->toBe(3); - -})->with([2023, 2024]); - it('can calculate observed holidays based on year', function () { // Check for Observed New Year's Day From f15956314c9f0de97c84ca73fd629a9e6ab25e0a Mon Sep 17 00:00:00 2001 From: Orane Edwards Date: Sat, 27 Jan 2024 20:01:30 -0500 Subject: [PATCH 5/5] Remove timezone specification. --- src/Countries/Jamaica.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Countries/Jamaica.php b/src/Countries/Jamaica.php index 010df1957..a6c955580 100644 --- a/src/Countries/Jamaica.php +++ b/src/Countries/Jamaica.php @@ -43,7 +43,7 @@ protected function fixedHolidays(): array protected function variableHolidays(int $year): array { $easter = $this->easter($year); - $heroesDay = new CarbonImmutable("third monday of October $year", 'America/Jamaica'); + $heroesDay = new CarbonImmutable("third monday of October $year"); return [ 'Ash Wednesday' => $easter->subDays(46), @@ -60,8 +60,7 @@ protected function observedHolidays(int $year): array $observedHolidays = []; foreach ($this->fixedHolidays() as $name => $date) { - $date = CarbonImmutable::parse("$year-$date") - ->setTimeZone('America/Jamaica'); + $date = CarbonImmutable::parse("$year-$date"); // If any holiday falls on a Sunday, then it is observed on Monday if($date->dayOfWeek === 0) {