Skip to content

Commit

Permalink
Merge pull request #46 from Icinga/fix-cron
Browse files Browse the repository at this point in the history
Cron/OneOff: Always use default time zone
  • Loading branch information
yhabteab authored Aug 10, 2023
2 parents 6a7271f + c8fe107 commit 98449f1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 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
7 changes: 4 additions & 3 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 */
/** @var DateTimeInterface 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
34 changes: 34 additions & 0 deletions tests/CronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Tests\Scheduler;

use DateTime;
use DateTimeZone;
use InvalidArgumentException;
use ipl\Scheduler\Contract\Frequency;
use ipl\Scheduler\Cron;
Expand Down Expand Up @@ -132,4 +133,37 @@ public function testJsonSerializeAndDeserialize()
$this->assertEquals($start, $fromJson->getStart());
$this->assertEquals($end, $fromJson->getEnd());
}

public function testSerializeAndDeserializeHandleTimezonesCorrectly()
{
$oldTz = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');

try {
$start = new DateTime('01-06-2023T12:00:00', new DateTimeZone('America/New_York'));
$end = new DateTime('01-06-2024T07:00:00', new DateTimeZone('America/New_York'));

$cron = Cron::fromJson(
json_encode(
(new Cron('@minutely'))
->startAt($start)
->endAt($end)
)
);

$this->assertEquals(
new DateTime('2023-06-01T18:00:00'),
$cron->getStart(),
'Cron::jsonSerialize() does not restore the start date with a time zone correctly'
);

$this->assertEquals(
new DateTime('2024-06-01T13:00:00'),
$cron->getEnd(),
'Cron::jsonSerialize() does not restore the end date with a time zone correctly'
);
} finally {
date_default_timezone_set($oldTz);
}
}
}
27 changes: 26 additions & 1 deletion tests/OneOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ipl\Tests\Scheduler;

use DateTime;
use ipl\Scheduler\Contract\Frequency;
use DateTimeZone;
use ipl\Scheduler\OneOff;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -45,4 +45,29 @@ public function testJsonSerialize()
$this->assertEquals($oneOff, $fromJson);
$this->assertEquals($now, $fromJson->getStart());
}

public function testSerializeAndDeserializeHandleTimezonesCorrectly()
{
$oldTz = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');

try {
$dateTime = new DateTime('01-06-2023T12:00:00', new DateTimeZone('America/New_York'));
$oneOff = OneOff::fromJson(json_encode(new OneOff($dateTime)));

$this->assertEquals(
new DateTime('2023-06-01T18:00:00'),
$oneOff->getStart(),
'OneOff::jsonSerialize() does not restore the start date with a time zone correctly'
);

$this->assertEquals(
new DateTime('2023-06-01T18:00:00'),
$oneOff->getEnd(),
'OneOff::jsonSerialize() does not restore the start/end date with a time zone correctly'
);
} finally {
date_default_timezone_set($oldTz);
}
}
}

0 comments on commit 98449f1

Please sign in to comment.