Skip to content

Commit

Permalink
Replace datetimeFormatDuration() with ago() for relative age
Browse files Browse the repository at this point in the history
  • Loading branch information
user996015 committed Mar 3, 2012
1 parent 3f9ce28 commit 9db1409
Showing 1 changed file with 42 additions and 58 deletions.
100 changes: 42 additions & 58 deletions inc/functions.php
Expand Up @@ -3,70 +3,54 @@
* Functions used by ViewGit. * Functions used by ViewGit.
*/ */


/** function ago($a) {
* Format a unix timestamp. Borrowed from websvn. $b = strtotime('now');
*/ $c = strtotime($a);
// {{{ datetimeFormatDuration $d = $b - $c;
// $minute = 60;
// Formats a duration of seconds for display. $hour = $minute * 60;
// $day = $hour * 24;
// $seconds the number of seconds until something $week = $day * 7;
// $nbsp true if spaces should be replaced by nbsp
// $skipSeconds true if seconds should be omitted if (!ctype_digit($d) || $d < 0) {
// return '';
// return the formatted duration (e.g. @c "8h 6m 1s")

function datetimeFormatDuration($seconds, $nbsp = false, $skipSeconds = false) {
//global $lang;
$lang = array();
$lang['DAYLETTER'] = 'd';
$lang['HOURLETTER'] = 'h';
$lang['MINUTELETTER'] = 'm';
$lang['SECONDLETTER'] = 's';

$neg = false;
if ($seconds < 0) {
$seconds = 0 - $seconds;
$neg = true;
} }


$qty = array(); if ($d < 3) {
$names = array($lang['DAYLETTER'], $lang['HOURLETTER'], $lang['MINUTELETTER']); return 'right now';
}


$qty[] = (int)($seconds / (60 * 60 * 24)); if ($d < $minute) {
$seconds %= 60 * 60 * 24; return floor($d / $e) . ' seconds ago';
}


$qty[] = (int)($seconds / (60 * 60)); if ($d < $minute * 2) {
$seconds %= 60 * 60; return 'about 1 minute ago';
}


$qty[] = (int)($seconds / 60); if ($d < $hour) {
return floor($d / $minute) . ' minutes ago';
}


if (!$skipSeconds) { if ($d < $hour * 2) {
$qty[] = (int)($seconds % 60); return 'about 1 hour ago';
$names[] = $lang['SECONDLETTER'];
} }


$text = $neg ? '-' : ''; if ($d < $day) {
$any = false; return floor($d / $hour) . ' hours ago';
$count = count($names); }
$parts = 0;
for ($i = 0; $i < $count; $i++) { if ($d > $day && $d < $day * 2) {
// If a "higher valued" time slot had a value or this time slot return 'yesterday';
// has a value or this is the very last entry (i.e. all values
// are 0 and we still want to print seconds)
if ($any || $qty[$i] > 0 || $i == $count - 1) {
if ($any) $text .= $nbsp ? '&nbsp;' : ' ';
if ($any && $qty[$i] < 10) $text .= '0';
$text .= $qty[$i].$names[$i];
$any = true;
$parts++;
if ($parts >= 2) break;
}
} }
return $text;
}


// }}} if ($d < $day * 365) {
return floor($d / $day) . ' days ago';
}
else {
return 'over a year ago';
}
}


function debug($msg) function debug($msg)
{ {
Expand Down Expand Up @@ -408,7 +392,7 @@ function git_ls_tree($project, $tree, $path='')
$parts = preg_split('/\s+/', $line, 4); $parts = preg_split('/\s+/', $line, 4);


// Calculate age // Calculate age
$command = 'log -n 1 --format="%ct|%an|%s" ' . (empty($path) ? '' : $path . '/') . $parts[3]; $command = 'log -n 1 --format="%aD|%an|%s" ' . (empty($path) ? '' : $path . '/') . $parts[3];
$out = run_git($project, $command); $out = run_git($project, $command);


$age = ''; $age = '';
Expand All @@ -417,8 +401,8 @@ function git_ls_tree($project, $tree, $path='')


if (isset($out['0'])) { if (isset($out['0'])) {
$explode = explode('|', $out['0']); $explode = explode('|', $out['0']);
$committer_date_unix_timestamp = $explode['0']; $author_date = $explode['0'];
$age = datetimeFormatDuration(time() - $committer_date_unix_timestamp); $age = ago($author_date);
$author = $explode['1']; $author = $explode['1'];
$message = $explode['2']; $message = $explode['2'];
} }
Expand Down Expand Up @@ -526,7 +510,7 @@ function handle_shortlog($project, $hash = 'HEAD', $page = 0)
if (in_array($rev, array_keys($refs_by_hash))) { if (in_array($rev, array_keys($refs_by_hash))) {
$refs = $refs_by_hash[$rev]; $refs = $refs_by_hash[$rev];
} }
$age = datetimeFormatDuration(time() - $info['author_utcstamp']); $age = ago($info['author_utcstamp']);
$result[] = array( $result[] = array(
'author' => $info['author_name'], 'author' => $info['author_name'],
'age' => $age, 'age' => $age,
Expand Down

0 comments on commit 9db1409

Please sign in to comment.