Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
25 / 25 |
MicroSecondTime | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
18 | |
100.00% |
25 / 25 |
__construct | |
100.00% |
1 / 1 |
10 | |
100.00% |
10 / 10 |
|||
setMicrotimeString | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
__toString | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
add | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
subtract | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
toSecond | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
toMillis | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getUsec | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getSec | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
<?php | |
/** | |
* μ秒を上手に扱う | |
*/ | |
namespace Puyo\Time; | |
/** | |
* MicrosecondTime | |
*/ | |
class MicroSecondTime | |
{ | |
/** | |
* Microsecond part. | |
* | |
* 0.12345678 | |
* @var float | |
*/ | |
private $_usec = 0.0; | |
/** | |
* Second part. | |
* | |
* 1234567890 | |
* @var float | |
*/ | |
private $_sec = 0.0; | |
/** | |
* Constructor | |
* | |
* overloadをしているため,複数の引数を取ります。 | |
* <pre> | |
* 1. __construct(float $usec, float $sec) | |
* 2. __construct(string strMicrotime) | |
* 3. __construct(void) | |
* </pre> | |
* | |
* @param mixed $arg1 | |
* @param float $arg2 | |
* @throws \InvalidArgumentException | |
*/ | |
public function __construct($arg1 = null, $arg2 = null) { | |
// Pseudo overload | |
if (isset($arg1) && is_float($arg1) && isset($arg2) && is_float($arg2)) { | |
// 1. __construct(float $usec, float $sec) | |
$this->_usec = $arg1; | |
$this->_sec = $arg2; | |
} elseif (isset($arg1) && is_string($arg1) && is_null($arg2)) { | |
// 2. __construct(string strMicrotime) | |
$this->setMicrotimeString($arg1); | |
} elseif ($arg1 === null && $arg2 === null) { | |
// 3. __construct(void) | |
$this->setMicrotimeString(microtime()); | |
} else { | |
throw new \InvalidArgumentException(); | |
} | |
} | |
/** | |
* 文字列(microtime() の結果)から設定する。 | |
* @param string $strMicrotime | |
*/ | |
protected function setMicrotimeString($strMicrotime) { | |
list($usec, $sec) = explode(" ", $strMicrotime); | |
$this->_usec = (float)$usec; | |
$this->_sec = (float)$sec; | |
} | |
/** | |
* microtime() の書式で出力する | |
* @return string 文字列表現 | |
*/ | |
public function __toString() { | |
return $this->_usec . ' ' . $this->_sec; | |
} | |
/** | |
* 加算して返す | |
* @param self $microTime | |
* @return self | |
*/ | |
public function add(self $microTime) { | |
$newUsec = $this->_usec + $microTime->getUsec(); | |
$newSec = $this->_sec + $microTime->getSec(); | |
return new self($newUsec, $newSec); | |
} | |
/** | |
* 減算して返す | |
* @param self $microTime | |
* @return self | |
*/ | |
public function subtract(self $microTime) { | |
$newUsec = $this->_usec - $microTime->getUsec(); | |
$newSec = $this->_sec - $microTime->getSec(); | |
return new self($newUsec, $newSec); | |
} | |
/** | |
* 秒単位にして返す | |
* @return float | |
*/ | |
public function toSecond() { | |
return $this->_usec + $this->_sec; | |
} | |
/** | |
* ミリ秒単位にして返す | |
* @return float | |
*/ | |
public function toMillis() { | |
return floor($this->_usec * 1000) + $this->_sec * 1000; | |
} | |
/** | |
* マイクロ秒部分を返す | |
* @return float | |
*/ | |
public function getUsec() { | |
return $this->_usec; | |
} | |
/** | |
* 秒部分を返す | |
* @return float | |
*/ | |
public function getSec() { | |
return $this->_sec; | |
} | |
} |