Merge pull request #1511 from ankr/caketime_accuracy_about

Making CakeTime display 'about X ago' when time passed is lower than acc...
This commit is contained in:
Christian Winther 2013-08-12 09:23:46 -07:00
commit af1c2a1c51
2 changed files with 70 additions and 16 deletions

View file

@ -227,6 +227,20 @@ class CakeTimeTest extends CakeTestCase {
);
$expected = '1 year';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+58 minutes'),
array('accuracy' => 'hour')
);
$expected = 'in about an hour';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+23 hours'),
array('accuracy' => 'day')
);
$expected = 'in about a day';
$this->assertEquals($expected, $result);
}
/**
@ -314,6 +328,18 @@ class CakeTimeTest extends CakeTestCase {
array('end' => '2 years')
);
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
$result = $this->Time->timeAgoInWords(
strtotime('-58 minutes'),
array('accuracy' => 'hour')
);
$this->assertEquals('about an hour ago', $result);
$result = $this->Time->timeAgoInWords(
strtotime('-23 hours'),
array('accuracy' => 'day')
);
$this->assertEquals('about a day ago', $result);
}
/**

View file

@ -820,50 +820,78 @@ class CakeTime {
return __d('cake', 'on %s', date($format, $inSeconds));
}
$f = $accuracy['second'];
$fWord = $accuracy['second'];
if ($years > 0) {
$f = $accuracy['year'];
$fWord = $accuracy['year'];
} elseif (abs($months) > 0) {
$f = $accuracy['month'];
$fWord = $accuracy['month'];
} elseif (abs($weeks) > 0) {
$f = $accuracy['week'];
$fWord = $accuracy['week'];
} elseif (abs($days) > 0) {
$f = $accuracy['day'];
$fWord = $accuracy['day'];
} elseif (abs($hours) > 0) {
$f = $accuracy['hour'];
$fWord = $accuracy['hour'];
} elseif (abs($minutes) > 0) {
$f = $accuracy['minute'];
$fWord = $accuracy['minute'];
}
$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
$fNum = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $fWord);
$relativeDate = '';
if ($f >= 1 && $years > 0) {
if ($fNum >= 1 && $years > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
}
if ($f >= 2 && $months > 0) {
if ($fNum >= 2 && $months > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
}
if ($f >= 3 && $weeks > 0) {
if ($fNum >= 3 && $weeks > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
}
if ($f >= 4 && $days > 0) {
if ($fNum >= 4 && $days > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
}
if ($f >= 5 && $hours > 0) {
if ($fNum >= 5 && $hours > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
}
if ($f >= 6 && $minutes > 0) {
if ($fNum >= 6 && $minutes > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
}
if ($f >= 7 && $seconds > 0) {
if ($fNum >= 7 && $seconds > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
}
$aboutAgo = array(
'second' => __d('cake', 'about a second ago'),
'minute' => __d('cake', 'about a minute ago'),
'hour' => __d('cake', 'about an hour ago'),
'day' => __d('cake', 'about a day ago'),
'week' => __d('cake', 'about a week ago'),
'year' => __d('cake', 'about a year ago')
);
$aboutIn = array(
'second' => __d('cake', 'in about a second'),
'minute' => __d('cake', 'in about a minute'),
'hour' => __d('cake', 'in about an hour'),
'day' => __d('cake', 'in about a day'),
'week' => __d('cake', 'in about a week'),
'year' => __d('cake', 'in about a year')
);
// When time has passed
if (!$backwards) {
if ($relativeDate) {
return __d('cake', '%s ago', $relativeDate);
}
return $aboutAgo[$fWord];
}
// When time is to come
if (!$relativeDate) {
return $aboutIn[$fWord];
}
return $relativeDate;
}