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
This commit is contained in:
Schlaefer 2013-09-05 12:22:28 +02:00
parent 6e6748955a
commit 82da8d9d07

View file

@ -793,6 +793,14 @@ class CakeTime {
} }
$diff = $futureTime - $pastTime; $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 more than a week, then take into account the length of months
if ($diff >= 604800) { 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)); 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 @@ class CakeTime {
$diff = $diff - ($minutes * 60); $diff = $diff - ($minutes * 60);
$seconds = $diff; $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']; $fWord = $accuracy['second'];
if ($years > 0) { if ($years > 0) {
@ -905,6 +904,11 @@ class CakeTime {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds); $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
} }
// When time has passed
if (!$backwards && $relativeDate) {
return sprintf($relativeString, $relativeDate);
}
if (!$backwards) {
$aboutAgo = array( $aboutAgo = array(
'second' => __d('cake', 'about a second ago'), 'second' => __d('cake', 'about a second ago'),
'minute' => __d('cake', 'about a minute ago'), 'minute' => __d('cake', 'about a minute ago'),
@ -914,6 +918,11 @@ class CakeTime {
'year' => __d('cake', 'about a year ago') 'year' => __d('cake', 'about a year ago')
); );
return $aboutAgo[$fWord];
}
// When time is to come
if (!$relativeDate) {
$aboutIn = array( $aboutIn = array(
'second' => __d('cake', 'in about a second'), 'second' => __d('cake', 'in about a second'),
'minute' => __d('cake', 'in about a minute'), 'minute' => __d('cake', 'in about a minute'),
@ -923,16 +932,6 @@ class CakeTime {
'year' => __d('cake', 'in about a year') 'year' => __d('cake', 'in about a year')
); );
// When time has passed
if (!$backwards && $relativeDate) {
return sprintf($relativeString, $relativeDate);
}
if (!$backwards) {
return $aboutAgo[$fWord];
}
// When time is to come
if (!$relativeDate) {
return $aboutIn[$fWord]; return $aboutIn[$fWord];
} }