/
TimeUnit.php
202 lines (181 loc) · 5.4 KB
/
TimeUnit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
* Copyright (c) 2012 Janos Szurovecz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace precore\util\concurrent;
use DateInterval;
use precore\lang\Enum;
use precore\lang\IllegalStateException;
use precore\util\Preconditions;
class TimeUnit extends Enum
{
const C0 = 1;
const C1 = 1000;
const C2 = 1000000;
const C3 = 60000000;
const C4 = 3600000000;
const C5 = 86400000000;
/**
* Does not support toDateInterval()
*
* @var TimeUnit
*/
public static $MICROSECONDS;
/**
* Does not support toDateInterval()
*
* @var TimeUnit
*/
public static $MILLISECONDS;
/**
* @var TimeUnit
*/
public static $SECONDS;
/**
* @var TimeUnit
*/
public static $MINUTES;
/**
* @var TimeUnit
*/
public static $HOURS;
/**
* @var TimeUnit
*/
public static $DAYS;
private $inMicros;
private $dateIntervalFormat;
protected static function constructorArgs()
{
return [
'MICROSECONDS' => [self::C0, null],
'MILLISECONDS' => [self::C1, null],
'SECONDS' => [self::C2, 'PT%dS'],
'MINUTES' => [self::C3, 'PT%dM'],
'HOURS' => [self::C4, 'PT%dH'],
'DAYS' => [self::C5, 'P%dD']
];
}
/**
* @param $inMicros float The amount of microseconds of this unit.
* @param $dateIntervalFormat string the format character for DateInterval
*/
protected function __construct($inMicros, $dateIntervalFormat)
{
$this->inMicros = $inMicros;
$this->dateIntervalFormat = $dateIntervalFormat;
}
/**
* Equivalent to TimeUnit::$MICROSECONDS->convert($duration, $this).
*
* @param $duration float
* @return float
*/
public function toMicros($duration)
{
return TimeUnit::$MICROSECONDS->convert($duration, $this);
}
/**
* Equivalent to TimeUnit::$MILLISECONDS->convert($duration, $this).
*
* @param $duration float
* @return float
*/
public function toMillis($duration)
{
return TimeUnit::$MILLISECONDS->convert($duration, $this);
}
/**
* Equivalent to TimeUnit::$SECONDS->convert(duration, $this).
*
* @param $duration float
* @return float
*/
public function toSeconds($duration)
{
return TimeUnit::$SECONDS->convert($duration, $this);
}
/**
* Equivalent to TimeUnit::$MINUTES->convert(duration, $this).
*
* @param $duration float
* @return float
*/
public function toMinutes($duration)
{
return TimeUnit::$MINUTES->convert($duration, $this);
}
/**
* Equivalent to TimeUnit::$HOURS->convert(duration, $this).
*
* @param $duration float
* @return float
*/
public function toHours($duration)
{
return TimeUnit::$HOURS->convert($duration, $this);
}
/**
* Equivalent to TimeUnit::$DAYS->convert($duration, $this).
*
* @param $duration float
* @return float
*/
public function toDays($duration)
{
return TimeUnit::$DAYS->convert($duration, $this);
}
/**
* Creates a DateInterval with the given duration and this unit.
*
* @param $duration int
* @throws IllegalStateException if this object does not have proper DateInterval format string
* @return DateInterval
*/
public function toDateInterval($duration)
{
Preconditions::checkState($this->dateIntervalFormat !== null, '[%s] does not support toDateInterval()', $this);
return new DateInterval(sprintf($this->dateIntervalFormat, $duration));
}
/**
* Convert the given time duration in the given unit to this unit.
*
* For example, to convert 10 minutes to milliseconds, use: TimeUnit::$MILLISECONDS->convert(10, TimeUnit::$MINUTES)
*
* @param $duration float The time duration in the given sourceUnit
* @param TimeUnit $timeUnit the unit of the sourceDuration argument
* @return float the converted duration in this unit
*/
public function convert($duration, TimeUnit $timeUnit)
{
return $duration * ($timeUnit->inMicros / $this->inMicros);
}
/**
* Sleeps the process using this TimeUnit.
*
* @param $duration
*/
public function sleep($duration)
{
usleep($this->toMicros($duration));
}
}
TimeUnit::init();