generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Swedish holidays (#199)
* Add support for Swedish holidays * Remove timezones
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
|
||
class Sweden extends Country | ||
{ | ||
public function countryCode(): string | ||
{ | ||
return 'se'; | ||
} | ||
|
||
protected function allHolidays(int $year): array | ||
{ | ||
return array_merge([ | ||
'Nyårsdagen' => '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<string, CarbonImmutable> */ | ||
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, | ||
]; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/.pest/snapshots/Countries/SwedenTest/it_can_calculate_swedish_holidays.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 swedish holidays', function () { | ||
CarbonImmutable::setTestNow('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'se')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
}); |