Skip to content

Commit

Permalink
feature #50572 [Scheduler] Allow setting cron expression next run dat…
Browse files Browse the repository at this point in the history
…e timezone (danielburger1337)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Scheduler] Allow setting cron expression next run date timezone

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Allow setting the timezone for the next run date derived from a cron expression.

This is my first contribution to an experimental component and therefor I am not familiar with the exact contribution guidelines.
As this component is experimental, I think it is ok to submit this PR against branch 6.3, right?

Im also not sure about the CHANGELOG.md entry I should make. Would I add this under the 6.3.1 section or where?

Commits
-------

35756c1 [Scheduler] Allow setting cron expression next run date timezone
  • Loading branch information
nicolas-grekas committed Jun 8, 2023
2 parents 30b2b73 + 35756c1 commit be9ea53
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
@@ -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
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
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());
}
}
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 comments on commit be9ea53

Please sign in to comment.