Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Time::humanize() causes error with ar locale #6120

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's kind of funny that we were using an unnecessary intermediate string to begin with 😅

$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
41 changes: 41 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());
kenjis marked this conversation as resolved.
Show resolved Hide resolved

Locale::setDefault($currentLocale);
}

public function testSetTimezoneDate()
{
$time = Time::parse('13 May 2020 10:00', 'GMT');
Expand Down Expand Up @@ -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);
}
}