Skip to content

Commit

Permalink
Adding holidays for Bahrain (#206)
Browse files Browse the repository at this point in the history
* Adding holidays for Bahrain

* Update Bahrain.php

* Update tests/Countries/BahrainTest.php

Co-authored-by: Niels Vanpachtenbeke <[email protected]>

---------

Co-authored-by: Niels Vanpachtenbeke <[email protected]>
  • Loading branch information
bo3bdo and Nielsvanpach authored Feb 21, 2024
1 parent 8bea6ac commit 9106de5
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lang/bahrain/ar/holidays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"New Year\\'s Day": "رأس السنة الميلادية",
"Labour Day": "عيد العمال",
"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": "عيد الأضحى المبارك",
"Islamic New Year": "رأس السنة الهجرية",
"Birthday of the Prophet Muhammad": "المولد النبوي الشريف",
"Arafat Day": "يوم عرفة",
"National Day": "اليوم الوطني",
"National Day 2": "اليوم الوطني",
"Arafat Day 2": "يوم عرفة",
}
16 changes: 16 additions & 0 deletions lang/bahrain/en/holidays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"New Year\\'s Day": "New Year\\'s Day",
"Labour Day": "Labour Day",
"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",
"Islamic New Year": "Islamic New Year",
"Birthday of the Prophet Muhammad": "Birthday of the Prophet Muhammad",
"Arafat Day": "Arafat Day",
"National Day": "National Day",
"National Day 2": "National Day",
"Arafat Day 2": "Arafat Day",
}
212 changes: 212 additions & 0 deletions src/Countries/Bahrain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Spatie\Holidays\Exceptions\InvalidYear;

