Skip to content

Commit e6acaca

Browse files
committed
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
1 parent 6fae3f6 commit e6acaca

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ before_script:
3737
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE SCHEMA test3;' -U postgres -d cakephp_test; fi"
3838
- chmod -R 777 ./app/tmp
3939
- sudo apt-get install lighttpd
40+
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.3" ]] ; then pecl install timezonedb ; fi
4041
- sh -c "if [ '$PHPCS' = '1' ]; then composer global require 'cakephp/cakephp-codesniffer:1.*'; fi"
4142
- sh -c "if [ '$PHPCS' = '1' ]; then ~/.composer/vendor/bin/phpcs --config-set installed_paths ~/.composer/vendor/cakephp/cakephp-codesniffer; fi"
4243
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

lib/Cake/Test/Case/Utility/CakeTimeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,19 @@ public function testListTimezones() {
11661166
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
11671167
$this->assertFalse(isset($return['Pacific']));
11681168

1169+
$return = CakeTime::listTimezones(null, null, array('abbr' => true));
1170+
$this->assertTrue(isset($return['Asia']['Asia/Jakarta']));
1171+
$this->assertEquals('Jakarta - WIB', $return['Asia']['Asia/Jakarta']);
1172+
$this->assertEquals('Amsterdam - CEST', $return['Europe']['Europe/Amsterdam']);
1173+
1174+
$return = CakeTime::listTimezones(null, null, array(
1175+
'abbr' => true,
1176+
'before' => ' (',
1177+
'after' => ')',
1178+
));
1179+
$this->assertEquals('Jayapura (WIT)', $return['Asia']['Asia/Jayapura']);
1180+
$this->assertEquals('Amsterdam (CEST)', $return['Europe']['Europe/Amsterdam']);
1181+
11691182
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
11701183
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
11711184
$this->assertTrue(isset($return['Pacific/Tahiti']));

lib/Cake/Utility/CakeTime.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,12 +1075,29 @@ public static function i18nFormat($date, $format = null, $default = false, $time
10751075
* Or one of DateTimeZone class constants (PHP 5.3 and above)
10761076
* @param string $country A two-letter ISO 3166-1 compatible country code.
10771077
* This option is only used when $filter is set to DateTimeZone::PER_COUNTRY (available only in PHP 5.3 and above)
1078-
* @param bool $group If true (default value) groups the identifiers list by primary region
1078+
* @param bool|array $options If true (default value) groups the identifiers list by primary region.
1079+
* Otherwise, an array containing `group`, `abbr`, `before`, and `after` keys.
1080+
* Setting `group` and `abbr` to true will group results and append timezone
1081+
* abbreviation in the display value. Set `before` and `after` to customize
1082+
* the abbreviation wrapper.
10791083
* @return array List of timezone identifiers
10801084
* @since 2.2
10811085
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::listTimezones
10821086
*/
1083-
public static function listTimezones($filter = null, $country = null, $group = true) {
1087+
public static function listTimezones($filter = null, $country = null, $options = array()) {
1088+
if (is_bool($options)) {
1089+
$options = array(
1090+
'group' => $options,
1091+
);
1092+
}
1093+
$options = array_merge(array(
1094+
'group' => true,
1095+
'abbr' => false,
1096+
'before' => ' - ',
1097+
'after' => null,
1098+
), $options);
1099+
$group = $options['group'];
1100+
10841101
$regex = null;
10851102
if (is_string($filter)) {
10861103
$regex = $filter;
@@ -1108,12 +1125,23 @@ public static function listTimezones($filter = null, $country = null, $group = t
11081125

11091126
if ($group) {
11101127
$return = array();
1128+
$now = time();
1129+
$before = $options['before'];
1130+
$after = $options['after'];
11111131
foreach ($identifiers as $key => $tz) {
1132+
$abbr = null;
1133+
if ($options['abbr']) {
1134+
$dateTimeZone = new DateTimeZone($tz);
1135+
$trans = $dateTimeZone->getTransitions($now, $now);
1136+
$abbr = isset($trans[0]['abbr']) ?
1137+
$before . $trans[0]['abbr'] . $after :
1138+
null;
1139+
}
11121140
$item = explode('/', $tz, 2);
11131141
if (isset($item[1])) {
1114-
$return[$item[0]][$tz] = $item[1];
1142+
$return[$item[0]][$tz] = $item[1] . $abbr;
11151143
} else {
1116-
$return[$item[0]] = array($tz => $item[0]);
1144+
$return[$item[0]] = array($tz => $item[0] . $abbr);
11171145
}
11181146
}
11191147
return $return;

0 commit comments

Comments
 (0)