From 0f4aa4100e17598c3d1c2539c0e72e5662c200d4 Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sun, 31 Jan 2016 17:10:56 +0100 Subject: [PATCH 1/5] add a failing test to reproduce the issue --- tests/TestCase/View/Helper/TimeHelperTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/TestCase/View/Helper/TimeHelperTest.php b/tests/TestCase/View/Helper/TimeHelperTest.php index 21d6764e24d..a0651dde1d4 100644 --- a/tests/TestCase/View/Helper/TimeHelperTest.php +++ b/tests/TestCase/View/Helper/TimeHelperTest.php @@ -431,6 +431,15 @@ public function testFormat() $result = $this->Time->format('invalid date', null, 'Date invalid'); $expected = 'Date invalid'; $this->assertEquals($expected, $result); + + \Cake\I18n\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); + $expected = '14/01/2010 13:59'; + $this->assertTimeFormat($expected, $result); + \Cake\I18n\I18n::locale('en-US'); + } /** From 37503b1dd7115c8cdf04523bbbb38738b4b38c40 Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sun, 31 Jan 2016 17:11:28 +0100 Subject: [PATCH 2/5] transform immutable datetime to mutable when using the time helper --- src/View/Helper/TimeHelper.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/View/Helper/TimeHelper.php b/src/View/Helper/TimeHelper.php index 51765c5ad84..bb2925fb563 100644 --- a/src/View/Helper/TimeHelper.php +++ b/src/View/Helper/TimeHelper.php @@ -333,6 +333,10 @@ public function i18nFormat($date, $format = null, $invalid = false, $timezone = return $invalid; } + if ($date instanceof \DateTimeImmutable) { + $date = $date->toMutable(); + } + try { $time = new Time($date); return $time->i18nFormat($format, $timezone); From 764f283ef4962c27d1d514631878c6d00e906df4 Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sun, 31 Jan 2016 18:55:24 +0100 Subject: [PATCH 3/5] update test to fix travis build --- tests/TestCase/View/Helper/TimeHelperTest.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/TestCase/View/Helper/TimeHelperTest.php b/tests/TestCase/View/Helper/TimeHelperTest.php index a0651dde1d4..363dc8dbb43 100644 --- a/tests/TestCase/View/Helper/TimeHelperTest.php +++ b/tests/TestCase/View/Helper/TimeHelperTest.php @@ -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; @@ -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); } /** @@ -432,14 +446,12 @@ public function testFormat() $expected = 'Date invalid'; $this->assertEquals($expected, $result); - \Cake\I18n\I18n::locale('fr-FR'); - Time::$defaultLocale = 'fr-FR'; + 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); - $expected = '14/01/2010 13:59'; + $expected = '14/01/10 13:59'; $this->assertTimeFormat($expected, $result); - \Cake\I18n\I18n::locale('en-US'); - } /** From 3e9121b11e4a042c1397f9dbfbbc1fc0bdc28ac8 Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sun, 31 Jan 2016 20:00:42 +0100 Subject: [PATCH 4/5] update test to avoid de be dependent of ICU version so its passes locally and on appveyor --- tests/TestCase/View/Helper/TimeHelperTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/TestCase/View/Helper/TimeHelperTest.php b/tests/TestCase/View/Helper/TimeHelperTest.php index 363dc8dbb43..f180804d4bd 100644 --- a/tests/TestCase/View/Helper/TimeHelperTest.php +++ b/tests/TestCase/View/Helper/TimeHelperTest.php @@ -449,8 +449,8 @@ public function testFormat() 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); - $expected = '14/01/10 13:59'; + $result = $this->Time->format($time, \IntlDateFormatter::FULL); + $expected = 'jeudi 14 janvier 2010 13:59:28 UTC'; $this->assertTimeFormat($expected, $result); } @@ -496,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) ); } From cf1993af2014f6a7051e5463c52d0bf80dd7823b Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sun, 31 Jan 2016 21:18:36 +0100 Subject: [PATCH 5/5] move the check to the Time class --- src/I18n/Time.php | 4 ++++ src/View/Helper/TimeHelper.php | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/I18n/Time.php b/src/I18n/Time.php index 297cc5c2a38..3396ad13fb0 100644 --- a/src/I18n/Time.php +++ b/src/I18n/Time.php @@ -17,6 +17,7 @@ use Cake\Chronos\ChronosInterface; use Cake\Chronos\MutableDateTime; use DateTime; +use DateTimeImmutable; use DateTimeZone; use IntlDateFormatter; use JsonSerializable; @@ -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'); diff --git a/src/View/Helper/TimeHelper.php b/src/View/Helper/TimeHelper.php index bb2925fb563..51765c5ad84 100644 --- a/src/View/Helper/TimeHelper.php +++ b/src/View/Helper/TimeHelper.php @@ -333,10 +333,6 @@ public function i18nFormat($date, $format = null, $invalid = false, $timezone = return $invalid; } - if ($date instanceof \DateTimeImmutable) { - $date = $date->toMutable(); - } - try { $time = new Time($date); return $time->i18nFormat($format, $timezone);