4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Counter;
13 use Behat\Testwork\Counter\Exception\TimerException;
16 * Provides time counting functionality.
18 * @author Konstantin Kudryashov <ever.zet@gmail.com>
34 public function start()
36 $this->starTime = microtime(true);
42 * @throws TimerException If timer has not been started
44 public function stop()
46 if (!$this->starTime) {
47 throw new TimerException('You can not stop timer that has not been started.');
50 $this->stopTime = microtime(true);
56 * @throws TimerException If timer has not been started
58 public function getTime()
60 if (!$this->starTime) {
61 throw new TimerException('You can not get time from timer that never been started.');
64 $stopTime = $this->stopTime;
65 if (!$this->stopTime) {
66 $stopTime = microtime(true);
69 return $stopTime - $this->starTime;
73 * Returns number of minutes passed.
77 public function getMinutes()
79 return intval(floor($this->getTime() / 60));
83 * Returns number of seconds passed.
87 public function getSeconds()
89 return round($this->getTime() - ($this->getMinutes() * 60), 3);
93 * Returns string representation of time passed.
97 public function __toString()
99 if (!$this->starTime || !$this->stopTime) {
103 return sprintf('%dm%.2fs', $this->getMinutes(), $this->getSeconds());