Browse Source

Implemented Time::i18nFormat using IntlDateFormatter

Jose Lorenzo Rodriguez 12 years ago
parent
commit
f67dde6a6d
2 changed files with 45 additions and 47 deletions
  1. 35 11
      src/Utility/Time.php
  2. 10 36
      tests/TestCase/Utility/TimeTest.php

+ 35 - 11
src/Utility/Time.php

@@ -29,6 +29,24 @@ use IntlDateFormatter;
  */
 class Time extends Carbon {
 
+
+/**
+ * The format to use when formatting a time using `Cake\Utility\Time::i18nFormat()`
+ * and `__toString`
+ *
+ * The format should be eiter the formatting constants from IntDateFormatter as
+ * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
+ * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
+ *
+ * It is possible to provide an array of 2 constants. In this case, the first position
+ * will be used for formatting the date part of the object and the second position
+ * will be used to format the time part.
+ *
+ * @var mixed
+ * @see \Cake\Utility\Time::i18nFormat()
+ */
+	protected static $toStringFormat = [IntlDateFormatter::SHORT, IntlDateFormatter::SHORT];
+
 /**
  * The format to use when formatting a time using `Cake\Utility\Time::nice()`
  *
@@ -106,18 +124,11 @@ class Time extends Carbon {
  * @param string|\DateTimeZone $timezone Timezone string or DateTimeZone object
  * in which the date will be displayed. The timezone stored for this object will not
  * be changed.
+ * @param $locale The locale name in which the date should be displayed (e.g. pt-BR)
  * @return string Formatted date string
  */
 	public function nice($timezone = null, $locale = null) {
-		$time = $this;
-
-		if ($timezone) {
-			$time = clone $this;
-			$time->timezone($timezone);
-		}
-
-		$locale = $locale ?: static::$defaultLocale;
-		return IntlDateFormatter::formatObject($time, static::$niceFormat, $locale);
+		return $this->i18nFormat(static::$niceFormat, $timezone, $locale);
 	}
 
 /**
@@ -466,11 +477,24 @@ class Time extends Carbon {
  * }}}
  *
  * @param string $format strftime format string.
- * @param bool|string $default if an invalid date is passed it will output supplied default value. Pass false if you want raw conversion value
+ * @param string|\DateTimeZone $timezone Timezone string or DateTimeZone object
+ * in which the date will be displayed. The timezone stored for this object will not
+ * be changed.
+ * @param $locale The locale name in which the date should be displayed (e.g. pt-BR)
  * @return string Formatted and translated date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::i18nFormat
  */
-	public static function i18nFormat($format = null, $default = false) {
+	public function i18nFormat($format = null, $timezone = null, $locale = null) {
+		$time = $this;
+
+		if ($timezone) {
+			$time = clone $this;
+			$time->timezone($timezone);
+		}
+
+		$format = $format !== null ? $format : static::$toStringFormat;
+		$locale = $locale ?: static::$defaultLocale;
+		return IntlDateFormatter::formatObject($time, $format, $locale);
 	}
 
 /**

+ 10 - 36
tests/TestCase/Utility/TimeTest.php

@@ -483,53 +483,27 @@ class TimeTest extends TestCase {
  * @return void
  */
 	public function testI18nFormat() {
-		Configure::write('Config.language', 'es');
+		$time = new Time('Thu Jan 14 13:59:28 2010');
+		$result = $time->i18nFormat();
 
-		$time = strtotime('Thu Jan 14 13:59:28 2010');
-
-		$result = $this->Time->i18nFormat($time);
-		$expected = '14/01/10';
-		$this->assertEquals($expected, $result);
-
-		$result = $this->Time->i18nFormat($time, '%c');
-		$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
-		$this->assertEquals($expected, $result);
-
-		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
-		$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
-		$this->assertEquals($expected, $result);
-
-		$time = strtotime('Wed Jan 13 13:59:28 2010');
-
-		$result = $this->Time->i18nFormat($time);
-		$expected = '13/01/10';
+		$expected = '1/14/10, 1:59 PM';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Time->i18nFormat($time, '%c');
-		$expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
+		$result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
+		$expected = 'jueves, 14 de enero de 2010, 13:59:28 (GMT)';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
-		$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
+		$format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
+		$result = $time->i18nFormat($format);
+		$expected = '1:59 PM';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
-		$expected = 'Date invalid';
+		$result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
+		$expected = '00:59:28';
 		$this->assertEquals($expected, $result);
 	}
 
 /**
- * test new format() syntax which inverts first and second parameters
- *
- * @return void
- */
-	public function testFormatNewSyntax() {
-		$time = time();
-		$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