Better display of normal Time instances with humanize: Today, Tomorrow, and actual time instead of hours.

This commit is contained in:
Lonnie Ezell 2017-07-26 23:31:14 -05:00
parent 0eca54f09f
commit efb0b47647
No known key found for this signature in database
GPG Key ID: 8EB408F8D82F5002
2 changed files with 32 additions and 23 deletions

View File

@ -1119,13 +1119,22 @@ class Time extends DateTime
}
else if ($days !== 0)
{
$phrase = lang('Time.days', [abs($days)]);
$before = $days < 0;
// Yesterday/Tommorrow special cases
if (abs($days) === 1)
{
return $before
? lang('Time.yesterday')
: lang('Time.tomorrow');
}
$phrase = lang('Time.days', [abs($days)]);
}
else if ($hours !== 0)
{
$phrase = lang('Time.hours', [abs($hours)]);
$before = $hours < 0;
// Display the actual time instead of a regular phrase.
return $this->format('g:i a');
}
else if ($minutes !== 0)
{
@ -1138,8 +1147,8 @@ class Time extends DateTime
}
return $before
? $phrase .' '. lang('ago')
: lang('Time.inFuture') .' '. $phrase;
? lang('Time.ago', [$phrase])
: lang('Time.inFuture', [$phrase]);
}
/**

View File

@ -829,9 +829,9 @@ class TimeTest extends \CIUnitTestCase
public function testHumanizeDaysSingle()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 9, 2017', 'America/Chicago');
$time = Time::parse('March 8, 2017', 'America/Chicago');
$this->assertEquals('1 day ago', $time->humanize());
$this->assertEquals('2 days ago', $time->humanize());
}
public function testHumanizeDaysPlural()
@ -843,35 +843,35 @@ class TimeTest extends \CIUnitTestCase
}
public function testHumanizeDaysForward()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 12, 2017', 'America/Chicago');
$this->assertEquals('in 2 days', $time->humanize());
}
public function testHumanizeDaysTomorrow()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 11, 2017', 'America/Chicago');
$this->assertEquals('in 1 day', $time->humanize());
$this->assertEquals('Tomorrow', $time->humanize());
}
public function testHumanizeHoursSingle()
public function testHumanizeDaysYesterday()
{
Time::setTestNow('March 10, 2017 12:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 11:00', 'America/Chicago');
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 9, 2017', 'America/Chicago');
$this->assertEquals('1 hour ago', $time->humanize());
$this->assertEquals('Yesterday', $time->humanize());
}
public function testHumanizeHoursPlural()
public function testHumanizeHoursAsTime()
{
Time::setTestNow('March 10, 2017 12:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 10:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 14:00', 'America/Chicago');
$this->assertEquals('2 hours ago', $time->humanize());
}
public function testHumanizeHoursForward()
{
Time::setTestNow('March 10, 2017 12:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 13:00', 'America/Chicago');
$this->assertEquals('in 1 hour', $time->humanize());
$this->assertEquals('2:00 pm', $time->humanize());
}
public function testHumanizeMinutesSingle()