Skip to content

Commit

Permalink
Driver: add timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jan 19, 2023
1 parent ee4b2c9 commit 946909e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Driver/Cnb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ class Day extends Exchange\Driver\Driver

protected string $refresh = 'today 15:30';

protected string $timeZone = 'Europe/Prague';


protected function createList(ResponseInterface $response): iterable
{
$data = $response->getBody()->getContents();
$list = explode("\n", Exchange\Utils::stroke2point($data));
$list[1] = 'Česká Republika|koruna|1|CZK|1';

$this->setDate('d.m.Y', explode(' ', $list[0])[0]);
$this->setDate('!d.m.Y', explode(' ', $list[0])[0]);
unset($list[0]);

return $list;
Expand Down
15 changes: 11 additions & 4 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class Driver

private \DateTimeImmutable $date;

protected string $timeZone = 'UTC';

protected string $refresh = 'tomorrow midnight';

/**
Expand Down Expand Up @@ -72,19 +74,19 @@ public function properties(array $allowedCurrencies): \Generator
}


public function getRefresh(): string
public function getRefresh(): \DateTime
{
return $this->refresh;
return new \DateTime($this->refresh, new \DateTimeZone($this->timeZone));
}


protected function setDate(string $format, string $value): void
{
$date = \DateTime::createFromFormat($format, $value);
$date = \DateTime::createFromFormat($format, $value, new \DateTimeZone($this->timeZone));
if ($date === false) {
throw new Exchange\Exceptions\InvalidStateException(sprintf('Can not create DateTime object from source "%s" with format "%s".', $value, $format));
}
$this->date = \DateTimeImmutable::createFromMutable($date->setTime(0, 0));
$this->date = \DateTimeImmutable::createFromMutable($date);
}


Expand All @@ -106,6 +108,11 @@ abstract protected function prepareUrl(?\DateTimeInterface $date): string;

protected function createRequest(?\DateTimeInterface $date): RequestInterface
{
if ($date !== null && $date->getTimezone()->getName() !== $this->timeZone) {
$date = new \DateTime('@' . $date->getTimestamp(), new \DateTimeZone('UTC'));
$date->setTimezone(new \DateTimeZone($this->timeZone));
}

return $this->requestFactory->createRequest('GET', $this->prepareUrl($date));
}

Expand Down
4 changes: 3 additions & 1 deletion src/Driver/Ecb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class Day extends Exchange\Driver\Driver
{
protected string $timeZone = 'Europe/Berlin';


/**
* @return iterable<\SimpleXMLElement>
Expand All @@ -29,7 +31,7 @@ protected function createList(ResponseInterface $response): iterable
$eur->addAttribute('currency', 'EUR');
$eur->addAttribute('rate', '1');
assert(isset($xml->Cube->Cube) && $xml->Cube->Cube->attributes() !== null);
$this->setDate('Y-m-d', (string) $xml->Cube->Cube->attributes()['time']);
$this->setDate('!Y-m-d', (string) $xml->Cube->Cube->attributes()['time']);

return $xml->Cube->Cube->Cube;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RatingList/RatingListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function create(Driver $driver, ?\DateTimeInterface $date = null): Rating

private function countTTL(Driver $driver): int
{
$refresh = (int) (new \DateTime($driver->getRefresh()))->format('U');
$refresh = (int) $driver->getRefresh()->format('U');
if (time() >= $refresh) {
$refresh += self::DAY;
}
Expand Down

0 comments on commit 946909e

Please sign in to comment.