Skip to content

Commit

Permalink
fix: update timezoneService
Browse files Browse the repository at this point in the history
  • Loading branch information
joy2362 committed Jun 28, 2024
1 parent ef9dced commit 6811be4
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/TimeZoneService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Joy2362\PhpTimezone;

use DateTime;
use Exception;
use DateTimeZone;
use Illuminate\Support\Facades\Config;

class TimeZoneService
{
Expand Down Expand Up @@ -53,11 +55,13 @@ public function getSupportedTimeZone(): array
public function list(): array
{
$list = [];
$label = config('Timezone.LABEL_FIELD_NAME') ?? 'label';
$value = config('Timezone.VALUE_FIELD_NAME') ?? 'value';

$label = Config::get('Timezone.LABEL_FIELD_NAME','label');
$value = Config::get('Timezone.VALUE_FIELD_NAME','value');

foreach ($this->regions as $region) {
$timezones = DateTimeZone::listIdentifiers($region);
$data = [];
foreach ($timezones as $timezone) {
$data[$label] = $this->getLabel($timezone);
$data[$value] = $timezone;
Expand Down Expand Up @@ -106,8 +110,8 @@ public function listByRegion($region): array
return [];
}
$list = [];
$label = config('Timezone.LABEL_FIELD_NAME') ?? 'label';
$value = config('Timezone.VALUE_FIELD_NAME') ?? 'value';
$label = Config::get('Timezone.LABEL_FIELD_NAME','label');
$value = Config::get('Timezone.VALUE_FIELD_NAME','value');

$timezones = DateTimeZone::listIdentifiers($this->regions[$region]);
foreach ($timezones as $timezone) {
Expand Down Expand Up @@ -148,10 +152,11 @@ private function getLabel($timezone): string
$time = new DateTime(null, new DateTimeZone($timezone));
$time_diff = $this->getTimeDiff($time);
$zone = $this->getZone($time);
$defaultTimeZone = config('Timezone.DEFAULT_TIME_ZONE') ?? 'GMT';

$defaultTimeZone = Config::get('Timezone.DEFAULT_TIME_ZONE' , 'GMT');
$defaultTimeZone = in_array($defaultTimeZone, $this->supportedTimeZone) ? $defaultTimeZone : $this->supportedTimeZone[0];
return "({$defaultTimeZone} {$time_diff}) {$zone}";
} catch (\Exception $ex) {
} catch (Exception $ex) {
return '';
}
}
Expand All @@ -162,7 +167,7 @@ private function getLabel($timezone): string
*/
private function getTimeDiff($time): string
{
$time_diff_symbol = config('Timezone.TIME_DIFF_SYMBOL') ?? '.';
$time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL','.');
$str_time_diff = $time->format('p');
return str_replace(':', $time_diff_symbol, $str_time_diff);
}
Expand All @@ -173,9 +178,6 @@ private function getTimeDiff($time): string
*/
private function getZone($time): string
{
$str_zone = $time->format('e');
return str_replace('_', ' ', $str_zone);
return str_replace('_', ' ', $time->format('e'));
}


}
}

0 comments on commit 6811be4

Please sign in to comment.