Skip to content

Commit 880857f

Browse files
author
SmetDenis
committed
fixes for formatMS()
1 parent 22340d7 commit 880857f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Timer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ public static function format($time)
6363
*/
6464
public static function formatMS($time)
6565
{
66-
$decimals = 3;
66+
$time = round($time * 1000, 3);
67+
$dec = 3;
6768

68-
if ($time >= 0.001 || $time == 0) {
69-
$decimals = 0;
69+
if (!$time || $time >= 10 || $time >= 100) {
70+
$dec = 0;
71+
} elseif ($time < 10 && $time >= 0.1) {
72+
$dec = 1;
73+
} elseif ($time <= 0.01) {
74+
$dec = 3;
7075
}
7176

72-
$time *= round(1000, 3);
73-
74-
return number_format($time, $decimals, '.', ' ') . ' ms';
77+
return number_format($time, $dec, '.', ' ') . ' ms';
7578
}
7679

7780
/**

tests/TimerTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ public function milliSecondsProvider()
6262
{
6363
return array(
6464
array('1 000 ms', 1),
65-
array('100 ms', 0.1),
65+
array('100 ms', 0.100),
66+
array('106 ms', 0.1056),
6667
array('10 ms', 0.01),
68+
array('15 ms', 0.015),
6769
array('0 ms', 0),
68-
array('1 ms', 0.001),
69-
array('0.100 ms', 0.0001),
70+
array('1.0 ms', 0.001),
71+
array('9.9 ms', 0.0099),
72+
array('10.0 ms', 0.00999),
73+
array('0.6 ms', 0.00055),
7074
array('0.010 ms', 0.00001),
7175
array('0.001 ms', 0.000001),
72-
array('0.000 ms', 0.0000001),
76+
array('0 ms', 0.0000001),
7377
);
7478
}
7579

0 commit comments

Comments
 (0)