Skip to content

Commit

Permalink
Fix code to use IslamicCalendar
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-wolf-gb committed Apr 2, 2024
1 parent f31ab11 commit 548c21a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 193 deletions.
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",
"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]);

0 comments on commit 548c21a

Please sign in to comment.