From 03fbc8f18d5fa186353466362000b9ed191bcc1d Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 8 Aug 2023 18:32:07 +0200 Subject: [PATCH] Cron/OneOff: Always use the application's time zone Since `Cron` & `OneOff` frequencies work with `DatemTime` by either calling the methods of the class manually or constructing from a serialized representation, time zones can differ from the application's time zone. However, in the frontend we don't display time zone information, so the time may appear incorrect. This change ensures that both frequencies always work with the application's time zone as well. --- src/Cron.php | 3 +++ src/OneOff.php | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Cron.php b/src/Cron.php index 56de840..3f391a7 100644 --- a/src/Cron.php +++ b/src/Cron.php @@ -5,6 +5,7 @@ use Cron\CronExpression; use DateTime; use DateTimeInterface; +use DateTimeZone; use InvalidArgumentException; use ipl\Scheduler\Contract\Frequency; @@ -98,6 +99,7 @@ public function getExpression(): string public function startAt(DateTimeInterface $start): self { $this->start = clone $start; + $this->start->setTimezone(new DateTimeZone(date_default_timezone_get())); return $this; } @@ -112,6 +114,7 @@ public function startAt(DateTimeInterface $start): self public function endAt(DateTimeInterface $end): Frequency { $this->end = clone $end; + $this->end->setTimezone(new DateTimeZone(date_default_timezone_get())); return $this; } diff --git a/src/OneOff.php b/src/OneOff.php index 1c99e40..d5624ab 100644 --- a/src/OneOff.php +++ b/src/OneOff.php @@ -4,7 +4,7 @@ use DateTime; use DateTimeInterface; -use InvalidArgumentException; +use DateTimeZone; use ipl\Scheduler\Contract\Frequency; class OneOff implements Frequency @@ -12,9 +12,10 @@ class OneOff implements Frequency /** @var DateTime Start time of this frequency */ protected $dateTime; - public function __construct(DateTime $dateTime) + public function __construct(DateTimeInterface $dateTime) { $this->dateTime = clone $dateTime; + $this->dateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); } public function isDue(DateTimeInterface $dateTime): bool