Permalink
Browse files

Making CakeTime display 'about X ago' when time passed is lower than …

…accuracy

Removing unnecessary else

Also support 'about' times for future times

Make 'about' times translateable in all languages
  • Loading branch information...
1 parent 1bdcc58 commit 1cb41294b8c44cbbdf92fba538947e8d494da3de @ankr ankr committed Aug 12, 2013
Showing with 70 additions and 16 deletions.
  1. +26 −0 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. +44 −16 lib/Cake/Utility/CakeTime.php
@@ -227,6 +227,20 @@ public function testTimeAgoInWordsAccuracy() {
);
$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 @@ public function testTimeAgoInWordsNegativeValues() {
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);
}
/**
@@ -820,48 +820,76 @@ public static function timeAgoInWords($dateTime, $options = array()) {
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) {
- return __d('cake', '%s ago', $relativeDate);
+ if ($relativeDate) {
+ return __d('cake', '%s ago', $relativeDate);
+ }
+
+ return $aboutAgo[$fWord];
+ }
+
+ // When time is to come
+ if (!$relativeDate) {
+ return $aboutIn[$fWord];
}
return $relativeDate;

0 comments on commit 1cb4129

Please sign in to comment.