class Bahrain extends Country
{
protected const EID_AL_FITR_HOLIDAYS = [
2020 => '05-24',
2021 => '05-13',
2022 => '05-02',
2023 => '04-20',
2024 => '04-10',
2025 => '03-31',
2026 => '03-21',
2027 => '03-10',
2028 => '02-27',
2029 => '02-15',
2030 => '02-04',
2031 => '01-25',
2032 => '01-15',
2033 => '01-03',
2034 => '12-13',
2035 => '12-02',
2036 => '11-20',
2037 => '11-09',
];

protected const ARAFAT_DAY_HOLIDAYS = [
2020 => '07-30',
2021 => '07-19',
2022 => '07-09',
2023 => '06-27',
2024 => '06-16',
2025 => '06-06',
2026 => '05-26',
2027 => '05-16',
2028 => '05-05',
2029 => '04-24',
2030 => '04-13',
2031 => '04-02',
2032 => '03-21',
2033 => '03-11',
2034 => '03-01',
2035 => '02-18',
2036 => '02-07',
2037 => '01-26',
];

protected const EID_AL_ADHA_HOLIDAYS = [
2020 => '07-31',
2021 => '07-20',
2022 => '07-09',
2023 => '06-28',
2024 => '06-17',
2025 => '06-07',
2026 => '05-27',
2027 => '05-17',
2028 => '05-06',
2029 => '04-25',
2030 => '04-14',
2031 => '04-03',
2032 => '03-22',
2033 => '03-12',
2034 => '03-02',
2035 => '02-19',
2036 => '02-08',
2037 => '01-27',
];

protected const ISLAMIC_NEW_YEAR_HOLIDAYS = [
2020 => '08-20',
2021 => '08-09',
2022 => '07-30',
2023 => '07-19',
2024 => '07-08',
2025 => '06-06',
2026 => '06-17',
2027 => '06-07',
2028 => '05-26',
2029 => '05-15',
2030 => '05-05',
2031 => '04-24',
2032 => '04-12',
2033 => '04-01',
2034 => '03-22',
2035 => '03-12',
2036 => '02-29',
2037 => '02-17',
];

protected const ASHURA_HOLIDAYS = [
2020 => '08-30',
2021 => '08-19',
2022 => '08-08',
2023 => '07-28',
2024 => '07-17',
2025 => '07-07',
2026 => '06-26',
2027 => '06-15',
2028 => '06-04',
2029 => '05-24',
2030 => '05-13',
2031 => '05-02',
2032 => '04-20',
2033 => '04-10',
2034 => '03-30',
2035 => '03-19',
2036 => '03-08',
2037 => '02-25',
];

protected const PROPHET_MUHAMMAD_BIRTHDAY_HOLIDAYS = [
2020 => '10-29',
2021 => '10-21',
2022 => '10-08',
2023 => '09-28',
2024 => '09-16',
2025 => '09-06',
2026 => '08-26',
2027 => '08-15',
2028 => '08-04',
2029 => '07-25',
2030 => '07-14',
2031 => '07-03',
2032 => '06-21',
2033 => '06-10',
2034 => '05-31',
2035 => '05-21',
2036 => '05-09',
2037 => '04-29',
];

public function countryCode(): string
{
return 'bh';
}

protected function allHolidays(int $year): array
{
$variableHolidays = $this->variableHolidays($year);

return array_merge([
'New Year\'s Day' => '1-1',
'Labour Day' => '5-1',
'National Day' => '12-16',
'National Day 2' => '12-17',
], $variableHolidays);
}

/**
* @return array<string, CarbonInterface>
*/
protected function variableHolidays(int $year): array
{
$holidays = [
['EID_AL_FITR_HOLIDAYS', 'Eid al-Fitr', 3],
['EID_AL_ADHA_HOLIDAYS', 'Eid al-Adha', 3],
['ARAFAT_DAY_HOLIDAYS', 'Arafat Day'],
['ISLAMIC_NEW_YEAR_HOLIDAYS', 'Islamic New Year'],
['ASHURA_HOLIDAYS', 'Ashura', 2],
['PROPHET_MUHAMMAD_BIRTHDAY_HOLIDAYS', 'Birthday of the Prophet Muhammad']
];

$dates = [];
foreach ($holidays as $holiday) {
$dates = array_merge($dates, $this->getIslamicHolidayDatesForYear(constant('self::' . $holiday[0]), $year, $holiday[1], $holiday[2] ?? 1));
}

return $dates;
}

/**
* Prepare holiday dates for the given year.
*
* @param array<int, string> $holidayDates Array mapping years to dates.
* @param int $year The year for which to prepare holiday dates.
* @param string $holidayName The name of the holiday.
* @param int $duration The duration of the holiday in days.
* @return array<string, CarbonImmutable> An array of holiday dates.
*/
private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, string $holidayName, int $duration = 1): array
{
$dates = [];

if ($year < 2020) {
throw InvalidYear::yearTooLow(2020);
}

if (! isset($holidayDates[$year])) {
return $dates;
}

$startDay = CarbonImmutable::createFromFormat('Y-m-d', sprintf('%s-%s', $year, $holidayDates[$year]));

if ($duration === 1) {
// For single-day holidays, use the holiday name without "Day"
$dates[$holidayName] = $startDay;
} else {
// For multi-day holidays, append "Day N" for the second day onwards
for ($i = 0; $i < $duration; $i++) {
$dayLabel = $i === 0 ? $holidayName : sprintf('%s Day %d', $holidayName, $i + 1);
$dates[$dayLabel] = $startDay->addDays($i);
}
}

return $dates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Eid al-Fitr",
"date": "2024-04-10"
},
{
"name": "Eid al-Fitr Day 2",
"date": "2024-04-11"
},
{
"name": "Eid al-Fitr Day 3",
"date": "2024-04-12"
},
{
"name": "Labour Day",
"date": "2024-05-01"
},
{
"name": "Arafat Day",
"date": "2024-06-16"
},
{
"name": "Eid al-Adha",
"date": "2024-06-17"
},
{
"name": "Eid al-Adha Day 2",
"date": "2024-06-18"
},
{
"name": "Eid al-Adha Day 3",
"date": "2024-06-19"
},
{
"name": "Islamic New Year",
"date": "2024-07-08"
},
{
"name": "Ashura",
"date": "2024-07-17"
},
{
"name": "Ashura Day 2",
"date": "2024-07-18"
},
{
"name": "Birthday of the Prophet Muhammad",
"date": "2024-09-16"
},
{
"name": "National Day",
"date": "2024-12-16"
},
{
"name": "National Day 2",
"date": "2024-12-17"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/BahrainTest.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 bahrain holidays', function () {
CarbonImmutable::setTestNow('2024-01-01');

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

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

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

0 comments on commit 9106de5

Please sign in to comment.