Skip to content

Commit

Permalink
Merge pull request #76 from kbond/timezone
Browse files Browse the repository at this point in the history
Timezone fix
  • Loading branch information
kbond authored Mar 4, 2023
2 parents de61ddb + 8565e22 commit 1163f22
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
17 changes: 6 additions & 11 deletions src/Schedule/RunContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
*/
abstract class RunContext
{
/** @var int */
private $startTime;

/** @var int|null */
private $duration;

/** @var int|null */
private $memory;
private \DateTimeImmutable $startTime;
private ?int $duration = null;
private ?int $memory = null;

public function __construct()
{
$this->startTime = \time();
$this->startTime = new \DateTimeImmutable('now');
}

abstract public function __toString(): string;

final public function getStartTime(): \DateTimeImmutable
{
return new \DateTimeImmutable('@'.$this->startTime);
return $this->startTime;
}

final public function hasRun(): bool
Expand Down Expand Up @@ -70,7 +65,7 @@ final public function getFormattedMemory(): string

final protected function markAsRun(int $memory): void
{
$this->duration = \time() - $this->startTime;
$this->duration = \time() - $this->startTime->getTimestamp();
$this->memory = $memory;
}

Expand Down
17 changes: 5 additions & 12 deletions src/Schedule/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ abstract class Task

private const DEFAULT_EXPRESSION = '* * * * *';

/** @var string */
private $description;
private string $expression = self::DEFAULT_EXPRESSION;
private ?\DateTimeZone $timezone = null;

/** @var string */
private $expression = self::DEFAULT_EXPRESSION;

/** @var \DateTimeZone|null */
private $timezone;

public function __construct(string $description)
public function __construct(private string $description)
{
$this->description = $description;
}

final public function __toString(): string
Expand Down Expand Up @@ -749,8 +742,8 @@ private function spliceIntoPosition(int $position, $value, ...$values): self
return $this->cron(\implode(' ', $segments));
}

private function getTimezoneValue(): ?string
private function getTimezoneValue(): string
{
return $this->getTimezone() ? $this->getTimezone()->getName() : null;
return ($this->getTimezone() ?? new \DateTimeZone(\date_default_timezone_get()))->getName();
}
}
24 changes: 24 additions & 0 deletions tests/Schedule/ScheduleRunContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@
*/
final class ScheduleRunContextTest extends TestCase
{
private string $timezone;

protected function setUp(): void
{
$this->timezone = \date_default_timezone_get();
}

protected function tearDown(): void
{
\date_default_timezone_set($this->timezone);
}

/**
* @test
*/
public function start_time_timezone(): void
{
\date_default_timezone_set('America/New_York');

$context = new ScheduleRunContext(new Schedule());

$this->assertSame('America/New_York', $context->getStartTime()->getTimezone()->getName());
}

/**
* @test
*/
Expand Down

0 comments on commit 1163f22

Please sign in to comment.