From b3b88ea646af31ee94d1589fd12ab086bc0fc7b6 Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Tue, 4 Sep 2012 09:02:09 +0000 Subject: Code reorg. - CSS move in style.css - configuration values move in conf.php - function definitions move in lib.php - a few code style fixes - test_index.php to test these changes before pushing to prod file --- lib.php | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 lib.php (limited to 'lib.php') diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..5feb424 --- /dev/null +++ b/lib.php @@ -0,0 +1,136 @@ + 'rejected', + 'upload' => 'uploaded', + 'failure' => 'failure', + 'done' => 'partial', + 'build' => 'building', + 'todo' => 'todo' + ); + + foreach ($labels as $k => $v) { + if (array_key_exists($k, $pkg['status'])) { + return $v; + } + } + + return 'unknown'; +} + +/** + * @param integer $num + * + * @return string +*/ +function plural($num) +{ + if ($num > 1) + return "s"; +} + +/** + * Return timestamp from package key + * + * @param string $key package submission key + * + * @return integer +*/ + +function key2timestamp($key) { + global $tz; + + $date = DateTime::createFromFormat("YmdHis", $key+0, $tz); + if ($date <= 0) + return null; + + return $date->getTimestamp(); +} + +/** + * Return human-readable time difference + * + * @param integer $start timestamp + * @param integer $end timestamp, defaults to now + * + * @return string +*/ +function timediff($start, $end) +{ + if (is_null($end)) { + $end = time(); + } + $diff = $end - $start; + if ($diff < 60) { + return $diff . " second" . plural($diff); + } + $diff = round($diff/60); + if ($diff < 60) { + return $diff . " minute" . plural($diff); + } + $diff = round($diff/60); + if ($diff < 24) { + return $diff . " hour" . plural($diff); + } + $diff = round($diff/24); + + return $diff . " day" . plural($diff); +} + + +/** + * Compare two duration strings + * + * @param string $a "1 hour" or "23 mins" + * @param string $b + * + * @return integer +*/ +function timesort($a, $b) +{ + $a = explode(' ', trim($a)); + $b = explode(' ', trim($b)); + + if ($a[1] == 'hour' || $a[1] == 'hours') { + $a[0] *= 3600; + } + + if ($b[1] == 'hour' || $a[1] == 'hours') { + $b[0] *= 3600; + } + + if ($a[0] > $b[0]) { + return 1; + } elseif ($a[0] < $b[0]) { + return -1; + } else { + return 0; + } +} -- cgit v1.2.1