diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2014-01-24 04:53:56 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2014-01-24 04:53:56 +0400 |
commit | 89452456103674637c863e38e461946a45a53a41 (patch) | |
tree | 0ec7f9d1d6275a834ca2282bdfd0b86cbeacd375 | |
parent | c65b1d5a64b2fe199b0ce897af258a62cee2551d (diff) |
refs #4540 fix time ago is not correct calculated if numberOfSeconds is negative
-rw-r--r-- | core/MetricsFormatter.php | 15 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Core/PiwikTest.php | 4 |
2 files changed, 18 insertions, 1 deletions
diff --git a/core/MetricsFormatter.php b/core/MetricsFormatter.php index 5fd464ff8d..8f58e52588 100644 --- a/core/MetricsFormatter.php +++ b/core/MetricsFormatter.php @@ -56,6 +56,12 @@ class MetricsFormatter { $numberOfSeconds = $round ? (int)$numberOfSeconds : (float)$numberOfSeconds; + $isNegative = false; + if ($numberOfSeconds < 0) { + $numberOfSeconds = -1 * $numberOfSeconds; + $isNegative = true; + } + // Display 01:45:17 time format if ($displayTimeAsSentence === false) { $hours = floor($numberOfSeconds / 3600); @@ -66,9 +72,13 @@ class MetricsFormatter if ($centiSeconds) { $time .= '.' . sprintf("%02s", $centiSeconds); } + if ($isNegative) { + $time = '-' . $time; + } return $time; } $secondsInYear = 86400 * 365.25; + $years = floor($numberOfSeconds / $secondsInYear); $minusYears = $numberOfSeconds - $years * $secondsInYear; $days = floor($minusYears / 86400); @@ -94,6 +104,11 @@ class MetricsFormatter } else { $return = sprintf(Piwik::translate('General_Seconds'), $seconds); } + + if ($isNegative) { + $return = '-' . $return; + } + if ($isHtml) { return str_replace(' ', ' ', $return); } diff --git a/tests/PHPUnit/Integration/Core/PiwikTest.php b/tests/PHPUnit/Integration/Core/PiwikTest.php index 1f289fde94..1c49070681 100644 --- a/tests/PHPUnit/Integration/Core/PiwikTest.php +++ b/tests/PHPUnit/Integration/Core/PiwikTest.php @@ -149,7 +149,9 @@ class Core_PiwikTest extends DatabaseTestCase array(1.002, array('1s', '00:00:01')), array(1.02, array('1.02s', '00:00:01.02')), array(1.2, array('1.2s', '00:00:01.20')), - array(122.1, array('2 min 2.1s', '00:02:02.10')) + array(122.1, array('2 min 2.1s', '00:02:02.10')), + array(-122.1, array('-2 min 2.1s', '-00:02:02.10')), + array(86400 * -365, array('-365 days 0 hours', '-8760:00:00')) ); } |