-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeOfDay.php
More file actions
277 lines (244 loc) · 7.64 KB
/
TimeOfDay.php
File metadata and controls
277 lines (244 loc) · 7.64 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Icecave\Chrono;
use DateTime as NativeDateTime;
use Icecave\Chrono\Detail\Iso8601;
use Icecave\Chrono\Detail\Normalizer;
use Icecave\Chrono\Format\DefaultFormatter;
use Icecave\Chrono\Format\FormatterInterface;
use Icecave\Parity\Exception\NotComparableException;
use Icecave\Parity\ExtendedComparableTrait;
use Icecave\Parity\SubClassComparable;
class TimeOfDay implements TimeInterface, SubClassComparable
{
use ExtendedComparableTrait;
/**
* @param int $hour The hour component of the time.
* @param int $minute The minute component of the time.
* @param int $second The second component of the time.
* @param TimeZone|null $timeZone The time zone of the time, or null to use UTC.
*/
public function __construct(
$hour = 0,
$minute = 0,
$second = 0,
TimeZone $timeZone = null
) {
Normalizer::normalizeTime($hour, $minute, $second);
if ($timeZone === null) {
$timeZone = new TimeZone();
}
$this->hour = $hour;
$this->minute = $minute;
$this->second = $second;
$this->timeZone = $timeZone;
}
/**
* Standard time formats:
* hh:mm:ss[timezone]
* hhmmss[timezone]
*
* @link http://en.wikipedia.org/wiki/ISO_8601#Times
*
* Note: Formats hh:mm and hhmm for reduced precision are currently not supported.
*
* @param string $isoString A string containing a time in any ISO-8601 compatible time format.
*
* @return TimeOfDay The TimeOfDay constructed from the ISO compatible string and optional time zone.
*/
public static function fromIsoString($isoString)
{
$time = Iso8601::parseTime($isoString);
if ($time['offset'] !== null) {
$timeZone = new TimeZone($time['offset']);
} else {
$timeZone = null;
}
return new self(
$time['hour'],
$time['minute'],
$time['second'],
$timeZone
);
}
/**
* @param int $unixTime The unix timestamp.
* @param TimeZone|null $timeZone The time zone of the time, or null to use UTC.
*
* @return TimeOfDay The TimeOfDay constructed from the given timestamp and time zone.
*/
public static function fromUnixTime($unixTime, TimeZone $timeZone = null)
{
if ($timeZone) {
$unixTime += $timeZone->offset();
}
$parts = gmdate('H,i,s', $unixTime);
$parts = explode(',', $parts);
$parts = array_map('intval', $parts);
list($hour, $minute, $second) = $parts;
return new self($hour, $minute, $second, $timeZone);
}
/**
* @param NativeDateTime $native The native PHP DateTime instance.
*
* @return TimeOfDay The TimeOfDay constructed from the given instance.
*/
public static function fromNativeDateTime(NativeDateTime $native)
{
$unixTime = $native->getTimestamp();
$transitions = $native->getTimezone()->getTransitions($unixTime, $unixTime);
$isDst = $transitions && $transitions[0]['isdst'];
return self::fromUnixTime(
$unixTime,
new TimeZone($native->getTimezone()->getOffset($native), $isDst)
);
}
/**
* @return int The hour component of the time.
*/
public function hour()
{
return $this->hour;
}
/**
* @return int The minute component of the time.
*/
public function minute()
{
return $this->minute;
}
/**
* @return int The second component of the time.
*/
public function second()
{
return $this->second;
}
/**
* Convert this time to a different timezone.
*
* @param TimeZone $timeZone The target timezone
*
* @return TimeOfDay
*/
public function toTimeZone(TimeZone $timeZone)
{
if ($this->timeZone()->isEqualTo($timeZone)) {
return $this;
}
$offset = $timeZone->offset()
- $this->timeZone()->offset();
return new self(
$this->hour(),
$this->minute(),
$this->second() + $offset,
$timeZone
);
}
/**
* Convert this time to the UTC timezone.
*
* @return TimeOfDay
*/
public function toUtc()
{
return $this->toTimeZone(new TimeZone());
}
/**
* @return TimeZone The time zone of the time.
*/
public function timeZone()
{
return $this->timeZone;
}
/**
* Create a {@see DateTime} instance from this time and the given {@see Date} component.
*
* The timezone of the date component will be coverted to the timezone of this time.
*
* @param Date $date The date component.
*
* @return DateTime A {@see DateTime instance} consiting of this time and the given date component.
*/
public function on(Date $date)
{
$date = $date->toTimeZone($this->timeZone());
return new DateTime(
$date->year(),
$date->month(),
$date->day(),
$this->hour(),
$this->minute(),
$this->second(),
$this->timeZone()
);
}
/**
* Compare this object with another value, yielding a result according to the following table:
*
* +--------------------+---------------+
* | Condition | Result |
* +--------------------+---------------+
* | $this == $value | $result === 0 |
* | $this < $value | $result < 0 |
* | $this > $value | $result > 0 |
* +--------------------+---------------+
*
* @param mixed $time The time to compare.
*
* @return int 0 if $this and $time are equal, <0 if $this < $time, or >0 if $this > $time.
* @throws NotComparableException Indicates that the implementation does not know how to compare $this to $time.
*/
public function compare($time): int
{
if (!$time instanceof self) {
throw new NotComparableException($this, $time);
}
return ($this->totalSeconds() - $this->timeZone()->offset())
- ($time->totalSeconds() - $time->timeZone()->offset());
}
/**
* @return int The total number of seconds since 00:00.
*/
public function totalSeconds()
{
return $this->hour() * 3600
+ $this->minute() * 60
+ $this->second();
}
/**
* @param string $formatSpecifier The format of the output string.
* @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
*
* @return string The formatted string.
*/
public function format($formatSpecifier, FormatterInterface $formatter = null)
{
if (null === $formatter) {
$formatter = DefaultFormatter::instance();
}
return $formatter->formatTimeOfDay($this, $formatSpecifier);
}
/**
* @return string A string representing this object in an ISO compatible time format (hh:mm:ss[+-]hh:mm).
*/
public function isoString()
{
return Iso8601::formatTime(
$this->hour(),
$this->minute(),
$this->second(),
$this->timeZone()->isoString()
);
}
/**
* @return string A string representing this object in an ISO compatible time format (hh:mm:ss[+-]hh:mm).
*/
public function __toString()
{
return $this->isoString();
}
private $hour;
private $minute;
private $second;
private $timeZone;
}