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 Tunisia Holidays #109

Merged
merged 31 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
abd492d
add Tunisia Holidays
mr-wolf-gb Jan 18, 2024
ff904a6
fix Tunisia Hijri Holidays
mr-wolf-gb Jan 18, 2024
ea6d1af
fix PHPStan error
mr-wolf-gb Jan 21, 2024
4aae5f9
sync latest commits
mr-wolf-gb Jan 31, 2024
a27dfe5
change Hijri Dates to generated Like in turkey holidays
mr-wolf-gb Jan 31, 2024
0b37ecb
Merge branch 'main' into main
Jan 31, 2024
30af822
minor fixes
mr-wolf-gb Jan 31, 2024
45d77ec
minor fixes
mr-wolf-gb Jan 31, 2024
ce11a5d
fix phpstan error
mr-wolf-gb Jan 31, 2024
616f4d1
fix phpstan error
mr-wolf-gb Jan 31, 2024
b98f93d
Merge branch 'spatie:main' into main
Feb 1, 2024
074767d
implement turkey method as trait for Hijri holidays
mr-wolf-gb Feb 1, 2024
19f83fa
Merge branch 'spatie:main' into main
Feb 1, 2024
342a574
minor fix
mr-wolf-gb Feb 1, 2024
4f96329
Merge remote-tracking branch 'origin/main'
mr-wolf-gb Feb 1, 2024
c38bd7a
move tunisia translations to correct dir
mr-wolf-gb Feb 1, 2024
707db3e
Merge branch 'spatie:main' into main
Feb 1, 2024
09e1ef5
Merge branch 'spatie:main' into main
Feb 5, 2024
8a40743
Merge branch 'main' into main
mr-wolf-gb Feb 11, 2024
c25e1e4
Update src/Countries/Tunisia.php
mr-wolf-gb Feb 12, 2024
d8ed1e7
Update src/Countries/Tunisia.php
mr-wolf-gb Feb 12, 2024
7ff692a
Update tests/Countries/TunisiaTest.php
mr-wolf-gb Feb 12, 2024
fae4fce
Update tests/Countries/TunisiaTest.php
mr-wolf-gb Feb 12, 2024
7685704
remove HijriCalendar
mr-wolf-gb Feb 12, 2024
80ef304
Fix phpstan fail test
mr-wolf-gb Feb 12, 2024
f7c8094
Fix phpstan Ternary operator condition is always true.
mr-wolf-gb Feb 12, 2024
404e998
Merge branch 'spatie:main' into main
mr-wolf-gb Mar 21, 2024
f31ab11
Merge branch 'spatie:main' into main
mr-wolf-gb Apr 1, 2024
548c21a
Fix code to use IslamicCalendar
mr-wolf-gb Apr 2, 2024
7ed2051
Merge branch 'spatie:main' into main
mr-wolf-gb Apr 3, 2024
db2e6b1
Fix translation
mr-wolf-gb Apr 3, 2024
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
14 changes: 10 additions & 4 deletions lang/tunisia/ar/holidays.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
"Women's Day": "عيد المرأة",
"Evacuation Day": "عيد الجلاء",
"Revolution and Youth Day": "عيد الثورة والشباب",
"Islamic new year": "رأس السنة الهجرية",
"Birthday of the Prophet Mohamed": "المولد النبوي الشريف",
"Eid al-Fitr": "عيد الفطر",
"Eid al-Adha": "عيد الإضحى",
"Eid al-Fitr": "عيد الفطر المبارك",
"Eid al-Fitr Day 2": "عيد الفطر المبارك",
"Eid al-Fitr Day 3": "عيد الفطر المبارك",
"Eid al-Adha": "عيد الأضحى المبارك",
"Eid al-Adha Day 2": "عيد الأضحى المبارك",
"Eid al-Adha Day 3": "عيد الأضحى المبارك",
"Eid al-Adha Day 4": "عيد الأضحى المبارك",
"Islamic New Year": "رأس السنة الهجرية",
"Birthday of the Prophet Muhammad": "المولد النبوي الشريف",
"Arafat Day": "يوم عرفة (وقفة عيد الأضحى)",
"Day": "يوم"
}

10 changes: 8 additions & 2 deletions lang/tunisia/en/holidays.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
"Women's Day": "Women's Day",
"Evacuation Day": "Evacuation Day",
"Revolution and Youth Day": "Revolution and Youth Day",
"Islamic new year": "Islamic new year",
"Birthday of the Prophet Mohamed": "Birthday of the Prophet Mohamed",
"Eid al-Fitr": "Eid al-Fitr",
"Eid al-Fitr Day 2": "Eid al-Fitr",
"Eid al-Fitr Day 3": "Eid al-Fitr",
"Eid al-Adha": "Eid al-Adha",
"Eid al-Adha Day 2": "Eid al-Adha",
"Eid al-Adha Day 3": "Eid al-Adha",
"Eid al-Adha Day 4": "Eid al-Adha",
"Islamic New Year": "Islamic new year",
"Birthday of the Prophet Muhammad": "Birthday of the Prophet Mohamed",
"Arafat Day": "Arafat Day",
"Day": "Day"
}

