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

Add Tanzania national holiday #98

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
99 changes: 99 additions & 0 deletions src/Countries/Tanzania.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use IntlCalendar;

class Tanzania extends Country
{
public function countryCode(): string
{
return 'tz';
}

/** @return array<string, string|CarbonImmutable> */
alphaolomi marked this conversation as resolved.
Show resolved Hide resolved
protected function allHolidays(int $year): array
{
return array_merge([
'New Year\'s Day' => '01-01',
'Labor Day' => '05-01',
'Saba Saba Day' => '07-07',
'Farmers Day (Nane Nane Day)' => '08-08',
'Christmas Day' => '12-25',
'Boxing Day' => '12-26'
], $this->variableHolidays($year));
}

/** @return array<string, string|CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easter = CarbonImmutable::createFromTimestamp(easter_date($year))
alphaolomi marked this conversation as resolved.
Show resolved Hide resolved
->setTimezone('Africa/Dar_es_Salaam');

// Eid al-Fitr is on the first day of the 10th month in the calendar
alphaolomi marked this conversation as resolved.
Show resolved Hide resolved
// since the PHP array index begins at 0, it is the 9th month
$eid_al_fitr_cal = IntlCalendar::createInstance('Africa/Dar_es_Salaam', '@calendar=islamic-rgsa');
$eid_al_fitr_cal->set(IntlCalendar::FIELD_MONTH, 9);
$eid_al_fitr_cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 1);
$eid_al_fitr_cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$eid_al_fitr_cal->clear(IntlCalendar::FIELD_MINUTE);
$eid_al_fitr_cal->clear(IntlCalendar::FIELD_SECOND);
$eid_al_fitr_cal->clear(IntlCalendar::FIELD_MILLISECOND);

// Eid al-Hajj is on the 10th day of the Dhu al-Hijja the 12th month in the calendar
// since the PHP array index begins at 0, it is the 11th month
$eid_al_hajj_cal = IntlCalendar::createInstance('Africa/Dar_es_Salaam', '@calendar=islamic-civil');
$eid_al_hajj_cal->set(IntlCalendar::FIELD_MONTH, 11);
$eid_al_hajj_cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 10);
$eid_al_hajj_cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$eid_al_hajj_cal->clear(IntlCalendar::FIELD_MINUTE);
$eid_al_hajj_cal->clear(IntlCalendar::FIELD_SECOND);
$eid_al_hajj_cal->clear(IntlCalendar::FIELD_MILLISECOND);

// Eid-e-Milad an-Nabi (Mawlid) is on the 12th day of the 3rd month in the calendar
// since the PHP array index begins at 0, it is the 11th month
$mawlid_cal = IntlCalendar::createInstance('Africa/Dar_es_Salaam', '@calendar=islamic-civil');
$mawlid_cal->set(IntlCalendar::FIELD_MONTH, 2);
$mawlid_cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 12);
$mawlid_cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$mawlid_cal->clear(IntlCalendar::FIELD_MINUTE);
$mawlid_cal->clear(IntlCalendar::FIELD_SECOND);
$mawlid_cal->clear(IntlCalendar::FIELD_MILLISECOND);

$variable_holidays = [
'Easter Monday' => $easter->addDay(),
'Good Friday' => $easter->addDays(-2),
'Eid al-Fitr' => CarbonImmutable::createFromTimestamp($eid_al_fitr_cal->toDateTime()->getTimestamp()),
'Eid al-Hajj' => CarbonImmutable::createFromTimestamp($eid_al_hajj_cal->toDateTime()->getTimestamp()),
'Maulid Day' => CarbonImmutable::createFromTimestamp($mawlid_cal->toDateTime()->getTimestamp()),
];

// Zanzibar Revolutionary Day celebrations started in 1964
if ($year >= 1964) {
$variable_holidays['Zanzibar Revolutionary Day'] = '01-12';
}

// Karume Day celebrations started in 1973
if ($year >= 1973) {
$variable_holidays['Karume Day'] = '04-07';
}

// 'Union Day celebrations started in 1964
if ($year >= 1964) {
$variable_holidays['Union Day'] = '04-26';
}

// Nyerere Day day celebrations started in 2000
if ($year >= 2000) {
$variable_holidays['Nyerere Day'] = '10-14';
}

// Independence Day celebrations started in 1961
if ($year >= 1961) {
$variable_holidays['Independence Day'] = '12-09';
}

return $variable_holidays;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
{
"name": "Maulid Day",
"date": "2023-09-27"
},
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Zanzibar Revolutionary Day",
"date": "2024-01-12"
},
{
"name": "Good Friday",
"date": "2024-03-29"
},
{
"name": "Easter Monday",
"date": "2024-04-01"
},
{
"name": "Karume Day",
"date": "2024-04-07"
},
{
"name": "Eid al-Fitr",
"date": "2024-04-09"
},
{
"name": "Union Day",
"date": "2024-04-26"
},
{
"name": "Labor Day",
"date": "2024-05-01"
},
{
"name": "Eid al-Hajj",
"date": "2024-06-17"
},
{
"name": "Saba Saba Day",
"date": "2024-07-07"
},
{
"name": "Farmers Day (Nane Nane Day)",
"date": "2024-08-08"
},
{
"name": "Nyerere Day",
"date": "2024-10-14"
},
{
"name": "Independence Day",
"date": "2024-12-09"
},
{
"name": "Christmas Day",
"date": "2024-12-25"
},
{
"name": "Boxing Day",
"date": "2024-12-26"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/TanzaniaTest.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 tanzania holidays', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01');

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

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

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