From 15cd4ca05020b1aa25090ff4186aa6fa97ccc0a1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 14 Jun 2022 14:38:56 +0900 Subject: [PATCH] fix: Time::humanize() causes error with ar locale Fixes #4708 --- system/I18n/Time.php | 4 ++-- tests/system/I18n/TimeTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/system/I18n/Time.php b/system/I18n/Time.php index dd97815f66cf..659a0a252db2 100644 --- a/system/I18n/Time.php +++ b/system/I18n/Time.php @@ -1005,7 +1005,7 @@ public function isAfter($testTime, ?string $timezone = null): bool */ public function humanize() { - $now = IntlCalendar::fromDateTime(self::now($this->timezone)->toDateTimeString()); + $now = IntlCalendar::fromDateTime(self::now($this->timezone)); $time = $this->getCalendar()->getTime(); $years = $now->fieldDifference($time, IntlCalendar::FIELD_YEAR); @@ -1106,7 +1106,7 @@ public function getUTCObject($time, ?string $timezone = null) */ public function getCalendar() { - return IntlCalendar::fromDateTime($this->toDateTimeString()); + return IntlCalendar::fromDateTime($this); } /** diff --git a/tests/system/I18n/TimeTest.php b/tests/system/I18n/TimeTest.php index 7571f66eca99..2f2980f2411e 100644 --- a/tests/system/I18n/TimeTest.php +++ b/tests/system/I18n/TimeTest.php @@ -11,8 +11,10 @@ namespace CodeIgniter\I18n; +use CodeIgniter\Config\Factories; use CodeIgniter\I18n\Exceptions\I18nException; use CodeIgniter\Test\CIUnitTestCase; +use Config\App; use DateTime; use DateTimeZone; use IntlDateFormatter; @@ -1025,6 +1027,31 @@ public function testHumanizeNow() $this->assertSame('Just now', $time->humanize()); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/4708 + */ + public function testHumanizeWithArLocale() + { + $this->resetServices(); + + $currentLocale = Locale::getDefault(); + Locale::setDefault('ar'); + + $config = new App(); + $config->supportedLocales = ['ar']; + $config->defaultLocale = 'ar'; + Factories::injectMock('config', 'App', $config); + + Time::setTestNow('2022-06-14 12:00', 'America/Chicago'); + + $date = '2022-06-07 12:00'; + $time = Time::parse($date, 'America/Chicago'); + + $this->assertSame('١ week ago', $time->humanize()); + + Locale::setDefault($currentLocale); + } + public function testSetTimezoneDate() { $time = Time::parse('13 May 2020 10:00', 'GMT');