10 changes: 8 additions & 2 deletions lang/tunisia/fr/holidays.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
"Women's Day": "Fête de la femme",
"Evacuation Day": "Fête d'évacuation",
"Revolution and Youth Day": "Fête de la Révolution et de la Jeunesse",
"Islamic new year": "Nouvel an islamique",
"Birthday of the Prophet Mohamed": "Anniversaire du prophète Mohamed",
"Eid al-Fitr": "Eid al-Fitr",
"Eid al-Fitr Day 2": "Eid al-Fitr",
"Eid al-Fitr Day 3": "Eid al-Fitr",
"Eid al-Adha": "Eid al-Adha",
"Eid al-Adha Day 2": "Eid al-Adha",
"Eid al-Adha Day 3": "Eid al-Adha",
"Eid al-Adha Day 4": "Eid al-Adha",
"Islamic New Year": "Nouvel an islamique",
"Birthday of the Prophet Muhammad": "Anniversaire du prophète Mohamed",
"Arafat Day": "Arafat Day",
"Day": "Jour"
}

147 changes: 28 additions & 119 deletions src/Countries/Tunisia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Calendars\IslamicCalendar;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Contracts\Islamic;

class Tunisia extends Country
class Tunisia extends Country implements HasTranslations, Islamic
{
private const IslamicNewYear = [
use IslamicCalendar;
use Translatable;

private const islamicNewYear = [
1970 => "03-08",
1971 => "02-26",
1973 => "02-03",
Expand Down Expand Up @@ -76,7 +82,7 @@ class Tunisia extends Country
2036 => "02-27",
2037 => "02-16"
];
public const ProphetMohammedBirthday = [
public const prophetMuhammadBirthday = [
1970 => "05-17",
1971 => "05-07",
1972 => "04-25",
Expand Down Expand Up @@ -146,7 +152,7 @@ class Tunisia extends Country
2036 => "05-07",
2037 => "04-27"
];
public const EidAlFitr = [
public const eidAlFitr = [
1970 => "12-11",
1971 => "11-30",
1972 => "11-20",
Expand Down Expand Up @@ -216,7 +222,7 @@ class Tunisia extends Country
2036 => "12-02",
2037 => "11-20",
];
public const EidAlAdha = [
public const eidAlAdha = [
1970 => "02-17",
1971 => "02-06",
1972 => "01-27",
Expand Down Expand Up @@ -295,6 +301,11 @@ public function countryCode(): string
return 'tn';
}

public function defaultLocale(): string
{
return 'en';
}

protected function allHolidays(int $year): array
{
$revolutionHoliday = [];
Expand All @@ -315,124 +326,22 @@ protected function allHolidays(int $year): array
'Republic Day' => '07-25',
'Women\'s Day' => '08-13',
'Evacuation Day' => '10-15',
], $revolutionHoliday, $this->variableHolidays($year));
], $revolutionHoliday, $this->islamicHolidays($year));
}

/**
* The following holidays are considered public holidays in Tunisia. However, their dates vary each year,
* as they are based on the Islamic Hijri (lunar) calendar. These holidays do not have a fixed date and
* occur based on the lunar calendar sequence. The order listed reflects the chronological occurrence
* of these holidays throughout the year.
* @param int $year
* @return array<string, CarbonImmutable>
*/
protected function variableHolidays(int $year): array
public function islamicHolidays(int $year): array
{
$eidAlFitr = $this->eidAlFitr($year);
$eidAlAdha = $this->eidAlAdha($year);

return array_merge(
$this->getIslamicHolidays(
year: $year,
holidays: self::IslamicNewYear,
label: 'Islamic new year'
),
$this->getIslamicHolidays(
year: $year,
holidays: self::ProphetMohammedBirthday,
label: 'Birthday of the Prophet Mohamed'
),
$this->getIslamicHolidays(
year: $year,
holidays: self::EidAlFitr,
label: 'Eid al-Fitr',
day: 2
),
$this->getIslamicHolidays(
year: $year,
holidays: self::EidAlAdha,
label: 'Eid al-Adha',
day: 3
)
[
'Islamic New Year' => $this->islamicNewYear($year),
'Birthday of the Prophet Muhammad' => $this->prophetMuhammadBirthday($year),
],
$this->convertPeriods('Eid al-Adha', $year, $eidAlAdha[0]),
$this->convertPeriods('Eid al-Fitr', $year, $eidAlFitr[0]),
);
}

/**
* @param array<int, string|array<string>> $holidays
* @return array<string, CarbonImmutable>
*/
protected function getIslamicHolidays(
int $year,
array $holidays,
string $label,
int $day = 1,
): array
{
$islamicHolidays = [];
$counter = 0;

if ($year != 1970) {
$previousHoliday = is_array($holidays[$year - 1]) ? $holidays[$year - 1][1] : $holidays[$year - 1];

$previousHoliday = CarbonImmutable::createFromFormat('Y-m-d', ($year - 1) . '-' . $previousHoliday);

if ($previousHoliday->addDays($day - 1)->year == $year) {
$islamicHolidays = $this->prepareHolidays(
holiday: $previousHoliday,
day: $day,
label: $label,
filterYear: $year
);
$counter++;
}
}

$currentYearHolidays = is_array($holidays[$year]) ? $holidays[$year] : [$holidays[$year]];

foreach ($currentYearHolidays as $currentYearHoliday) {
$currentYearHoliday = CarbonImmutable::createFromFormat('Y-m-d', "$year-$currentYearHoliday");

$islamicHolidays = array_merge($islamicHolidays, $this->prepareHolidays(
holiday: $currentYearHoliday,
day: $day,
label: $label,
filterYear: $year,
prefix: $counter ? ($counter + 1) . '. ' : ''
));
$counter++;
}

if ($year != 2037) {
$nextHoliday = is_array($holidays[$year + 1]) ? $holidays[$year + 1][1] : $holidays[$year + 1];

$nextHoliday = CarbonImmutable::createFromFormat('Y-m-d', ($year + 1) . '-' . $nextHoliday);

if ($nextHoliday->addDays(-1)->year == $year) {
$islamicHolidays = array_merge($islamicHolidays, $this->prepareHolidays(
holiday: $nextHoliday,
day: $day,
label: $label,
filterYear: $year,
prefix: $counter ? ($counter + 1) . '. ' : ''
));
}
}

return $islamicHolidays;
}

/** @return array<string, CarbonImmutable> */
protected function prepareHolidays(
CarbonImmutable $holiday,
int $day,
string $label,
int $filterYear,
string $prefix = ''
): array
{
$holidays = [];

foreach (range(1, $day) as $range) {
$holidays[$prefix . $label . ' ' . $range . '. Day'] = $holiday->addDays($range - 1);
}

return array_filter($holidays, fn($holiday) => $holiday->year == $filterYear);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,19 @@
"date": "2024-04-09"
},
{
"name": "Eid al-Fitr 1. Day",
"date": "2024-04-10"
},
{
"name": "Eid al-Fitr 2. Day",
"date": "2024-04-11"
"name": "Eid al-Fitr",
mr-wolf-gb marked this conversation as resolved.
Show resolved Hide resolved
"date": "2024-04-12"
},
{
"name": "Labour Day",
"date": "2024-05-01"
},
{
"name": "Eid al-Adha 1. Day",
"date": "2024-06-17"
},
{
"name": "Eid al-Adha 2. Day",
"date": "2024-06-18"
},
{
"name": "Eid al-Adha 3. Day",
"date": "2024-06-19"
"name": "Eid al-Adha",
"date": "2024-06-20"
},
{
"name": "Islamic new year 1. Day",
"name": "Islamic new year",
"date": "2024-07-07"
},
{
Expand All @@ -48,7 +36,7 @@
"date": "2024-08-13"
},
{
"name": "Birthday of the Prophet Mohamed 1. Day",
"name": "Birthday of the Prophet Mohamed",
"date": "2024-09-15"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
"date": "2025-03-20"
},
{
"name": "Eid al-Fitr 1. Day",
"date": "2025-03-31"
},
{
"name": "Eid al-Fitr 2. Day",
"date": "2025-04-01"
"name": "Eid al-Fitr",
"date": "2025-04-02"
},
{
"name": "Martyrs' Day",
Expand All @@ -24,19 +20,11 @@
"date": "2025-05-01"
},
{
"name": "Eid al-Adha 1. Day",
"date": "2025-06-07"
},
{
"name": "Eid al-Adha 2. Day",
"date": "2025-06-08"
},
{
"name": "Eid al-Adha 3. Day",
"date": "2025-06-09"
"name": "Eid al-Adha",
"date": "2025-06-10"
},
{
"name": "Islamic new year 1. Day",
"name": "Islamic new year",
"date": "2025-06-26"
},
{
Expand All @@ -48,7 +36,7 @@
"date": "2025-08-13"
},
{
"name": "Birthday of the Prophet Mohamed 1. Day",
"name": "Birthday of the Prophet Mohamed",
"date": "2025-09-04"
},
{
Expand Down
33 changes: 3 additions & 30 deletions tests/Countries/TunisiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Carbon\CarbonImmutable;
use Spatie\Holidays\Holidays;

it('can calculate tunisian holidays 2024', function () {
CarbonImmutable::setTestNow('2024-01-01');
it('can calculate tunisia holidays', function ($year) {
CarbonImmutable::setTestNow($year . '-01-01');

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

Expand All @@ -15,31 +15,4 @@
->not()->toBeEmpty();

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

it('can calculate tunisian holidays 2025', function () {
CarbonImmutable::setTestNow('2025-01-01');

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

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

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

//it('can calculate tunisian holidays list', function () {
//
// $result = [];
// for ($i = 1970; $i <= 2037; $i++) {
// CarbonImmutable::setTestNowAndTimezone("$i-01-01");
// $holidays = Holidays::for(country: 'tn')->get();
// //dd($holidays);
// $result[$i] = formatDates($holidays)[0]['date'];
// }
// //dd($result);
// file_put_contents('fitr.json', json_encode($result));
// //file_put_contents('hijri.bin', serialize($result));
//});

})->with([2024, 2025]);
Loading