Skip to content
Permalink
Browse files

Added CakeTime::listIdentifiers()

  • Loading branch information...
ADmad committed May 12, 2012
1 parent 024e30e commit cfd9d8a8157fb5f70223f3a8e700c55a989bf480
Showing with 89 additions and 1 deletion.
  1. +35 −0 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. +54 −1 lib/Cake/Utility/CakeTime.php
@@ -999,4 +999,39 @@ public function testFormatNewSyntax() {
$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() {

This comment has been minimized.

Copy link
@ceeram
$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']));
}
}
}
@@ -560,7 +560,7 @@ public static function toQuarter($dateString, $range = false) {
/**
* 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 @@ public static function i18nFormat($date, $format = null, $invalid = false, $time
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.
*

0 comments on commit cfd9d8a

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