Skip to content

Commit

Permalink
Merge pull request #9 from whatthejeff/mocha_time
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 18, 2013
2 parents 5c7f176 + 2e26613 commit b871145
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 51 deletions.
42 changes: 15 additions & 27 deletions PHP/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
*/
class PHP_Timer
{
/**
* @var array
*/
private static $times = array(
'hour' => 3600000,
'minute' => 60000,
'second' => 1000
);

/**
* @var array
*/
Expand Down Expand Up @@ -107,37 +116,16 @@ public function stop()
*/
public function secondsToTimeString($time)
{
$buffer = '';

$time = floor($time * 1000.0) / 1000.0;
$hours = sprintf('%02d', ($time >= 3600) ? floor($time / 3600) : 0);
$minutes = sprintf(
'%02d',
($time >= 60) ? floor($time / 60) - 60 * $hours : 0
);
$seconds = sprintf('%06.3F', $time - 60 * 60 * $hours - 60 * $minutes);
$ms = round($time * 1000);

if ($hours == 0 && $minutes == 0) {
if ($seconds < 1) {
$buffer = ($seconds * 1000) . ' ms';
} else {
$seconds = (float)$seconds;

$buffer .= $seconds . ' second';

if ($seconds != '1') {
$buffer .= 's';
}
foreach (self::$times as $unit => $value) {
if ($ms >= $value) {
$time = floor($ms / $value * 100.0) / 100.0;
return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
}
} else {
if ($hours > 0) {
$buffer = $hours . ':';
}

$buffer .= $minutes . ':' . str_replace('.', ':', $seconds);
}

return $buffer;
return $ms . ' ms';
}

/**
Expand Down
48 changes: 24 additions & 24 deletions Tests/TimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,35 @@ public function secondsProvider()
array('10 ms', .01),
array('100 ms', .1),
array('999 ms', .999),
array('999 ms', .9999),
array('1 second', .9999),
array('1 second', 1),
array('2 seconds', 2),
array('59.9 seconds', 59.9),
array('59.99 seconds', 59.99),
array('59.999 seconds', 59.999),
array('59.999 seconds', 59.9999),
array('59.001 seconds', 59.001),
array('59.99 seconds', 59.999),
array('1 minute', 59.9999),
array('59 seconds', 59.001),
array('59.01 seconds', 59.01),
array('01:00:000', 60),
array('01:01:000', 61),
array('02:00:000', 120),
array('02:01:000', 121),
array('59:59:900', 3599.9),
array('59:59:990', 3599.99),
array('59:59:999', 3599.999),
array('59:59:999', 3599.9999),
array('59:59:001', 3599.001),
array('59:59:010', 3599.01),
array('01:00:00:000', 3600),
array('01:00:01:000', 3601),
array('01:00:01:900', 3601.9),
array('01:00:01:990', 3601.99),
array('01:00:01:999', 3601.999),
array('01:00:01:999', 3601.9999),
array('01:00:59:999', 3659.9999),
array('01:00:59:001', 3659.001),
array('01:00:59:010', 3659.01),
array('01:59:59:999', 7199.9999),
array('1 minute', 60),
array('1.01 minutes', 61),
array('2 minutes', 120),
array('2.01 minutes', 121),
array('59.99 minutes', 3599.9),
array('59.99 minutes', 3599.99),
array('59.99 minutes', 3599.999),
array('1 hour', 3599.9999),
array('59.98 minutes', 3599.001),
array('59.98 minutes', 3599.01),
array('1 hour', 3600),
array('1 hour', 3601),
array('1 hour', 3601.9),
array('1 hour', 3601.99),
array('1 hour', 3601.999),
array('1 hour', 3601.9999),
array('1.01 hours', 3659.9999),
array('1.01 hours', 3659.001),
array('1.01 hours', 3659.01),
array('2 hours', 7199.9999),
);
}
}

0 comments on commit b871145

Please sign in to comment.