Skip to content

Commit

Permalink
Add support for Swedish holidays (#199)
Browse files Browse the repository at this point in the history
* Add support for Swedish holidays

* Remove timezones
  • Loading branch information
adevade authored Feb 1, 2024
1 parent 9896b2f commit 6c140a5
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Countries/Sweden.php
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,
];
}
}
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"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/SwedenTest.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 swedish holidays', function () {
CarbonImmutable::setTestNow('2024-01-01');

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

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

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

0 comments on commit 6c140a5

Please sign in to comment.