From 0972ac71d91b79aa90572bedb7fd7af24c1e8d74 Mon Sep 17 00:00:00 2001 From: Maarten Paauw Date: Sat, 27 Apr 2024 17:54:26 +0200 Subject: [PATCH] feat(column): add support for `DateTimeZone` instance --- src/Concerns/CanFormatTimezone.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Concerns/CanFormatTimezone.php b/src/Concerns/CanFormatTimezone.php index 1d4346a..3bfabe5 100644 --- a/src/Concerns/CanFormatTimezone.php +++ b/src/Concerns/CanFormatTimezone.php @@ -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(); } }