generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tanzania national and religious holiday
Co-Authored-By: Michael Omakei <[email protected]>
- Loading branch information
1 parent
40e12a4
commit 9f54f4f
Showing
3 changed files
with
183 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,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> */ | ||
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)) | ||
->setTimezone('Africa/Dar_es_Salaam'); | ||
|
||
// Eid al-Fitr is on the first day of the 10th month in the calendar | ||
// 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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/.pest/snapshots/Countries/TanzaniaTest/it_can_calculate_tanzania_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,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" | ||
} | ||
] |
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 tanzania holidays', function () { | ||
CarbonImmutable::setTestNowAndTimezone('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'tz')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
}); |