|
1
|
|
|
<?php namespace Comodojo\Extender\Utils; |
|
2
|
|
|
|
|
3
|
|
|
use \DateTime; |
|
4
|
|
|
use \DateInterval; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Comodojo Extender |
|
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* |
|
11
|
|
|
* LICENSE: |
|
12
|
|
|
* |
|
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
19
|
|
|
* THE SOFTWARE. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
class StopWatch { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var DateTime |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $start_time; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var DateTime |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $stop_time; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return StopWatch |
|
36
|
|
|
*/ |
|
37
|
|
|
public function start() { |
|
38
|
|
|
|
|
39
|
|
|
$this->start_time = self::capture(); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return StopWatch |
|
47
|
|
|
*/ |
|
48
|
|
|
public function stop() { |
|
49
|
|
|
|
|
50
|
|
|
$this->stop_time = self::capture(); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return StopWatch |
|
58
|
|
|
*/ |
|
59
|
|
|
public function resume() { |
|
60
|
|
|
|
|
61
|
|
|
$this->stop_time = null; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return DateTime |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getStartTime() { |
|
72
|
|
|
|
|
73
|
|
|
return $this->start_time; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return DateTime |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getStopTime() { |
|
81
|
|
|
|
|
82
|
|
|
return $this->stop_time; |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return DateInterval |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getDrift() { |
|
90
|
|
|
|
|
91
|
|
|
return is_null($this->start_time) || is_null($this->stop_time) ? null : $this->stop_time->diff($this->start_time); |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return StopWatch |
|
97
|
|
|
*/ |
|
98
|
|
|
public function clear() { |
|
99
|
|
|
|
|
100
|
|
|
$this->start_time = null; |
|
101
|
|
|
$this->stop_time = null; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return DateTime |
|
109
|
|
|
*/ |
|
110
|
|
|
final public static function capture() { |
|
111
|
|
|
|
|
112
|
|
|
$t = microtime(true); |
|
113
|
|
|
|
|
114
|
|
|
$micro = sprintf("%06d",($t - floor($t)) * 1000000); |
|
115
|
|
|
|
|
116
|
|
|
return new DateTime( date('Y-m-d H:i:s.'.$micro, $t) ); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|