From 7449f2565a482227aa87a04d5ed40799a10c6aa1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 9 Mar 2014 21:12:24 +0100 Subject: [PATCH] Make timeUntil behave like timeSince --- library/Icinga/Util/Format.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index 503e78fc8..286492bee 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -96,32 +96,36 @@ class Format return floor($hour / 24) . 'd ' . ($hour % 24) . 'h'; } - public static function timeSince($timestamp) + protected static function smartTimeDiff($diff, $timestamp) { - if ($timestamp === null || $timestamp === false) { + if ($timestamp === null || $timestamp === false) { return '-'; } if (! preg_match('~^\d+$~', $timestamp)) { throw new ProgrammingError(sprintf('"%s" is not a number', $timestamp)); } - $duration = time() - $timestamp; $prefix = ''; - if ($duration < 0) { + if ($diff < 0) { $prefix = '-'; - $duration *= -1; + $diff *= -1; } - if ($duration > 3600 * 24 * 3) { + if ($diff > 3600 * 24 * 3) { if (date('Y') === date('Y', $timestamp)) { return date('d.m.', $timestamp); } return date('m.Y', $timestamp); } - return $prefix . self::showHourMin($duration); + return $prefix . self::showHourMin($diff); + } + + public static function timeSince($timestamp) + { + return self::smartTimeDiff(time() - $timestamp, $timestamp); } public static function timeUntil($timestamp) { - return self::duration($timestamp - time()); + return self::smartTimeDiff($timestamp - time(), $timestamp); } protected static function formatForUnits($value, & $units, $base)