Skip to content

Commit

Permalink
Merge pull request #13 from badams/new_zealand_holiday_provider
Browse files Browse the repository at this point in the history
Added Holiday Provider for New Zealand
  • Loading branch information
stelgenhof committed Apr 9, 2016
2 parents 673e3e3 + 8b42aa6 commit 7507e45
Show file tree
Hide file tree
Showing 17 changed files with 1,105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<directory suffix="Test.php">./tests/Greece</directory>
</testsuite>

<!-- Test Suite for holidays in New Zealand -->
<testsuite name="NewZealand">
<directory suffix="Test.php">./tests/NewZealand</directory>
</testsuite>

</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
218 changes: 218 additions & 0 deletions src/Yasumi/Provider/NewZealand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2016 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <stelgenhof@gmail.com>
*/
namespace Yasumi\Provider;

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

/**
* Provider for all holidays in New Zealand.
*/
class NewZealand extends AbstractProvider
{
use CommonHolidays, ChristianHolidays;

/**
* Initialize holidays for New Zealand.
*/
public function initialize()
{
$this->timezone = 'Pacific/Auckland';

// National Holidays
$this->calculateNewYearHolidays();
$this->calculateWaitangiDay();
$this->calculateAnzacDay();
$this->calculateQueensBirthday();
$this->calculateLabourDay();

// Add Christian holidays
$this->addHoliday($this->goodFriday($this->year, $this->timezone, $this->locale));
$this->addHoliday($this->easterMonday($this->year, $this->timezone, $this->locale));
$this->calculateChristmasHolidays();
}

/**
* Waitangi Day
*
* Waitangi Day (named after Waitangi, where the Treaty of Waitangi was first signed)
* commemorates a significant day in the history of New Zealand. It is observed as a public holiday each
* year on 6 February to celebrate the signing of the Treaty of Waitangi, New Zealand's founding document,
* on that date in 1840. In recent legislation, if 6 February falls on a Saturday or Sunday,
* the Monday that immediately follows becomes a public holiday.
*
* @link https://en.wikipedia.org/wiki/Waitangi_Day
* @link http://employment.govt.nz/er/holidaysandleave/publicholidays/mondayisation.asp
*/
public function calculateWaitangiDay()
{
if ($this->year < 1974) {
return;
}

$date = new DateTime("$this->year-02-6", new DateTimeZone($this->timezone));

if ($this->year >= 2015 && !$this->isWorkingDay($date)) {
$date->modify('next monday');
}

$this->addHoliday(new Holiday('waitangiDay', [], $date, $this->locale));
}

/**
* Christmas Day / Boxing Day
*
* Christmas day, and Boxing day are public holidays in New Zealand,
* they are subject to mondayisation rules.
*
* @link http://www.timeanddate.com/holidays/new-zealand/boxing-day
* @link http://www.timeanddate.com/holidays/new-zealand/christmas-day
* @link http://employment.govt.nz/er/holidaysandleave/publicholidays/mondayisation.asp
*/
public function calculateChristmasHolidays()
{
$christmasDay = new DateTime("$this->year-12-25", new DateTimeZone($this->timezone));
$boxingDay = new DateTime("$this->year-12-26", new DateTimeZone($this->timezone));

switch ($christmasDay->format('w')) {
case 0:
$christmasDay->add(new DateInterval('P2D'));
break;
case 5:
$boxingDay->add(new DateInterval('P2D'));
break;
case 6:
$christmasDay->add(new DateInterval('P2D'));
$boxingDay->add(new DateInterval('P2D'));
break;
}

$this->addHoliday(new Holiday('christmasDay', [], $christmasDay, $this->locale));
$this->addHoliday(new Holiday('secondChristmasDay', [], $boxingDay, $this->locale));
}

/**
* Holidays associated with the start of the modern Gregorian calendar
*
* New Zealanders celebrate New Years Day and The Day After New Years Day,
* if either of these holidays occur on a weekend, the dates need to be adjusted.
*
* @link https://en.wikipedia.org/wiki/Public_holidays_in_New_Zealand#Statutory_holidays
* @link http://www.timeanddate.com/holidays/new-zealand/new-year-day
* @link http://www.timeanddate.com/holidays/new-zealand/day-after-new-years-day
* @link http://employment.govt.nz/er/holidaysandleave/publicholidays/mondayisation.asp
*/
public function calculateNewYearHolidays()
{
$newYearsDay = new DateTime("$this->year-01-01", new DateTimeZone($this->timezone));
$dayAfterNewYearsDay = new DateTime("$this->year-01-02", new DateTimeZone($this->timezone));

switch ($newYearsDay->format('w')) {
case 0:
$newYearsDay->add(new DateInterval('P1D'));
$dayAfterNewYearsDay->add(new DateInterval('P1D'));
break;
case 5:
$dayAfterNewYearsDay->add(new DateInterval('P2D'));
break;
case 6:
$newYearsDay->add(new DateInterval('P2D'));
$dayAfterNewYearsDay->add(new DateInterval('P2D'));
break;
}

$this->addHoliday(new Holiday('newYearsDay', [], $newYearsDay, $this->locale));
$this->addHoliday(new Holiday('dayAfterNewYearsDay', [], $dayAfterNewYearsDay, $this->locale));
}

/**
* ANZAC Day
*
* Anzac Day is a national day of remembrance in Australia and New Zealand that broadly commemorates all Australians
* and New Zealanders "who served and died in all wars, conflicts, and peacekeeping operations"
* Observed on 25 April each year.
*
* @link https://en.wikipedia.org/wiki/Anzac_Day
* @link http://employment.govt.nz/er/holidaysandleave/publicholidays/mondayisation.asp
*/
public function calculateAnzacDay()
{
if ($this->year < 1921) {
return;
}

$date = new DateTime("$this->year-04-25", new DateTimeZone($this->timezone));

if ($this->year >= 2015 && !$this->isWorkingDay($date)) {
$date->modify('next monday');
}

$this->addHoliday(new Holiday('anzacDay', [], $date, $this->locale));
}

/**
* Queens Birthday
*
* The official head of state of New Zealand is the Monarch of the Commonwealth Realms.
* The monarch's birthday is officially celebrated in many parts of New Zealand.
* On her accession in 1952 Queen Elizabeth II was proclaimed in New Zealand ‘Queen of this Realm and all her
* other Realms’. Her representative in New Zealand, the governor general, has symbolic and ceremonial roles
* and is not involved in the day-to-day running of the government, which is the domain of the prime minister.
*
* Her actual birthday is on April 21, but it's celebrated as a public holiday on the first Monday of June.
*
* @link http://www.timeanddate.com/holidays/new-zealand/queen-birthday
*/
public function calculateQueensBirthday()
{
if ($this->year < 1952) {
return;
}

$this->addHoliday(new Holiday(
'queensBirthday',
[],
new DateTime("first monday of june $this->year", new DateTimeZone($this->timezone)),
$this->locale
));
}

/**
* During the 19th century, workers in New Zealand tried to claim the right for an 8-hour working day.
* In 1840 carpenter Samuel Parnell fought for this right in Wellington, NZ, and won.
* Labour Day was first celebrated in New Zealand on October 28, 1890, when thousands of workers paraded in the
* main city centres.
* Government employees were given the day off to attend the parades and many businesses closed for at least part
* of the day.
*
* The first official Labour Day public holiday in New Zealand was celebrated on the
* second Wednesday in October in 1900. The holiday was moved to the fourth Monday of October in 1910
* has remained on this date since then.
*
* @link http://www.timeanddate.com/holidays/new-zealand/labour-day
*/
public function calculateLabourDay()
{
if ($this->year < 1900) {
return;
}

$date = new DateTime(
(($this->year < 1910) ? 'second wednesday of october' : 'fourth monday of october') . " $this->year",
new DateTimeZone($this->timezone)
);

$this->addHoliday(new Holiday('labourDay', [], $date, $this->locale));
}
}
16 changes: 16 additions & 0 deletions src/Yasumi/data/translations/anzacDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2016 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <stelgenhof@gmail.com>
*/

