Skip to content

Commit

Permalink
Merge pull request #7294 from cakephp/3.1-tz-abbr
Browse files Browse the repository at this point in the history
Time::listTimezones(): Add option to display timezone abbreviations
  • Loading branch information
lorenzo committed Aug 24, 2015
2 parents 70e411b + 5327b3e commit ec38318
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/I18n/Time.php
Expand Up @@ -641,12 +641,29 @@ public function __toString()
* Or one of DateTimeZone class constants
* @param string $country A two-letter ISO 3166-1 compatible country code.
* This option is only used when $filter is set to DateTimeZone::PER_COUNTRY
* @param bool $group If true (default value) groups the identifiers list by primary region
* @param bool|array $options If true (default value) groups the identifiers list by primary region.
* Otherwise, an array containing `group`, `abbr`, `before`, and `after`
* keys. Setting `group` and `abbr` to true will group results and append
* timezone abbreviation in the display value. Set `before` and `after`
* to customize the abbreviation wrapper.
* @return array List of timezone identifiers
* @since 2.2
*/
public static function listTimezones($filter = null, $country = null, $group = true)
public static function listTimezones($filter = null, $country = null, $options = [])
{
if (is_bool($options)) {
$options = [
'group' => $options,
];
}
$options = array_merge([
'group' => true,
'abbr' => false,
'before' => ' - ',
'after' => null,
], $options);
$group = $options['group'];

$regex = null;
if (is_string($filter)) {
$regex = $filter;
Expand All @@ -667,12 +684,23 @@ public static function listTimezones($filter = null, $country = null, $group = t

if ($group) {
$groupedIdentifiers = [];
$now = time();
$before = $options['before'];
$after = $options['after'];
foreach ($identifiers as $key => $tz) {
$abbr = null;
if ($options['abbr']) {
$dateTimeZone = new \DateTimeZone($tz);
$trans = $dateTimeZone->getTransitions($now, $now);
$abbr = isset($trans[0]['abbr']) ?
$before . $trans[0]['abbr'] . $after :
null;
}
$item = explode('/', $tz, 2);
if (isset($item[1])) {
$groupedIdentifiers[$item[0]][$tz] = $item[1];
$groupedIdentifiers[$item[0]][$tz] = $item[1] . $abbr;
} else {
$groupedIdentifiers[$item[0]] = [$tz => $item[0]];
$groupedIdentifiers[$item[0]] = [$tz => $item[0] . $abbr];
}
}
return $groupedIdentifiers;
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/I18n/TimeTest.php
Expand Up @@ -630,6 +630,19 @@ public function testListTimezones()
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));

$return = Time::listTimezones(null, null, ['abbr' => true]);
$this->assertTrue(isset($return['Asia']['Asia/Jakarta']));
$this->assertEquals('Jakarta - WIB', $return['Asia']['Asia/Jakarta']);
$this->assertEquals('Amsterdam - CEST', $return['Europe']['Europe/Amsterdam']);

$return = Time::listTimezones(null, null, [
'abbr' => true,
'before' => ' (',
'after' => ')',
]);
$this->assertEquals('Jayapura (WIT)', $return['Asia']['Asia/Jayapura']);
$this->assertEquals('Amsterdam (CEST)', $return['Europe']['Europe/Amsterdam']);

$return = Time::listTimezones('#^(America|Pacific)/#', null, false);
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
$this->assertTrue(isset($return['Pacific/Tahiti']));
Expand Down

0 comments on commit ec38318

Please sign in to comment.