Skip to content

Commit

Permalink
Merge pull request #423 from cakephp/gettimezone-return
Browse files Browse the repository at this point in the history
Align Chronos::getTimezone() return type with DateTimeImmutable
  • Loading branch information
othercorey authored Sep 24, 2023
2 parents e1a5d70 + 1cc8f25 commit aa49da4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use ReturnTypeWillChange;
use RuntimeException;

/**
* An Immutable extension on the native DateTime object.
Expand Down Expand Up @@ -1009,12 +1009,16 @@ public function setTimezone(DateTimeZone|string $value): static
/**
* Return time zone set for this instance.
*
* @return \DateTimeZone|null
* @return \DateTimeZone
*/
#[ReturnTypeWillChange]
public function getTimezone(): ?DateTimeZone
public function getTimezone(): DateTimeZone
{
return parent::getTimezone() ?: null;
$tz = parent::getTimezone();
if ($tz === false) {
throw new RuntimeException('Time zone could not be retrieved.');
}

return $tz;
}

/**
Expand Down Expand Up @@ -2628,13 +2632,9 @@ public function __get(string $name): string|float|int|bool|DateTimeZone
return $this->offset === 0;

case $name === 'timezone' || $name === 'tz':
assert($this->getTimezone() !== null, 'Timezone is not set');

return $this->getTimezone();

case $name === 'timezoneName' || $name === 'tzName':
assert($this->getTimezone() !== null, 'Timezone is not set');

return $this->getTimezone()->getName();

default:
Expand Down

0 comments on commit aa49da4

Please sign in to comment.