From dc4bd1d9e93d0f5b1362b1cdc132f58fe70338ae Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 20 Apr 2014 11:14:12 +0200 Subject: [PATCH] Re-implementing Time::nice() so it uses IntlDateFormatter --- src/Utility/Time.php | 18 ++++++++++---- tests/TestCase/Utility/TimeTest.php | 37 ++++------------------------- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/Utility/Time.php b/src/Utility/Time.php index 86f8c6417e3..688a21b846b 100644 --- a/src/Utility/Time.php +++ b/src/Utility/Time.php @@ -18,6 +18,7 @@ use Cake\Core\Configure; use Carbon\Carbon; +use IntlDateFormatter; /** * Time Helper class for easy use of time data. @@ -34,10 +35,10 @@ class Time extends Carbon { * The format should use the locale strings as defined in the PHP docs under * `strftime` (http://php.net/manual/en/function.strftime.php) * - * @var string - * @see \Cake\Utility\Time::format() + * @var mixed + * @see \Cake\Utility\Time::nice() */ - public static $niceFormat = '%a, %b %eS %Y, %H:%M'; + public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT]; /** * The format to use when formatting a time using `Cake\Utility\Time::timeAgoInWords()` @@ -93,11 +94,18 @@ public function __construct($time = null, $tz = null) { * * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object * @param string|\DateTimeZone $timezone Timezone string or DateTimeZone object - * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used * @return string Formatted date string * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::nice */ - public function nice($date = null, $timezone = null, $format = null) { + public function nice($timezone = null) { + $time = $this; + + if ($timezone) { + $time = clone $this; + $time->timezone($timezone); + } + + return IntlDateFormatter::formatObject($time, static::$niceFormat); } /** diff --git a/tests/TestCase/Utility/TimeTest.php b/tests/TestCase/Utility/TimeTest.php index 4e59e864023..d9467aed41a 100644 --- a/tests/TestCase/Utility/TimeTest.php +++ b/tests/TestCase/Utility/TimeTest.php @@ -357,39 +357,12 @@ public function testTimeAgoInWordsNegativeValues() { * @return void */ public function testNice() { - $time = time() + 2 * DAY; - $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); + $time = new Time('2014-04-20 20:00', 'UTC'); + $this->assertEquals('Apr 20, 2014, 8:00 PM', $time->nice()); - $time = time() - 2 * DAY; - $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); - - $time = time(); - $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); - - $time = 0; - $this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); - - $time = null; - $this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); - - $time = time(); - $this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a')); - $this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y')); - - Time::$niceFormat = '%Y-%d-%m'; - $this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time)); - $this->assertEquals('%Y-%d-%m', Time::$niceFormat); - - Time::$niceFormat = '%Y-%d-%m %H:%M'; - $this->assertEquals(date('Y-d-m H:i', $time), $this->Time->nice($time)); - $this->assertEquals('%Y-%d-%m %H:%M', Time::$niceFormat); - - date_default_timezone_set('UTC'); - $result = $this->Time->nice(null, 'America/New_York'); - $expected = $this->Time->nice(time(), 'America/New_York'); - $this->assertEquals(substr($expected, 0, -1), substr($result, 0, -1)); - - $this->_restoreSystemTimezone(); + $result = $time->nice('America/New_York'); + $this->assertEquals('Apr 20, 2014, 4:00 PM', $result); + $this->assertEquals('UTC', $time->getTimezone()->getName()); } /**