Skip to content

Commit

Permalink
Added locale support for Time::nice()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 21, 2014
1 parent 387f9d7 commit bf857e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Utility/Time.php
Expand Up @@ -45,6 +45,13 @@ class Time extends Carbon {
*/
public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];

/**
* The default locale to be used for displaying formatted date strings.
*
* @var string
*/
public static $defaultLocale;

/**
* The format to use when formatting a time using `Cake\Utility\Time::timeAgoInWords()`
* and the difference is more than `Cake\Utility\Time::$wordEnd`
Expand Down Expand Up @@ -101,15 +108,16 @@ public function __construct($time = null, $tz = null) {
* be changed.
* @return string Formatted date string
*/
public function nice($timezone = null) {
public function nice($timezone = null, $locale = null) {
$time = $this;

if ($timezone) {
$time = clone $this;
$time->timezone($timezone);
}

return IntlDateFormatter::formatObject($time, static::$niceFormat);
$locale = $locale ?: static::$defaultLocale;
return IntlDateFormatter::formatObject($time, static::$niceFormat, $locale);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Utility/TimeTest.php
Expand Up @@ -363,6 +363,9 @@ public function testNice() {
$result = $time->nice('America/New_York');
$this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
$this->assertEquals('UTC', $time->getTimezone()->getName());

$this->assertEquals('20 avr. 2014 20:00', $time->nice(null, 'fr-FR'));
$this->assertEquals('20 avr. 2014 16:00', $time->nice('America/New_York', 'fr-FR'));
}

/**
Expand Down

0 comments on commit bf857e2

Please sign in to comment.