Skip to content

Commit 3700d2b

Browse files
authored
Merge pull request #9 from DirectoryTree/codex/fix-schedule-date-mutation
Prevent schedule date mutation during timezone calculations
2 parents d6aad79 + c4478c4 commit 3700d2b

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/Drivers/Schedule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ abstract protected function resolveNextOccurrence(CarbonInterface $after): ?Carb
4949
*/
5050
public function getNextOccurrence(CarbonInterface $after): ?CarbonInterface
5151
{
52+
$after = $after->avoidMutation();
53+
5254
if ($this->timezone) {
5355
$after = $after->setTimezone($this->timezone);
5456
}

tests/Drivers/CronScheduleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
expect($next->timezone('UTC')->format('Y-m-d H:i:s'))->toBe('2026-05-02 13:00:00');
3939
});
4040

41+
it('does not mutate the given date when computing with a timezone', function () {
42+
$date = Carbon::parse('2026-05-02 12:00:00', 'UTC');
43+
44+
(new CronSchedule('0 9 * * *', 'America/New_York'))->getNextOccurrence($date);
45+
46+
expect($date->timezoneName)->toBe('UTC');
47+
expect($date->format('Y-m-d H:i:s'))->toBe('2026-05-02 12:00:00');
48+
});
49+
4150
it('computes the next occurrence with timezone set via method', function () {
4251
Carbon::setTestNow('2026-05-02 12:00:00', 'UTC');
4352

0 commit comments

Comments
 (0)