diff --git a/system/I18n/Time.php b/system/I18n/Time.php index 64b4d350e8fd..659a0a252db2 100644 --- a/system/I18n/Time.php +++ b/system/I18n/Time.php @@ -82,7 +82,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc // If a test instance has been provided, use it instead. if ($time === '' && static::$testNow instanceof self) { $timezone = $timezone ?: static::$testNow->getTimezone(); - $time = (string) static::$testNow->toDateTimeString(); + $time = static::$testNow->format('Y-m-d H:i:s'); } $timezone = $timezone ?: date_default_timezone_get(); @@ -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 03f142479478..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'); @@ -1058,4 +1085,18 @@ public function testUnserializeTimeObject() $this->assertTrue($time2->equals($time1)); $this->assertNotSame($time1, $time2); } + + public function testSetTestNowWithFaLocale() + { + $currentLocale = Locale::getDefault(); + Locale::setDefault('fa'); + + Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo'); + + $now = Time::now()->format('c'); + + $this->assertSame('2017-03-10T12:00:00+09:00', $now); + + Locale::setDefault($currentLocale); + } }