From d68dc79911cfb804d6b1d50963c8b55588851d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Wylega=C5=82a?= Date: Thu, 30 May 2024 10:14:16 +0200 Subject: [PATCH] CakeTime migrated to Intl. --- composer.json | 5 ++++- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 22 ++++++++++----------- lib/Cake/Utility/CakeTime.php | 5 ++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 42449644f..18bd5abe1 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,10 @@ "source": "https://github.com/cakephp/cakephp" }, "require": { - "php": "^8.0" + "php": "^8.0", + "ext-mbstring": "*", + "symfony/polyfill-intl-icu": "^1.29", + "php81_bc/strftime": "^0.7.5" }, "suggest": { "ext-openssl": "You need to install ext-openssl or ext-mcrypt to use AES-256 encryption", diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index f9ce1158c..3ff7fb1fd 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -279,9 +279,7 @@ public function testTimeAgoInWordsWithFormat() { $this->assertEquals('on 2007-09-25', $result); $result = $this->Time->timeAgoInWords('2007-9-25', '%x'); - // @codingStandardsIgnoreStart - $this->assertEquals('on ' . @strftime('%x', strtotime('2007-9-25')), $result); - // @codingStandardsIgnoreEnd + $this->assertEquals('on ' . PHP81_BC\strftime('%x', strtotime('2007-9-25')), $result); $result = $this->Time->timeAgoInWords( strtotime('+2 weeks +2 days'), @@ -306,7 +304,7 @@ public function testTimeAgoInWordsWithFormat() { array('end' => '1 month', 'format' => '%x') ); // @codingStandardsIgnoreStart - $this->assertEquals('on ' . @strftime('%x', strtotime('+2 months +2 days')), $result); + $this->assertEquals('on ' . PHP81_BC\strftime('%x', strtotime('+2 months +2 days')), $result); // @codingStandardsIgnoreEnd } @@ -476,11 +474,11 @@ public function testNiceShort() { * @return void */ public function testNiceShortI18n() { - $restore = setlocale(LC_ALL, 0); - setlocale(LC_ALL, 'es_ES'); + $restore = Locale::getDefault(); + Locale::setDefault('es_ES'); $time = strtotime('2015-01-07 03:05:00'); $this->assertEquals('ene 7th 2015, 03:05', $this->Time->niceShort($time)); - setlocale(LC_ALL, $restore); + Locale::setDefault($restore); } /** @@ -1166,6 +1164,7 @@ public function testConvertPercentE() { * @return void */ public function testI18nFormat() { + $resetLocale = Locale::getDefault(); App::build(array( 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS) ), App::RESET); @@ -1173,13 +1172,14 @@ public function testI18nFormat() { $time = strtotime('Thu Jan 14 13:59:28 2010'); + Locale::setDefault('en_US'); $result = $this->Time->i18nFormat($time); $expected = '14/01/10'; $this->assertEquals($expected, $result); $result = $this->Time->i18nFormat($time, '%c'); // @codingStandardsIgnoreStart - $expected = 'jue 14 ene 2010 13:59:28 ' . mb_convert_encoding(@strftime('%Z', $time), 'UTF-8', 'ISO-8859-1'); + $expected = 'jue 14 ene 2010 13:59:28 ' . mb_convert_encoding(PHP81_BC\strftime('%Z', $time), 'UTF-8', 'ISO-8859-1'); // @codingStandardsIgnoreEnd $this->assertEquals($expected, $result); @@ -1194,9 +1194,7 @@ public function testI18nFormat() { $this->assertEquals($expected, $result); $result = $this->Time->i18nFormat($time, '%c'); - // @codingStandardsIgnoreStart - $expected = 'mié 13 ene 2010 13:59:28 ' . mb_convert_encoding(@strftime('%Z', $time), 'UTF-8', 'ISO-8859-1'); - // @codingStandardsIgnoreEnd + $expected = 'mié 13 ene 2010 13:59:28 ' . mb_convert_encoding(PHP81_BC\strftime('%Z', $time), 'UTF-8', 'ISO-8859-1'); $this->assertEquals($expected, $result); $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x'); @@ -1206,6 +1204,8 @@ public function testI18nFormat() { $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid'); $expected = 'Date invalid'; $this->assertEquals($expected, $result); + + Locale::setDefault($resetLocale); } public function testI18nFormatTimezoneConversionToUTC() { diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 75fae995e..fb9f1cad0 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -1167,9 +1167,8 @@ public static function listTimezones($filter = null, $country = null, $options = * @return string formatted string with correct encoding. */ protected static function _strftime($format, $timestamp) { - // @codingStandardsIgnoreStart - $format = @strftime($format, $timestamp); - // @codingStandardsIgnoreEnd + $format = PHP81_BC\strftime($format, $timestamp); + $encoding = Configure::read('App.encoding'); if (!empty($encoding) && $encoding === 'UTF-8') { if (function_exists('mb_check_encoding')) {