Skip to content

Commit

Permalink
Merge pull request #8146 from antograssiot/issue-8142
Browse files Browse the repository at this point in the history
Fix TimeHelper incorrectly formatting immutable datetime objects.
  • Loading branch information
markstory committed Jan 31, 2016
2 parents b081462 + cf1993a commit b38fd49
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/I18n/Time.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\MutableDateTime;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use IntlDateFormatter;
use JsonSerializable;
Expand Down Expand Up @@ -102,6 +103,9 @@ class Time extends MutableDateTime implements JsonSerializable
*/
public function __construct($time = null, $tz = null)
{
if ($time instanceof DateTimeImmutable) {
$time = $time->toMutable();
}
if ($time instanceof DateTime) {
$tz = $time->getTimeZone();
$time = $time->format('Y-m-d H:i:s');
Expand Down
25 changes: 23 additions & 2 deletions tests/TestCase/View/Helper/TimeHelperTest.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\I18n\I18n;
use Cake\I18n\Time;
use Cake\TestSuite\TestCase;
use Cake\View\Helper\TimeHelper;
Expand All @@ -42,6 +43,19 @@ public function setUp()
$this->View = new View();
$this->Time = new TimeHelper($this->View);
Time::$defaultLocale = 'en_US';
$this->locale = I18n::locale();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
Time::$defaultLocale = 'en_US';
I18n::locale($this->locale);
}

/**
Expand Down Expand Up @@ -431,6 +445,13 @@ public function testFormat()
$result = $this->Time->format('invalid date', null, 'Date invalid');
$expected = 'Date invalid';
$this->assertEquals($expected, $result);

I18n::locale('fr_FR');
Time::$defaultLocale = 'fr_FR';
$time = new \Cake\I18n\FrozenTime('Thu Jan 14 13:59:28 2010');
$result = $this->Time->format($time, \IntlDateFormatter::FULL);
$expected = 'jeudi 14 janvier 2010 13:59:28 UTC';
$this->assertTimeFormat($expected, $result);
}

/**
Expand Down Expand Up @@ -475,8 +496,8 @@ public function testFormatTimeInstance()
public function assertTimeFormat($expected, $result)
{
return $this->assertEquals(
str_replace([',', '(', ')', ' at'], '', $expected),
str_replace([',', '(', ')', ' at'], '', $result)
str_replace([',', '(', ')', ' at', ' à'], '', $expected),
str_replace([',', '(', ')', ' at', ' à'], '', $result)
);
}

Expand Down

0 comments on commit b38fd49

Please sign in to comment.