Test Setup Failed
Push — master ( aeb8b5...c4a9e3 )
by Alexpts
07:44
created

MicroDate::toDateTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Tools;
5
6
use DateTime;
7
use DateTimeZone;
8
9
class MicroDate
10
{
11
    public int $sec;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
12
    public int $uSec;
13
14
    /**
15
     * @param int $sec
16
     * @param int $uSec
17
     */
18
    public function __construct(int $sec, int $uSec)
19
    {
20 5
        $this->sec = $sec;
21
        $this->uSec = $uSec;
22 5
    }
23 5
24 5
    /**
25
     * @param DateTime $date
26
     *
27
     * @return MicroDate
28
     */
29
    public static function createFromDateTime(DateTime $date): MicroDate
30
    {
31 1
        return new self($date->getTimestamp(), (int)$date->format('u'));
32
    }
33 1
34
    /**
35
     * @return MicroDate
36
     */
37
    public static function now(): MicroDate
38
    {
39 3
        [$uSec, $sec] = explode(' ', microtime());
40
        $uSec = (float)$uSec * 100000;
41 3
        return new self((int)$sec, (int)$uSec);
42 3
    }
43 3
44
    /**
45
     * @param DateTimeZone|null $timeZone
46
     *
47
     * @return DateTime
48
     */
49
    public function toDateTime(DateTimeZone $timeZone = null): DateTime
50
    {
51 2
        $date = DateTime::createFromFormat('U.u', $this->sec . '.' . $this->uSec);
52
        if ($timeZone) {
53 2
            $date->setTimezone($timeZone);
54 2
        }
55 1
56
        return $date;
57
    }
58
}
59