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

Swedish observance holidays #172

Merged
merged 1 commit into from
Sep 6, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Translation for the Easter holiday for the 'fr_FR' locale [\#146](https://github.com/azuyalabs/yasumi/pull/146) ([pioc92](https://github.com/pioc92))
- Translation for the Pentecoste holiday for the 'fr_FR' locale [\#145](https://github.com/azuyalabs/yasumi/pull/145) ([pioc92](https://github.com/pioc92))
- Late Summer Bank Holiday in United Kingdom prior to 1965 [\#161](https://github.com/azuyalabs/yasumi/pull/161) ([c960657](https://github.com/c960657))
- Observance holidays for Sweden [\#172](https://github.com/azuyalabs/yasumi/pull/172) ([c960657](https://github.com/c960657))

### Changed
- Replaced the standard 'InvalidArgumentException' when an invalid year or holiday provider are given by a new exception for each of these two situations separately ('InvalidYearException' and 'ProviderNotFoundException'). This allows you to better distinguish which exception may occur when instantiating the Yasumi class. [\#95](https://github.com/azuyalabs/yasumi/pull/95) ([qneyrat](https://github.com/qneyrat))
Expand Down
94 changes: 87 additions & 7 deletions src/Yasumi/Provider/Sweden.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Yasumi\Provider;

use DateInterval;
use DateTime;
use DateTimeZone;
use Yasumi\Holiday;
Expand Down Expand Up @@ -47,21 +48,80 @@ public function initialize(): void

// Add common Christian holidays (common in Sweden)
$this->addHoliday($this->epiphany($this->year, $this->timezone, $this->locale));
$this->calculateEpiphanyEve();
$this->calculateWalpurgisEve();
$this->addHoliday($this->goodFriday($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->easter($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->easterMonday($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->ascensionDay($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->pentecost($this->year, $this->timezone, $this->locale));
$this->calculateStJohnsDay(); // aka Midsummer's Day
$this->calculateAllSaintsDay();
$this->addHoliday($this->christmasEve($this->year, $this->timezone, $this->locale, Holiday::TYPE_OFFICIAL));
$this->calculateStJohnsHolidays(); // aka Midsummer
$this->calculateAllSaintsHolidays();
$this->addHoliday($this->christmasEve($this->year, $this->timezone, $this->locale, Holiday::TYPE_OBSERVANCE));
$this->addHoliday($this->christmasDay($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->secondChristmasDay($this->year, $this->timezone, $this->locale));

$this->addHoliday($this->newYearsEve($this->year, $this->timezone, $this->locale, Holiday::TYPE_OBSERVANCE));

// Calculate other holidays
$this->calculateNationalDay();
}

/**
* Epiphany Eve.
*
* Epiphany is a Christian feast day that celebrates the revelation of God the Son as a human being in Jesus Christ.
* The traditional date for the feast is January 6. In Sweden the holiday is celebrated on the evening before, also
* known as Twelfth Night.
*
* Epiphany Eve is often treated with the afternoon off, but this varies depending on employer.
*
* @link https://en.wikipedia.org/wiki/Twelfth_Night_(holiday)
*
* @throws \Yasumi\Exception\InvalidDateException
* @throws \Yasumi\Exception\UnknownLocaleException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function calculateEpiphanyEve(): void
{
$this->addHoliday(new Holiday(
'epiphanyEve',
[],
new DateTime("$this->year-1-5", new DateTimeZone($this->timezone)),
$this->locale,
Holiday::TYPE_OBSERVANCE
));
}


/**
* Walpurgis Night.
*
* Walpurgis Night is the eve of the Christian feast day of Saint Walpurga, an 8th-century abbess in Francia.
* This feast commemorates the canonization of Saint Walpurga and the movement of her relics to Eichstätt,
* both of which occurred on 1 May 870
*
* Walpurgis Night is often treated with the afternoon off, but this varies depending on employer.
*
* @link https://en.wikipedia.org/wiki/Walpurgis_Night
*
* @throws \Yasumi\Exception\InvalidDateException
* @throws \Yasumi\Exception\UnknownLocaleException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function calculateWalpurgisEve(): void
{
$this->addHoliday(new Holiday(
'walpurgisEve',
[],
new DateTime("$this->year-4-30", new DateTimeZone($this->timezone)),
$this->locale,
Holiday::TYPE_OBSERVANCE
));
}

/**
* St. John's Day / Midsummer.
*
Expand All @@ -81,14 +141,24 @@ public function initialize(): void
* @throws \Yasumi\Exception\UnknownLocaleException
* @throws \Exception
*/
private function calculateStJohnsDay(): void
private function calculateStJohnsHolidays(): void
{
$date = new DateTime("$this->year-6-20 this saturday", new DateTimeZone($this->timezone));
$this->addHoliday(new Holiday(
'stJohnsDay',
[],
new DateTime("$this->year-6-20 this saturday", new DateTimeZone($this->timezone)),
$date,
$this->locale
));

$date->sub(new DateInterval('P1D'));
$this->addHoliday(new Holiday(
'stJohnsEve',
[],
$date,
$this->locale,
Holiday::TYPE_OBSERVANCE
));
}

/**
Expand All @@ -112,14 +182,24 @@ private function calculateStJohnsDay(): void
* @throws \Yasumi\Exception\UnknownLocaleException
* @throws \Exception
*/
private function calculateAllSaintsDay(): void
private function calculateAllSaintsHolidays(): void
{
$date = new DateTime("$this->year-10-31 this saturday", new DateTimeZone($this->timezone));
$this->addHoliday(new Holiday(
'allSaintsDay',
[],
new DateTime("$this->year-10-31 this saturday", new DateTimeZone($this->timezone)),
$date,
$this->locale
));

$date->sub(new DateInterval('P1D'));
$this->addHoliday(new Holiday(
'allSaintsEve',
[],
$date,
$this->locale,
Holiday::TYPE_OBSERVANCE
));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Yasumi/data/translations/allSaintsEve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2019 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <[email protected]>
*/

// Translations for All Saints' Eve
return [
'da_DK' => 'Allehelgensaften',
'en_US' => 'All Saints\' Eve',
'sv_SE' => 'alla helgons afton',
];
18 changes: 18 additions & 0 deletions src/Yasumi/data/translations/epiphanyEve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2019 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <[email protected]>
*/

// Translations for Epiphany Eve
return [
'da_DK' => 'Helligtrekongersaften',
'en_US' => 'Epiphany Eve',
'sv_SE' => 'trettondagsafton',
];
1 change: 1 addition & 0 deletions src/Yasumi/data/translations/newYearsEve.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
'en_US' => 'New Year\'s Eve',
'ko_KR' => '신년전야',
'lv_LV' => 'Vecgada vakars',
'sv_SE' => 'nyårsafton',
];
1 change: 1 addition & 0 deletions src/Yasumi/data/translations/stJohnsDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// Translations for St. John's Day
return [
'da_DK' => 'Sankthansaften',
'el_GR' => 'Σύναξις Προφήτου Προδρόμου και Βαπτιστού Ιωάννου',
'en_US' => 'St. John\'s Day',
'es_ES' => 'Sant Joan',
Expand Down
18 changes: 18 additions & 0 deletions src/Yasumi/data/translations/stJohnsEve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2019 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <[email protected]>
*/

// Translations for St. John's Eve
return [
'da_DK' => 'Sankthansaften',
'en_US' => 'St. John\'s Eve',
'sv_SE' => 'midsommarafton',
];
17 changes: 17 additions & 0 deletions src/Yasumi/data/translations/walpurgisEve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2019 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <[email protected]>
*/

// Translations for Walpurgis Night
return [
'en_US' => 'Walpurgis Night',
'sv_SE' => 'valborgsmässoafton',
];
95 changes: 95 additions & 0 deletions tests/Sweden/AllSaintsEveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2019 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <[email protected]>
*/

namespace Yasumi\tests\Sweden;

use DateInterval;
use DateTime;
use DateTimeZone;
use Yasumi\Holiday;
use Yasumi\tests\YasumiTestCaseInterface;

/**
* Class for testing All Saints' Eve in Sweden.
*/
class AllSaintsEveTest extends SwedenBaseTestCase implements YasumiTestCaseInterface
{
/**
* The name of the holiday to be tested
*/
public const HOLIDAY = 'allSaintsEve';

/**
* Tests the holiday defined in this test.
*
* @dataProvider HolidayDataProvider
*
* @param int $year the year for which the holiday defined in this test needs to be tested
* @param DateTime $expected the expected date
*
* @throws \ReflectionException
*/
public function testHoliday($year, $expected)
{
$this->assertHoliday(self::REGION, self::HOLIDAY, $year, $expected);
}

/**
* Returns a list of random test dates used for assertion of the holiday defined in this test
*
* @return array list of test dates for the holiday defined in this test
* @throws \Exception
*/
public function HolidayDataProvider(): array
{
$data = [];

for ($y = 0; $y < 50; $y++) {
$year = $this->generateRandomYear();
$date = new DateTime("$year-10-30", new DateTimeZone(self::TIMEZONE));

// Check between 30 October and 5th of November the day that is a Friday
for ($d = 0; $d <= 7; ++$d) {
if ($date->format('l') === 'Friday') {
$data[] = [$year, $date];
break;
}
$date->add(new DateInterval('P1D'));
}
}

return $data;
}

/**
* Tests the translated name of the holiday defined in this test.
* @throws \ReflectionException
*/
public function testTranslation(): void
{
$this->assertTranslatedHolidayName(
self::REGION,
self::HOLIDAY,
$this->generateRandomYear(),
[self::LOCALE => 'alla helgons afton']
);
}

/**
* Tests type of the holiday defined in this test.
* @throws \ReflectionException
*/
public function testHolidayType(): void
{
$this->assertHolidayType(self::REGION, self::HOLIDAY, $this->generateRandomYear(), Holiday::TYPE_OBSERVANCE);
}
}
2 changes: 1 addition & 1 deletion tests/Sweden/ChristmasEveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public function testTranslation(): void
*/
public function testHolidayType(): void
{
$this->assertHolidayType(self::REGION, self::HOLIDAY, $this->generateRandomYear(), Holiday::TYPE_OFFICIAL);
$this->assertHolidayType(self::REGION, self::HOLIDAY, $this->generateRandomYear(), Holiday::TYPE_OBSERVANCE);
}
}
Loading