1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace loophp\nanobench\Time; |
11
|
|
|
|
12
|
|
|
final class Duration extends AbstractTime |
13
|
|
|
{ |
14
|
|
|
private TimeInterface $from; |
15
|
|
|
|
16
|
|
|
private TimeInterface $to; |
17
|
|
|
|
18
|
12 |
|
public function __construct(TimeInterface $from, TimeInterface $to, string $unit = TimeUnit::SECOND) |
19
|
|
|
{ |
20
|
12 |
|
$this->from = $from; |
21
|
12 |
|
$this->to = $to; |
22
|
12 |
|
$this->unit = $unit; |
23
|
12 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function as(string $timeUnit): float |
26
|
|
|
{ |
27
|
1 |
|
return $this->diff()->as($timeUnit); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function asDay(): float |
31
|
|
|
{ |
32
|
1 |
|
return $this->diff()->asDay(); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function asHour(): float |
36
|
|
|
{ |
37
|
1 |
|
return $this->diff()->asHour(); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function asMicrosecond(): float |
41
|
|
|
{ |
42
|
1 |
|
return $this->diff()->asMicrosecond(); |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
public function asMillisecond(): float |
46
|
|
|
{ |
47
|
11 |
|
return $this->diff()->asMillisecond(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function asMinute(): float |
51
|
|
|
{ |
52
|
1 |
|
return $this->diff()->asMinute(); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function asNanosecond(): float |
56
|
|
|
{ |
57
|
1 |
|
return $this->diff()->asNanosecond(); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function asSecond(): float |
61
|
|
|
{ |
62
|
2 |
|
return $this->diff()->asSecond(); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function asWeek(): float |
66
|
|
|
{ |
67
|
1 |
|
return $this->diff()->asWeek(); |
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
private function diff(): TimeInterface |
71
|
|
|
{ |
72
|
12 |
|
return new Time(abs($this->from->asSecond() - $this->to->asSecond())); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|