Skip to content

Commit

Permalink
explicitly setting time zone for expiration times in cache item (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gediminas Šedbaras authored and dwsupplee committed Oct 29, 2019
1 parent 6d5455b commit c082be7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Cache/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Item implements CacheItemInterface
private $value;

/**
* @var \DateTime
* @var \DateTime|null
*/
private $expiration;

Expand Down Expand Up @@ -81,7 +81,7 @@ public function isHit()
return true;
}

return new \DateTime() < $this->expiration;
return $this->currentTime()->getTimestamp() < $this->expiration->getTimestamp();
}

/**
Expand Down Expand Up @@ -126,9 +126,9 @@ public function expiresAt($expiration)
public function expiresAfter($time)
{
if (is_int($time)) {
$this->expiration = new \DateTime("now + $time seconds");
$this->expiration = $this->currentTime()->add(new \DateInterval("PT{$time}S"));
} elseif ($time instanceof \DateInterval) {
$this->expiration = (new \DateTime())->add($time);
$this->expiration = $this->currentTime()->add($time);
} elseif ($time === null) {
$this->expiration = $time;
} else {
Expand Down Expand Up @@ -182,4 +182,9 @@ private function isValidExpiration($expiration)

return false;
}

protected function currentTime()
{
return new \DateTime('now', new \DateTimeZone('UTC'));
}
}

0 comments on commit c082be7

Please sign in to comment.