Skip to content

Commit

Permalink
Cron/OneOff: Always use the application's time zone
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yhabteab committed Aug 9, 2023
1 parent 4afe82c commit 03fbc8f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cron\CronExpression;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use ipl\Scheduler\Contract\Frequency;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/OneOff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use DateTime;
use DateTimeInterface;
use InvalidArgumentException;
use DateTimeZone;
use ipl\Scheduler\Contract\Frequency;

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
Expand Down

0 comments on commit 03fbc8f

Please sign in to comment.