Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron/OneOff: Always use default time zone #46

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}