Skip to content

Commit

Permalink
Merge pull request #6120 from kenjis/fix-Time-humanize
Browse files Browse the repository at this point in the history
fix: `Time::humanize()` causes error with ar locale
  • Loading branch information
kenjis authored Jun 15, 2022
2 parents ea5b283 + 15cd4ca commit 48a07b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1106,7 +1106,7 @@ public function getUTCObject($time, ?string $timezone = null)
*/
public function getCalendar()
{
return IntlCalendar::fromDateTime($this->toDateTimeString());
return IntlCalendar::fromDateTime($this);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 48a07b5

Please sign in to comment.