Merge pull request #2482 from michaelnabil230/master

Replace function `getTimezones` in to collect
This commit is contained in:
EnesSacid-Buker
2023-02-16 14:23:22 +03:00
committed by GitHub

View File

@ -3,8 +3,8 @@
namespace App\Traits; namespace App\Traits;
use App\Traits\SearchString; use App\Traits\SearchString;
use App\Utilities\Date;
use Carbon\CarbonPeriod; use Carbon\CarbonPeriod;
use Date;
trait DateTime trait DateTime
{ {
@ -60,42 +60,24 @@ trait DateTime
public function getTimezones() public function getTimezones()
{ {
$groups = [];
// The list of available timezone groups to use. // The list of available timezone groups to use.
$use_zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); $use_zones = ['Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'];
// Get the list of time zones from the server. // Get the list of time zones from the server.
$zones = \DateTimeZone::listIdentifiers(); $zones = \DateTimeZone::listIdentifiers();
// Build the group lists. return collect($zones)
foreach ($zones as $zone) { ->mapWithKeys(function ($timezone) use ($use_zones) {
// Time zones not in a group we will ignore. if (! in_array(str($timezone)->before('/'), $use_zones)) {
if (strpos($zone, '/') === false) { return [];
continue;
} }
// Get the group/locale from the timezone. return [$timezone => str($timezone)->after('/')->value()];
list ($group, $locale) = explode('/', $zone, 2); })
->groupBy(function ($item, $key) {
// Only use known groups. return str($key)->before('/')->value();
if (in_array($group, $use_zones)) { }, preserveKeys: true)
// Initialize the group if necessary. ->toArray();
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;
} }
public function getFinancialStart($year = null) public function getFinancialStart($year = null)