8000 [Scheduler] Allow setting cron expression next run date timezone by danielburger1337 · Pull Request #50572 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Scheduler] Allow setting cron expression next run date timezone #50572

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

Merged
merged 1 commit into from
Jun 8, 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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Allow setting timezone of next run date in CronExpressionTrigger

6.3
---

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Scheduler/RecurringMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ public static function every(string|int|\DateInterval $frequency, object $messag
return new self(new PeriodicalTrigger($frequency, $from, $until), $message);
}

public static function cron(string $expression, object $message): self
public static function cron(string $expression, object $message, \DateTimeZone|string $timezone = null): self
{
if (!str_contains($expression, '#')) {
return new self(CronExpressionTrigger::fromSpec($expression), $message);
return new self(CronExpressionTrigger::fromSpec($expression, null, $timezone), $message);
}

if (!$message instanceof \Stringable) {
throw new InvalidArgumentException('A message must be stringable to use "hashed" cron expressions.');
}

return new self(CronExpressionTrigger::fromSpec($expression, (string) $message), $message);
return new self(CronExpressionTrigger::fromSpec($expression, (string) $message, $timezone), $message);
}

public static function trigger(TriggerInterface $trigger, object $message): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,19 @@ public function testFromHashWithStandardExpression()
$this->assertSame('56 20 1 9 0', (string) CronExpressionTrigger::fromSpec('56 20 1 9 0', 'some context'));
$this->assertSame('0 0 * * *', (string) CronExpressionTrigger::fromSpec('@daily'));
}

public function testDefaultTimezone()
{
$now = new \DateTimeImmutable('Europe/Paris');
$trigger = CronExpressionTrigger::fromSpec('0 12 * * *');
$this->assertSame('Europe/Paris', $trigger->getNextRunDate($now)->getTimezone()->getName());
}

public function testTimezoneIsUsed()
{
$now = new \DateTimeImmutable('Europe/Paris');
$timezone = new \DateTimeZone('UTC');
$trigger = CronExpressionTrigger::fromSpec('0 12 * * *', null, $timezone);
$this->assertSame('UTC', $trigger->getNextRunDate($now)->getTimezone()->getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,40 @@ final class CronExpressionTrigger implements TriggerInterface
[0, 6],
];

private readonly ?string $timezone;

public function __construct(
private readonly CronExpression $expression = new CronExpression('* * * * *'),
\DateTimeZone|string $timezone = null,
) {
$this->timezone = $timezone instanceof \DateTimeZone ? $timezone->getName() : $timezone;
}

public function __toString(): string
{
return $this->expression->getExpression();
}

public static function fromSpec(string $expression = '* * * * *', string $context = null): self
public static function fromSpec(string $expression = '* * * * *', string $context = null, \DateTimeZone|string $timezone = null): self
{
if (!class_exists(CronExpression::class)) {
throw new LogicException(sprintf('You cannot use "%s" as the "cron expression" package is not installed. Try running "composer require dragonmantank/cron-expression".', __CLASS__));
}

if (!str_contains($expression, '#')) {
return new self(new CronExpression($expression));
return new self(new CronExpression($expression), $timezone);
}

if (null === $context) {
throw new LogicException('A context must be provided to use "hashed" cron expressions.');
}

return new self(new CronExpression(self::parseHashed($expression, $context)));
return new self(new CronExpression(self::parseHashed($expression, $context)), $timezone);
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
return \DateTimeImmutable::createFromInterface($this->expression->getNextRunDate($run));
return \DateTimeImmutable::createFromInterface($this->expression->getNextRunDate($run, timeZone: $this->timezone));
}

private static function parseHashed(string $expression, string $context): string
Expand Down
0