Browse Source

Added CakeTime::listIdentifiers()

ADmad 14 years ago
parent
commit
cfd9d8a815
2 changed files with 89 additions and 1 deletions
  1. 35 0
      lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. 54 1
      lib/Cake/Utility/CakeTime.php

+ 35 - 0
lib/Cake/Test/Case/Utility/CakeTimeTest.php

@@ -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']));
+		}
+	}
+
 }

+ 54 - 1
lib/Cake/Utility/CakeTime.php

@@ -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
@@ -973,6 +973,59 @@ class CakeTime {
 	}
 
 /**
+ * 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.
  *
  * Handles utf8_encoding the result of strftime when necessary.