From ae3a6218860833a8cd1ff0253f773a982ea50073 Mon Sep 17 00:00:00 2001 From: Dante Bazaldua Date: Wed, 17 Jan 2024 12:02:13 -0600 Subject: [PATCH] feat(Mexico.php): considering transition of executive in Mexico. Using only CarbonInmutable --- src/Countries/Mexico.php | 44 +++++++++++++++---- .../it_can_calculate_mexican_holidays.snap | 10 ++--- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Countries/Mexico.php b/src/Countries/Mexico.php index 0cf2027b2..ebc626834 100644 --- a/src/Countries/Mexico.php +++ b/src/Countries/Mexico.php @@ -2,7 +2,6 @@ namespace Spatie\Holidays\Countries; -use Carbon\Carbon; use Carbon\CarbonImmutable; class Mexico extends Country @@ -17,12 +16,9 @@ protected function allHolidays(int $year): array { return array_merge([ 'Año Nuevo' => '01-01', - 'Natalicio de Benito Juárez' => '04-18', 'Día Internacional de los Trabajadores' => '05-01', - 'Jornada Electoral General' => '06-02', 'Día de Independencia' => '09-16', 'Cambio de Gobierno' => '10-01', - 'Día de la Revolución' => '11-18', 'Navidad' => '12-25', ], $this->variableHolidays($year)); } @@ -30,13 +26,43 @@ protected function allHolidays(int $year): array /** @return array */ protected function variableHolidays(int $year): array { - /** @phpstan-ignore-next-line */ - $constitutionDay = Carbon::createFromFormat("d/m/Y", "01/02/" . $year) - ->firstOfMonth(1) + $constitutionDay = (new CarbonImmutable("first monday of february $year")) // 5 of february ->setTimezone('America/Mexico_City'); - return [ - 'Día de la Constitución' => CarbonImmutable::createFromMutable($constitutionDay) // It's the first monday of february + $benitoJuarezBirth = (new CarbonImmutable("third monday of March $year")) // 21 of march + ->setTimezone('America/Mexico_City'); + + $revolutionDay = (new CarbonImmutable("third monday of november $year")) // 20 of november + ->setTimezone('America/Mexico_City'); + + /** @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, ]; + + return array_merge( + $known_days, + $executiveChange ? ['Cambio de Gobierno' => $executiveChange] : [] + ); } + + protected function governmentChangeDate(): CarbonImmutable|false + { + $baseYear = 1946; // The first occurrence with president Miguel Aleman Valdes + $currentYear = CarbonImmutable::now()->year; // Get the current 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'); + + } + 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 index 8004d2bc0..7f15803a3 100644 --- a/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap +++ b/tests/.pest/snapshots/Countries/MexicoTest/it_can_calculate_mexican_holidays.snap @@ -9,27 +9,23 @@ }, { "name": "Natalicio de Benito Ju\u00e1rez", - "date": "2024-04-18" + "date": "2024-03-17" }, { "name": "D\u00eda Internacional de los Trabajadores", "date": "2024-05-01" }, - { - "name": "Jornada Electoral General", - "date": "2024-06-02" - }, { "name": "D\u00eda de Independencia", "date": "2024-09-16" }, { "name": "Cambio de Gobierno", - "date": "2024-10-01" + "date": "2024-09-30" }, { "name": "D\u00eda de la Revoluci\u00f3n", - "date": "2024-11-18" + "date": "2024-11-17" }, { "name": "Navidad",