Added CakeTime::listIdentifiers()

This commit is contained in:
ADmad 2012-05-12 20:33:56 +05:30
parent 024e30eb42
commit cfd9d8a815
2 changed files with 89 additions and 1 deletions

View file

@ -999,4 +999,39 @@ class CakeTimeTest extends CakeTestCase {
$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
}
/**
* testListTimezones
*
* @return void
*/
public function testListTimezones() {
$return = CakeTime::listTimezones();
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
$this->assertTrue(isset($return['UTC']['UTC']));
$this->assertFalse(isset($return['Cuba']));
$this->assertFalse(isset($return['US']));
$return = CakeTime::listTimezones('#^Asia/#');
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
$this->assertTrue(isset($return['Pacific/Tahiti']));
if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
$return = CakeTime::listTimezones(DateTimeZone::ASIA);
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));
$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
$this->assertTrue(isset($return['Pacific/Honolulu']));
$this->assertFalse(isset($return['Asia/Bangkok']));
}
}
}

View file

@ -560,7 +560,7 @@ class CakeTime {
/**
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
*
*
* @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
* @return integer Unix timestamp
@ -972,6 +972,59 @@ class CakeTime {
return self::_strftime($format, $date);
}
/**
* Get list of timezone identifiers
*
* @param int|string $filter A regex to filter identifer
* 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 boolean $group If true (default value) groups the identifiers list by primary region
* @return array List of timezone identifiers
* @since 2.2
*/
public static function listTimezones($filter = null, $country = null, $group = true) {
$regex = null;
if (is_string($filter)) {
$regex = $filter;
$filter = null;
}
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
if ($regex === null) {
$regex = '#^(Africa|America|Antartica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific|UTC)/#';
}
$identifiers = DateTimeZone::listIdentifiers();
} else {
if ($filter === null) {
$filter = DateTimeZone::ALL;
}
$identifiers = DateTimeZone::listIdentifiers($filter, $country);
}
if ($regex) {
foreach ($identifiers as $key => $tz) {
if (!preg_match($regex, $tz)) {
unset($identifiers[$key]);
}
}
}
if ($group) {
$return = array();
foreach ($identifiers as $key => $tz) {
$item = explode('/', $tz, 2);
if (isset($item[1])) {
$return[$item[0]][$tz] = $item[1];
} else {
$return[$item[0]] = array($tz => $item[0]);
}
}
return $return;
} else {
return array_combine($identifiers, $identifiers);
}
}
/**
* Multibyte wrapper for strftime.
*