// Translation for ANZAC Day
return [
'en_US' => 'ANZAC Day',
];
16 changes: 16 additions & 0 deletions src/Yasumi/data/translations/dayAfterNewYearsDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2016 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <stelgenhof@gmail.com>
*/

// Translation for Day after New Year's Day
return [
'en_US' => 'Day after New Year\'s Day',
];
16 changes: 16 additions & 0 deletions src/Yasumi/data/translations/waitangiDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2016 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <stelgenhof@gmail.com>
*/

// Translation for Waitangi Day
return [
'en_US' => 'Waitangi Day',
];
86 changes: 86 additions & 0 deletions tests/NewZealand/AnzacDayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* This file is part of the Yasumi package.
*
* Copyright (c) 2015 - 2016 AzuyaLabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Sacha Telgenhof <stelgenhof@gmail.com>
*/

namespace Yasumi\tests\NewZealand;

use DateTime;
use DateTimeZone;

/**
* Class for testing ANZAC day in the New Zealand.
*/
class AnzacDayTest extends NewZealandBaseTestCase
{
/**
* The name of the holiday
*/
const HOLIDAY = 'anzacDay';

/**
* Tests ANZAC Day
*
* @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
*/
public function testHoliday($year, $expected)
{
$this->assertHoliday(self::REGION, self::HOLIDAY, $year,
new DateTime($expected, new DateTimeZone(self::TIMEZONE)));
}

/**
* Tests that Labour Day is not present before 1921
*/
public function testNotHoliday()
{
$this->assertNotHoliday(self::REGION, self::HOLIDAY, 1920);
}

/**
* Tests the translated name of the holiday defined in this test.
*/
public function testTranslation()
{
$this->assertTranslatedHolidayName(
self::REGION,
self::HOLIDAY,
$this->generateRandomYear(1921),
['en_US' => 'ANZAC Day']
);
}

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

for ($i = 0; $i < 100; $i++) {
$year = $this->generateRandomYear(1921, 2100);
$date = new DateTime("$year-04-25", new DateTimeZone(self::TIMEZONE));

// in 2015 some policy was introduced to make sure this holiday was celebrated during the working week.
if ($year >= 2015 && in_array($date->format('w'), [0, 6])) {
$date->modify('next monday');
}

$data[] = [$year, $date->format('Y-m-d')];
}

return $data;
}
}
Loading

0 comments on commit 7507e45

Please sign in to comment.