Skip to content
Permalink
Browse files

performance improvements in CakeTime::timeAgoInWords Edit

- rearranges code to move return statements before otherwise dead code
- only do translation if translated string is actually used
  • Loading branch information...
Schlaefer committed Sep 5, 2013
1 parent 6e67489 commit 82da8d9d071c375440437a6913c21e10210d0ca4
Showing with 26 additions and 27 deletions.
  1. +26 −27 lib/Cake/Utility/CakeTime.php
@@ -793,6 +793,14 @@ public static function timeAgoInWords($dateTime, $options = array()) {
}
$diff = $futureTime - $pastTime;
if (!$diff) {
return __d('cake', 'just now', 'just now');
}
if ($diff > abs($now - self::fromString($end))) {
return sprintf($absoluteString, date($format, $inSeconds));
}
// If more than a week, then take into account the length of months
if ($diff >= 604800) {
list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
@@ -855,15 +863,6 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$diff = $diff - ($minutes * 60);
$seconds = $diff;
}
$diff = $futureTime - $pastTime;
if (!$diff) {
return __d('cake', 'just now', 'just now');
}
if ($diff > abs($now - self::fromString($end))) {
return sprintf($absoluteString, date($format, $inSeconds));
}
$fWord = $accuracy['second'];
if ($years > 0) {
@@ -905,34 +904,34 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$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 && $relativeDate) {
return sprintf($relativeString, $relativeDate);
}
if (!$backwards) {
$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')
);
return $aboutAgo[$fWord];
}
// When time is to come
if (!$relativeDate) {
$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')
);
return $aboutIn[$fWord];
}

0 comments on commit 82da8d9

Please sign in to comment.
You can’t perform that action at this time.