Skip to content
Permalink
Browse files

CakeTime::listTimezones(): Add option to Display timezone abbreviations

Useful for countries that do not have many of its cities, even major ones,
listed. For eg: Indonesia, only have 4 cities listed.

For backward compatibility, abbreviations will not be shown.

Note: You might need to update timezonedb for PHP 5.3

Closes #7271
  • Loading branch information...
rchavik committed Aug 20, 2015
1 parent 6fae3f6 commit e6acacac031a2a0b9ebd0edf07d77cb98d9d2880
Showing with 46 additions and 4 deletions.
  1. +1 −0 .travis.yml
  2. +13 −0 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  3. +32 −4 lib/Cake/Utility/CakeTime.php
@@ -37,6 +37,7 @@ before_script:
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE SCHEMA test3;' -U postgres -d cakephp_test; fi"
- chmod -R 777 ./app/tmp
- sudo apt-get install lighttpd
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.3" ]] ; then pecl install timezonedb ; fi
- sh -c "if [ '$PHPCS' = '1' ]; then composer global require 'cakephp/cakephp-codesniffer:1.*'; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then ~/.composer/vendor/bin/phpcs --config-set installed_paths ~/.composer/vendor/cakephp/cakephp-codesniffer; fi"
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
@@ -1166,6 +1166,19 @@ public function testListTimezones() {
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));
$return = CakeTime::listTimezones(null, null, array('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 = CakeTime::listTimezones(null, null, array(
'abbr' => true,
'before' => ' (',
'after' => ')',
));
$this->assertEquals('Jayapura (WIT)', $return['Asia']['Asia/Jayapura']);
$this->assertEquals('Amsterdam (CEST)', $return['Europe']['Europe/Amsterdam']);
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
$this->assertTrue(isset($return['Pacific/Tahiti']));
@@ -1075,12 +1075,29 @@ public static function i18nFormat($date, $format = null, $default = false, $time
* Or one of DateTimeZone class constants (PHP 5.3 and above)
* @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 (available only in PHP 5.3 and above)
* @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
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::listTimezones
*/
public static function listTimezones($filter = null, $country = null, $group = true) {
public static function listTimezones($filter = null, $country = null, $options = array()) {
if (is_bool($options)) {
$options = array(
'group' => $options,
);
}
$options = array_merge(array(
'group' => true,
'abbr' => false,
'before' => ' - ',
'after' => null,
), $options);
$group = $options['group'];
$regex = null;
if (is_string($filter)) {
$regex = $filter;
@@ -1108,12 +1125,23 @@ public static function listTimezones($filter = null, $country = null, $group = t
if ($group) {
$return = array();
$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])) {
$return[$item[0]][$tz] = $item[1];
$return[$item[0]][$tz] = $item[1] . $abbr;
} else {
$return[$item[0]] = array($tz => $item[0]);
$return[$item[0]] = array($tz => $item[0] . $abbr);
}
}
return $return;

0 comments on commit e6acaca

Please sign in to comment.
You can’t perform that action at this time.