diff --git a/src/Drivers/Schedule.php b/src/Drivers/Schedule.php index 54ab2eb..2bf0937 100644 --- a/src/Drivers/Schedule.php +++ b/src/Drivers/Schedule.php @@ -49,6 +49,8 @@ abstract protected function resolveNextOccurrence(CarbonInterface $after): ?Carb */ public function getNextOccurrence(CarbonInterface $after): ?CarbonInterface { + $after = $after->avoidMutation(); + if ($this->timezone) { $after = $after->setTimezone($this->timezone); } diff --git a/tests/Drivers/CronScheduleTest.php b/tests/Drivers/CronScheduleTest.php index 1b964c7..c7e2f65 100644 --- a/tests/Drivers/CronScheduleTest.php +++ b/tests/Drivers/CronScheduleTest.php @@ -38,6 +38,15 @@ expect($next->timezone('UTC')->format('Y-m-d H:i:s'))->toBe('2026-05-02 13:00:00'); }); +it('does not mutate the given date when computing with a timezone', function () { + $date = Carbon::parse('2026-05-02 12:00:00', 'UTC'); + + (new CronSchedule('0 9 * * *', 'America/New_York'))->getNextOccurrence($date); + + expect($date->timezoneName)->toBe('UTC'); + expect($date->format('Y-m-d H:i:s'))->toBe('2026-05-02 12:00:00'); +}); + it('computes the next occurrence with timezone set via method', function () { Carbon::setTestNow('2026-05-02 12:00:00', 'UTC');