Skip to content

Commit af1c2a1

Browse files
committed
Merge pull request cakephp#1511 from ankr/caketime_accuracy_about
Making CakeTime display 'about X ago' when time passed is lower than acc...
2 parents b44e12b + 1cb4129 commit af1c2a1

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

lib/Cake/Test/Case/Utility/CakeTimeTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ public function testTimeAgoInWordsAccuracy() {
227227
);
228228
$expected = '1 year';
229229
$this->assertEquals($expected, $result);
230+
231+
$result = $this->Time->timeAgoInWords(
232+
strtotime('+58 minutes'),
233+
array('accuracy' => 'hour')
234+
);
235+
$expected = 'in about an hour';
236+
$this->assertEquals($expected, $result);
237+
238+
$result = $this->Time->timeAgoInWords(
239+
strtotime('+23 hours'),
240+
array('accuracy' => 'day')
241+
);
242+
$expected = 'in about a day';
243+
$this->assertEquals($expected, $result);
230244
}
231245

232246
/**
@@ -314,6 +328,18 @@ public function testTimeAgoInWordsNegativeValues() {
314328
array('end' => '2 years')
315329
);
316330
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
331+
332+
$result = $this->Time->timeAgoInWords(
333+
strtotime('-58 minutes'),
334+
array('accuracy' => 'hour')
335+
);
336+
$this->assertEquals('about an hour ago', $result);
337+
338+
$result = $this->Time->timeAgoInWords(
339+
strtotime('-23 hours'),
340+
array('accuracy' => 'day')
341+
);
342+
$this->assertEquals('about a day ago', $result);
317343
}
318344

319345
/**

lib/Cake/Utility/CakeTime.php

+44-16
Original file line numberDiff line numberDiff line change
@@ -820,48 +820,76 @@ public static function timeAgoInWords($dateTime, $options = array()) {
820820
return __d('cake', 'on %s', date($format, $inSeconds));
821821
}
822822

823-
$f = $accuracy['second'];
823+
$fWord = $accuracy['second'];
824824
if ($years > 0) {
825-
$f = $accuracy['year'];
825+
$fWord = $accuracy['year'];
826826
} elseif (abs($months) > 0) {
827-
$f = $accuracy['month'];
827+
$fWord = $accuracy['month'];
828828
} elseif (abs($weeks) > 0) {
829-
$f = $accuracy['week'];
829+
$fWord = $accuracy['week'];
830830
} elseif (abs($days) > 0) {
831-
$f = $accuracy['day'];
831+
$fWord = $accuracy['day'];
832832
} elseif (abs($hours) > 0) {
833-
$f = $accuracy['hour'];
833+
$fWord = $accuracy['hour'];
834834
} elseif (abs($minutes) > 0) {
835-
$f = $accuracy['minute'];
835+
$fWord = $accuracy['minute'];
836836
}
837837

838-
$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
838+
$fNum = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $fWord);
839839

840840
$relativeDate = '';
841-
if ($f >= 1 && $years > 0) {
841+
if ($fNum >= 1 && $years > 0) {
842842
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
843843
}
844-
if ($f >= 2 && $months > 0) {
844+
if ($fNum >= 2 && $months > 0) {
845845
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
846846
}
847-
if ($f >= 3 && $weeks > 0) {
847+
if ($fNum >= 3 && $weeks > 0) {
848848
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
849849
}
850-
if ($f >= 4 && $days > 0) {
850+
if ($fNum >= 4 && $days > 0) {
851851
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
852852
}
853-
if ($f >= 5 && $hours > 0) {
853+
if ($fNum >= 5 && $hours > 0) {
854854
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
855855
}
856-
if ($f >= 6 && $minutes > 0) {
856+
if ($fNum >= 6 && $minutes > 0) {
857857
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
858858
}
859-
if ($f >= 7 && $seconds > 0) {
859+
if ($fNum >= 7 && $seconds > 0) {
860860
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
861861
}
862862

863+
$aboutAgo = array(
864+
'second' => __d('cake', 'about a second ago'),
865+
'minute' => __d('cake', 'about a minute ago'),
866+
'hour' => __d('cake', 'about an hour ago'),
867+
'day' => __d('cake', 'about a day ago'),
868+
'week' => __d('cake', 'about a week ago'),
869+
'year' => __d('cake', 'about a year ago')
870+
);
871+
872+
$aboutIn = array(
873+
'second' => __d('cake', 'in about a second'),
874+
'minute' => __d('cake', 'in about a minute'),
875+
'hour' => __d('cake', 'in about an hour'),
876+
'day' => __d('cake', 'in about a day'),
877+
'week' => __d('cake', 'in about a week'),
878+
'year' => __d('cake', 'in about a year')
879+
);
880+
881+
// When time has passed
863882
if (!$backwards) {
864-
return __d('cake', '%s ago', $relativeDate);
883+
if ($relativeDate) {
884+
return __d('cake', '%s ago', $relativeDate);
885+
}
886+
887+
return $aboutAgo[$fWord];
888+
}
889+
890+
// When time is to come
891+
if (!$relativeDate) {
892+
return $aboutIn[$fWord];
865893
}
866894

867895
return $relativeDate;

0 commit comments

Comments
 (0)