Skip to content

Commit

Permalink
Re-implementing Time::nice() so it uses IntlDateFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 21, 2014
1 parent 19fef27 commit dc4bd1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
18 changes: 13 additions & 5 deletions src/Utility/Time.php
Expand Up @@ -18,6 +18,7 @@

use Cake\Core\Configure;
use Carbon\Carbon;
use IntlDateFormatter;

/**
* Time Helper class for easy use of time data.
Expand All @@ -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()`
Expand Down Expand Up @@ -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);
}

/**
Expand Down
37 changes: 5 additions & 32 deletions tests/TestCase/Utility/TimeTest.php
Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit dc4bd1d

Please sign in to comment.