Time   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A diff() 0 4 1
A sanitizeFormatString() 0 3 1
A sub() 0 6 1
A format() 0 5 1
A __construct() 0 6 1
A fromDateTimeInterface() 0 3 1
A add() 0 6 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * Copyright (c) DateTime-Contributors
7
 *
8
 * Licensed under the MIT License. See LICENSE.md file in the project root
9
 * for full license information.
10
 */
11
12
namespace DateTime;
13
14
use DateTimeImmutable;
15
use DateTimeInterface;
16
use DateTimeZone;
17
18
class Time
19
{
20
    private $datetime;
21
22
    public function __construct(string $time)
23
    {
24
        $tempdate = new DateTimeImmutable($time);
25
        $this->datetime = new DateTimeImmutable(
26
            '1970-01-01 ' . $tempdate->format('H:i:s.u'),
27
            new DateTimeZone('UTC')
28
        );
29
    }
30
31
    public function add(TimeInterval $interval) : Time
32
    {
33
        $self = clone($this);
34
        $self->datetime = $this->datetime->add($interval->getDateTimeInterval());
35
36
        return $self;
37
    }
38
39
    public function sub(TimeInterval $interval) : Time
40
    {
41
        $self = clone($this);
42
        $self->datetime = $this->datetime->sub($interval->getDateTimeInterval());
43
44
        return $self;
45
    }
46
47
    public function format(string $format) : string
48
    {
49
        $format = $this->sanitizeFormatString($format);
50
51
        return $this->datetime->format($format);
52
    }
53
54
    public function diff(Time $time) : TimeInterval
55
    {
56
        return TimeInterval::fromDateTimeInterval(
57
            $this->datetime->diff($time->datetime)
58
        );
59
    }
60
61
    public static function fromDateTimeInterface(DateTimeInterface $datetime)
62
    {
63
        return new self($datetime->format('H:i:s.u'));
64
    }
65
66
    private function sanitizeFormatString(string $format) : string
67
    {
68
        return preg_replace('/(?<!\\\\)([rceIOPTZUdDjlNSwzWFmMntLoYy])/', '\\\\$1', $format);
69
    }
70
}
71