From edc8d392258ea33ef935e421214f4615db8d54df Mon Sep 17 00:00:00 2001 From: dantes4ur <114443923+dantes4ur@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:53:18 -0600 Subject: [PATCH] Adding Full Mandatory Mexican holidays (#20) * feat(Mexico.php): rebase main to put mandatory days * feat(Mexico.php): finished dates --- src/Countries/Mexico.php | 76 +++++++------------ .../it_can_calculate_mexican_holidays.snap | 34 +++++++++ .../it_can_calculate_mexico_holidays.snap | 66 ---------------- tests/Countries/MexicoTest.php | 3 +- 4 files changed, 64 insertions(+), 115 deletions(-) create mode 100644 tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap delete mode 100644 tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexico_holidays.snap diff --git a/src/Countries/Mexico.php b/src/Countries/Mexico.php index 21b25f104..9a363fd07 100644 --- a/src/Countries/Mexico.php +++ b/src/Countries/Mexico.php @@ -2,9 +2,7 @@ namespace Spatie\Holidays\Countries; -use Carbon\Carbon; use Carbon\CarbonImmutable; -use Carbon\CarbonPeriod; class Mexico extends Country { @@ -16,16 +14,10 @@ public function countryCode(): string protected function allHolidays(int $year): array { return array_merge([ - 'Año nuevo' => '01-01', - 'Día de la Candelaria' => '02-02', - 'Día de la Bandera' => '02-24', - 'Día del Niño' => '04-30', - 'Día de la Madre' => '04-30', - 'Día del Trabajo' => '05-01', - 'Día de la Independencia' => '09-15', - 'Día de la Raza' => '10-12', - 'Día de Muertos' => '11-02', - 'Virgen de Guadalupe' => '12-12', + 'Año Nuevo' => '01-01', + 'Día Internacional de los Trabajadores' => '05-01', + 'Día de Independencia' => '09-16', + 'Cambio de Gobierno' => '10-01', 'Navidad' => '12-25', ], $this->variableHolidays($year)); } @@ -33,53 +25,43 @@ protected function allHolidays(int $year): array /** @return array */ protected function variableHolidays(int $year): array { + $constitutionDay = (new CarbonImmutable("first monday of february $year")) // 5 of february + ->setTimezone('America/Mexico_City'); - $natalicioBenitoJuarez = new CarbonImmutable(sprintf('third monday of march %s', $year)); - $promulgacionConstitucion = new CarbonImmutable(sprintf('first monday of february %s', $year)); - $revolucionMexicana = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-11-20")->setTimezone('America/Mexico_City'); + $benitoJuarezBirth = (new CarbonImmutable("third monday of March $year")) // 21 of march + ->setTimezone('America/Mexico_City'); - if ($revolucionMexicana->isSunday()) { - $revolucionMexicana = $revolucionMexicana->next('monday'); - } - - $fathersDay = new CarbonImmutable(sprintf('third sunday of june %s', $year)); + $revolutionDay = (new CarbonImmutable("third monday of november $year")) // 20 of november + ->setTimezone('America/Mexico_City'); - $days = [ - 'Aniversario de la promulgación de la Constitución de 1917' => $promulgacionConstitucion->format('m-d'), - 'Natalicio de Benito Juárez' => $natalicioBenitoJuarez->format('m-d'), - 'Revolución Mexicana' => $revolucionMexicana->format('m-d'), - 'Día del Padre' => $fathersDay->format('m-d'), + /** @var CarbonImmutable|false $executiveChange */ + $executiveChange = $this->governmentChangeDate(); + $known_days = [ + 'Día de la Constitución' => $constitutionDay, // It's the first monday of february + 'Natalicio de Benito Juárez' => $benitoJuarezBirth, + 'Día de la Revolución' => $revolutionDay, ]; - if ($this->transmisionPoderEjecutivoFederal($year)) { - $days[ - 'Transmisión del Poder Ejecutivo Federal' - ] = $this->transmisionPoderEjecutivoFederal($year); - } - - return $days; + return array_merge( + $known_days, + $executiveChange ? ['Cambio de Gobierno' => $executiveChange] : [] + ); } - protected function transmisionPoderEjecutivoFederal($year): bool|string + protected function governmentChangeDate(): CarbonImmutable|false { - $period = new CarbonPeriod(); - $period->setDateClass(CarbonImmutable::class); - $period - ->every('6 years') - ->since(sprintf('%s-10-01', 2024)) - ->until(sprintf('%s-10-01 00:00:00', Carbon::now()->addYears(6)->year)); + $baseYear = 1946; // The first occurrence with president Miguel Aleman Valdes + $currentYear = CarbonImmutable::now()->year; // Get the current year - $period->addFilter(function ($date) use ($year) { - return $date->year === $year; - }); + // Check if the current year is a transmission year + if (($currentYear - $baseYear) % 6 == 0) { + /** @phpstan-ignore-next-line */ + return CarbonImmutable::create($currentYear, 10, 1) // October 1st of the transmission year + ->setTimezone('America/Mexico_City'); - $availableDates = $period->toArray(); - - if (count($availableDates)) { - return $availableDates[0]->format('m-d'); } - return false; } + } diff --git a/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap b/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap new file mode 100644 index 000000000..7f15803a3 --- /dev/null +++ b/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap @@ -0,0 +1,34 @@ +[ + { + "name": "A\u00f1o Nuevo", + "date": "2024-01-01" + }, + { + "name": "D\u00eda de la Constituci\u00f3n", + "date": "2024-02-04" + }, + { + "name": "Natalicio de Benito Ju\u00e1rez", + "date": "2024-03-17" + }, + { + "name": "D\u00eda Internacional de los Trabajadores", + "date": "2024-05-01" + }, + { + "name": "D\u00eda de Independencia", + "date": "2024-09-16" + }, + { + "name": "Cambio de Gobierno", + "date": "2024-09-30" + }, + { + "name": "D\u00eda de la Revoluci\u00f3n", + "date": "2024-11-17" + }, + { + "name": "Navidad", + "date": "2024-12-25" + } +] \ No newline at end of file diff --git a/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexico_holidays.snap b/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexico_holidays.snap deleted file mode 100644 index ecafb2e00..000000000 --- a/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexico_holidays.snap +++ /dev/null @@ -1,66 +0,0 @@ -[ - { - "name": "A\u00f1o nuevo", - "date": "2024-01-01" - }, - { - "name": "D\u00eda de la Candelaria", - "date": "2024-02-02" - }, - { - "name": "Aniversario de la promulgaci\u00f3n de la Constituci\u00f3n de 1917", - "date": "2024-02-05" - }, - { - "name": "D\u00eda de la Bandera", - "date": "2024-02-24" - }, - { - "name": "Natalicio de Benito Ju\u00e1rez", - "date": "2024-03-18" - }, - { - "name": "D\u00eda del Ni\u00f1o", - "date": "2024-04-30" - }, - { - "name": "D\u00eda de la Madre", - "date": "2024-04-30" - }, - { - "name": "D\u00eda del Trabajo", - "date": "2024-05-01" - }, - { - "name": "D\u00eda del Padre", - "date": "2024-06-16" - }, - { - "name": "D\u00eda de la Independencia", - "date": "2024-09-15" - }, - { - "name": "Transmisi\u00f3n del Poder Ejecutivo Federal", - "date": "2024-10-01" - }, - { - "name": "D\u00eda de la Raza", - "date": "2024-10-12" - }, - { - "name": "D\u00eda de Muertos", - "date": "2024-11-02" - }, - { - "name": "Revoluci\u00f3n Mexicana", - "date": "2024-11-19" - }, - { - "name": "Virgen de Guadalupe", - "date": "2024-12-12" - }, - { - "name": "Navidad", - "date": "2024-12-25" - } -] \ No newline at end of file diff --git a/tests/Countries/MexicoTest.php b/tests/Countries/MexicoTest.php index cb2876aeb..dfd62a71e 100644 --- a/tests/Countries/MexicoTest.php +++ b/tests/Countries/MexicoTest.php @@ -5,7 +5,7 @@ use Carbon\CarbonImmutable; use Spatie\Holidays\Holidays; -it('can calculate mexico holidays', function () { +it('can calculate mexican holidays', function () { CarbonImmutable::setTestNowAndTimezone('2024-01-01'); $holidays = Holidays::for(country: 'mx')->get(); @@ -15,5 +15,4 @@ ->not()->toBeEmpty(); expect(formatDates($holidays))->toMatchSnapshot(); - });