Skip to content

Commit

Permalink
Merge pull request #10 from maartenpaauw/feature/date-time-zone-insta…
Browse files Browse the repository at this point in the history
…nce-support

feat(column): add support for `DateTimeZone` instance
  • Loading branch information
andreia authored Apr 30, 2024
2 parents cbe624b + 0972ac7 commit ec1f9a7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Concerns/CanFormatTimezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@ protected function getFormattedOffset(string $offset): string
return $this->getTimezoneType().($offset ? sprintf('%+03d:%02d', $hours, $minutes) : '');
}

protected function getFormattedTimezoneName(string $name): string
protected function getFormattedTimezoneName(string | DateTimeZone $name): string
{
$name = is_string($name) ? $name : $name->getName();

return str_replace(
['/', '_', 'St '],
[', ', ' ', 'St. '],
$name
$name,
);
}

protected function getFormattedOffsetAndTimezone(string $offset, string $timezone): string
protected function getFormattedOffsetAndTimezone(string $offset, string | DateTimeZone $timezone): string
{
return sprintf('(%s) %s', $this->getFormattedOffset($offset), $this->getFormattedTimezoneName($timezone));
}

public function getOffset(string $timezone): string
public function getOffset(string | DateTimeZone $timezone): string
{
$now = new DateTime('now', new DateTimeZone($this->getTimezoneType()));
$timezone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone;

return $now->setTimezone(new DateTimeZone($timezone))->getOffset();
return $now->setTimezone($timezone)->getOffset();
}
}

0 comments on commit ec1f9a7

Please sign in to comment.