From fbbc24e4b4bca988003e593b689bbde52c22b2a8 Mon Sep 17 00:00:00 2001 From: Michael Nabil <46572405+michaelnabil230@users.noreply.github.com> Date: Sat, 25 Jun 2022 19:32:46 +0200 Subject: [PATCH 1/3] Replace function `getTimezones` in to collect --- app/Traits/DateTime.php | 49 +++++++++-------------------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index 2929413c4..f77af2fc0 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -2,9 +2,11 @@ namespace App\Traits; -use App\Traits\SearchString; -use Carbon\CarbonPeriod; use Date; +use DateTimeZone; +use Illuminate\Support\Str; +use Carbon\CarbonPeriod; +use App\Traits\SearchString; trait DateTime { @@ -59,42 +61,13 @@ trait DateTime public function getTimezones() { - $groups = []; - - // The list of available timezone groups to use. - $use_zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); - - // Get the list of time zones from the server. - $zones = \DateTimeZone::listIdentifiers(); - - // Build the group lists. - foreach ($zones as $zone) { - // Time zones not in a group we will ignore. - if (strpos($zone, '/') === false) { - continue; - } - - // Get the group/locale from the timezone. - list ($group, $locale) = explode('/', $zone, 2); - - // Only use known groups. - if (in_array($group, $use_zones)) { - // Initialize the group if necessary. - if (!isset($groups[$group])) { - $groups[$group] = []; - } - - // Only add options where a locale exists. - if (!empty($locale)) { - $groups[$group][$zone] = str_replace('_', ' ', $locale); - } - } - } - - // Sort the group lists. - ksort($groups); - - return $groups; + return collect(DateTimeZone::listIdentifiers()) + ->mapWithKeys(function ($timezone) { + return [$timezone => Str::after($timezone, '/')]; + }) + ->groupBy(function ($item, $key) { + return Str::before($key, '/'); + }, preserveKeys: true);; } public function getFinancialStart($year = null) From 25a4aaf837761c2b56f303f47712372465f29b44 Mon Sep 17 00:00:00 2001 From: Michael Nabil <46572405+michaelnabil230@users.noreply.github.com> Date: Wed, 6 Jul 2022 00:22:35 +0200 Subject: [PATCH 2/3] Update DateTime.php --- app/Traits/DateTime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index f77af2fc0..f0e359a30 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -67,7 +67,7 @@ trait DateTime }) ->groupBy(function ($item, $key) { return Str::before($key, '/'); - }, preserveKeys: true);; + }, preserveKeys: true); } public function getFinancialStart($year = null) From 7093afeb6ff9b460aaabfe1f22ff8f8c8a7e1417 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker <73346401+EnesSacid-Buker@users.noreply.github.com> Date: Thu, 16 Feb 2023 13:08:02 +0300 Subject: [PATCH 3/3] Code refactoring --- app/Traits/DateTime.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index f0e359a30..cf653e26a 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -2,11 +2,9 @@ namespace App\Traits; -use Date; -use DateTimeZone; -use Illuminate\Support\Str; -use Carbon\CarbonPeriod; use App\Traits\SearchString; +use App\Utilities\Date; +use Carbon\CarbonPeriod; trait DateTime { @@ -61,13 +59,24 @@ trait DateTime public function getTimezones() { - return collect(DateTimeZone::listIdentifiers()) - ->mapWithKeys(function ($timezone) { - return [$timezone => Str::after($timezone, '/')]; - }) - ->groupBy(function ($item, $key) { - return Str::before($key, '/'); - }, preserveKeys: true); + // The list of available timezone groups to use. + $use_zones = ['Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific']; + + // Get the list of time zones from the server. + $zones = \DateTimeZone::listIdentifiers(); + + return collect($zones) + ->mapWithKeys(function ($timezone) use ($use_zones) { + if (! in_array(str($timezone)->before('/'), $use_zones)) { + return []; + } + + return [$timezone => str($timezone)->after('/')->value()]; + }) + ->groupBy(function ($item, $key) { + return str($key)->before('/')->value(); + }, preserveKeys: true) + ->toArray(); } public function getFinancialStart($year = null)