From 6c140a5336c51f4162cb93c764c4168df452f369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= <1066486+adevade@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:39:09 +0100 Subject: [PATCH] Add support for Swedish holidays (#199) * Add support for Swedish holidays * Remove timezones --- src/Countries/Sweden.php | 57 +++++++++++++++++++ .../it_can_calculate_swedish_holidays.snap | 54 ++++++++++++++++++ tests/Countries/SwedenTest.php | 18 ++++++ 3 files changed, 129 insertions(+) create mode 100644 src/Countries/Sweden.php create mode 100644 tests/.pest/snapshots/Countries/SwedenTest/it_can_calculate_swedish_holidays.snap create mode 100644 tests/Countries/SwedenTest.php diff --git a/src/Countries/Sweden.php b/src/Countries/Sweden.php new file mode 100644 index 000000000..dc4a5b888 --- /dev/null +++ b/src/Countries/Sweden.php @@ -0,0 +1,57 @@ + '01-01', + 'Trettondedag jul' => '01-06', + 'Första maj' => '05-1', + 'Nationaldagen' => '06-6', + 'Juldagen' => '12-25', + 'Annandag jul' => '12-26', + ], $this->variableHolidays($year)); + } + + /** @return array */ + protected function variableHolidays(int $year): array + { + $easter = $this->easter($year); + + /** @var CarbonImmutable $midsummerDay */ + $midsummerDay = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-06-20"); + + if (! $midsummerDay->isSaturday()) { + $midsummerDay = $midsummerDay->next(CarbonImmutable::SATURDAY); + } + + /** @var CarbonImmutable $halloween */ + $halloween = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-10-31"); + + if (! $halloween->isSaturday()) { + $halloween = $halloween->next(CarbonImmutable::SATURDAY); + } + + return [ + 'Långfredagen' => $easter->subDays(2), + 'Påskdagen' => $easter, + 'Annandag påsk' => $easter->addDay(), + 'Kristi himmelsfärdsdag' => $easter->addDays(39), + 'Pingstdagen' => $easter->addDays(49), + 'Midsommardagen' => $midsummerDay->day > 26 + ? $midsummerDay->subWeek() + : $midsummerDay, + 'Alla helgons dag' => $halloween, + ]; + } +} diff --git a/tests/.pest/snapshots/Countries/SwedenTest/it_can_calculate_swedish_holidays.snap b/tests/.pest/snapshots/Countries/SwedenTest/it_can_calculate_swedish_holidays.snap new file mode 100644 index 000000000..c22a86d64 --- /dev/null +++ b/tests/.pest/snapshots/Countries/SwedenTest/it_can_calculate_swedish_holidays.snap @@ -0,0 +1,54 @@ +[ + { + "name": "Ny\u00e5rsdagen", + "date": "2024-01-01" + }, + { + "name": "Trettondedag jul", + "date": "2024-01-06" + }, + { + "name": "L\u00e5ngfredagen", + "date": "2024-03-29" + }, + { + "name": "P\u00e5skdagen", + "date": "2024-03-31" + }, + { + "name": "Annandag p\u00e5sk", + "date": "2024-04-01" + }, + { + "name": "F\u00f6rsta maj", + "date": "2024-05-01" + }, + { + "name": "Kristi himmelsf\u00e4rdsdag", + "date": "2024-05-09" + }, + { + "name": "Pingstdagen", + "date": "2024-05-19" + }, + { + "name": "Nationaldagen", + "date": "2024-06-06" + }, + { + "name": "Midsommardagen", + "date": "2024-06-22" + }, + { + "name": "Alla helgons dag", + "date": "2024-11-02" + }, + { + "name": "Juldagen", + "date": "2024-12-25" + }, + { + "name": "Annandag jul", + "date": "2024-12-26" + } +] \ No newline at end of file diff --git a/tests/Countries/SwedenTest.php b/tests/Countries/SwedenTest.php new file mode 100644 index 000000000..5505f368d --- /dev/null +++ b/tests/Countries/SwedenTest.php @@ -0,0 +1,18 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); +});