Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
81.82% |
9 / 11 |
CRAP | |
92.59% |
25 / 27 |
DTI | |
0.00% |
0 / 1 |
|
81.82% |
9 / 11 |
14.08 | |
92.59% |
25 / 27 |
add | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
createFromFormat | |
0.00% |
0 / 1 |
3.04 | |
83.33% |
5 / 6 |
|||
createFromMutable | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
modify | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setDate | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setISODate | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setTime | |
0.00% |
0 / 1 |
2.06 | |
75.00% |
3 / 4 |
|||
setTimestamp | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setTimezone | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
sub | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
cloneAsDateTime | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
<?php | |
namespace Puyo\Time; | |
/** | |
* DateTimeImmutableと同等のもの。ImmutableなDateTime。 | |
* | |
* PHP-5.5系のDateTimeImmutableには致命的なバグが多く、 | |
* とくに 5.5.8 よりも古い環境ではそれが顕著。 | |
* | |
* マイナーバージョンが不明な環境でDateTimeImmutable相当のことをするために作成した。 | |
* よってDateTimeImmutableが信頼できる環境下ではそれを使ったほうがよい。 | |
* @author y.kushida | |
* @version 1.0.0 | |
* @uses >= php-5.5 | |
*/ | |
class DTI extends \DateTime | |
{ | |
public function add($interval) { | |
$datetime = $this->cloneAsDateTime()->add($interval); | |
return self::createFromMutable($datetime); | |
} | |
public static function createFromFormat($format, $time, $timezone=null) { | |
if($timezone !== null) { // php-5.5.11ではnullを渡してはならなかった | |
$datetime = parent::createFromFormat($format, $time, $timezone); | |
} else { | |
$datetime = parent::createFromFormat($format, $time); | |
} | |
if($datetime === false) { | |
return false; | |
} else { | |
return self::createFromMutable($datetime); | |
} | |
} | |
public static function createFromMutable(\DateTime $datetime) { | |
return new DTI($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone()); | |
} | |
public function modify($modify) { | |
$datetime = $this->cloneAsDateTime()->modify($modify); | |
return self::createFromMutable($datetime); | |
} | |
public function setDate($year, $month, $day) { | |
$datetime = $this->cloneAsDateTime()->setDate($year, $month, $day); | |
return self::createFromMutable($datetime); | |
} | |
public function setISODate($year, $month, $day=1) { | |
$datetime = $this->cloneAsDateTime()->setISODate($year, $month, $day); | |
return self::createFromMutable($datetime); | |
} | |
public function setTime($hour, $minute, $second=0, $microseconds=0) { | |
if(version_compare(PHP_VERSION, '7.1', '<')) { | |
$datetime = $this->cloneAsDateTime()->setTime($hour, $minute, $second); | |
} else { | |
$datetime = $this->cloneAsDateTime()->setTime($hour, $minute, $second, $microseconds); | |
} | |
return self::createFromMutable($datetime); | |
} | |
public function setTimestamp($unixtimestamp) { | |
$datetime = $this->cloneAsDateTime()->setTimestamp($unixtimestamp); | |
return self::createFromMutable($datetime); | |
} | |
public function setTimezone($timezone) { | |
$datetime = $this->cloneAsDateTime()->setTimezone($timezone); | |
return self::createFromMutable($datetime); | |
} | |
public function sub($interval) { | |
$datetime = $this->cloneAsDateTime()->sub($interval); | |
return self::createFromMutable($datetime); | |
} | |
/** | |
* DateTimeにして返す。オリジナル。 | |
* @return \DateTime | |
*/ | |
public function cloneAsDateTime() { | |
$format = 'Y-m-d H:i:s'; | |
return \DateTime::createFromFormat($format, $this->format($format), $this->getTimezone()); | |
} | |
} |