Skip to content

Commit 48a07b5

Browse files
authored
Merge pull request #6120 from kenjis/fix-Time-humanize
fix: `Time::humanize()` causes error with ar locale
2 parents ea5b283 + 15cd4ca commit 48a07b5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

system/I18n/Time.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ public function isAfter($testTime, ?string $timezone = null): bool
10051005
*/
10061006
public function humanize()
10071007
{
1008-
$now = IntlCalendar::fromDateTime(self::now($this->timezone)->toDateTimeString());
1008+
$now = IntlCalendar::fromDateTime(self::now($this->timezone));
10091009
$time = $this->getCalendar()->getTime();
10101010

10111011
$years = $now->fieldDifference($time, IntlCalendar::FIELD_YEAR);
@@ -1106,7 +1106,7 @@ public function getUTCObject($time, ?string $timezone = null)
11061106
*/
11071107
public function getCalendar()
11081108
{
1109-
return IntlCalendar::fromDateTime($this->toDateTimeString());
1109+
return IntlCalendar::fromDateTime($this);
11101110
}
11111111

11121112
/**

tests/system/I18n/TimeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace CodeIgniter\I18n;
1313

14+
use CodeIgniter\Config\Factories;
1415
use CodeIgniter\I18n\Exceptions\I18nException;
1516
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\App;
1618
use DateTime;
1719
use DateTimeZone;
1820
use IntlDateFormatter;
@@ -1025,6 +1027,31 @@ public function testHumanizeNow()
10251027
$this->assertSame('Just now', $time->humanize());
10261028
}
10271029

1030+
/**
1031+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4708
1032+
*/
1033+
public function testHumanizeWithArLocale()
1034+
{
1035+
$this->resetServices();
1036+
1037+
$currentLocale = Locale::getDefault();
1038+
Locale::setDefault('ar');
1039+
1040+
$config = new App();
1041+
$config->supportedLocales = ['ar'];
1042+
$config->defaultLocale = 'ar';
1043+
Factories::injectMock('config', 'App', $config);
1044+
1045+
Time::setTestNow('2022-06-14 12:00', 'America/Chicago');
1046+
1047+
$date = '2022-06-07 12:00';
1048+
$time = Time::parse($date, 'America/Chicago');
1049+
1050+
$this->assertSame('١ week ago', $time->humanize());
1051+
1052+
Locale::setDefault($currentLocale);
1053+
}
1054+
10281055
public function testSetTimezoneDate()
10291056
{
10301057
$time = Time::parse('13 May 2020 10:00', 'GMT');

0 commit comments

Comments
 (0)