1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Period (https://period.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Period; |
15
|
|
|
|
16
|
|
|
use DateInterval; |
17
|
|
|
use DateTime; |
18
|
|
|
use DateTimeImmutable; |
19
|
|
|
use DateTimeInterface; |
20
|
|
|
use DateTimeZone; |
21
|
|
|
use function filter_var; |
22
|
|
|
use function intdiv; |
23
|
|
|
use const FILTER_VALIDATE_INT; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* League Period Datepoint. |
27
|
|
|
* |
28
|
|
|
* @package League.period |
29
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
30
|
|
|
* @since 4.2.0 |
31
|
|
|
*/ |
32
|
|
|
final class Datepoint extends DateTimeImmutable |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Returns a position in time expressed as a DateTimeImmutable object. |
36
|
|
|
* |
37
|
|
|
* A datepoint can be |
38
|
|
|
* <ul> |
39
|
|
|
* <li>a DateTimeInterface object |
40
|
|
|
* <li>a integer interpreted as a timestamp |
41
|
|
|
* <li>a string parsable by DateTime::__construct |
42
|
|
|
* </ul> |
43
|
|
|
* |
44
|
|
|
* @param mixed $datepoint a position in time |
45
|
|
|
*/ |
46
|
954 |
|
public static function create($datepoint): self |
47
|
|
|
{ |
48
|
954 |
|
if ($datepoint instanceof DateTimeInterface) { |
49
|
462 |
|
return new self($datepoint->format('Y-m-d H:i:s.u'), $datepoint->getTimezone()); |
50
|
|
|
} |
51
|
|
|
|
52
|
519 |
|
if (false !== ($timestamp = filter_var($datepoint, FILTER_VALIDATE_INT))) { |
53
|
15 |
|
return new self('@'.$timestamp); |
54
|
|
|
} |
55
|
|
|
|
56
|
504 |
|
return new self($datepoint); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
* |
62
|
|
|
* @param string $format |
63
|
|
|
* @param string $datetime |
64
|
|
|
* @param DateTimeZone $timezone |
65
|
|
|
* |
66
|
|
|
* @return static|false |
67
|
|
|
*/ |
68
|
3 |
|
public static function createFromFormat($format, $datetime, $timezone = null) |
69
|
|
|
{ |
70
|
3 |
|
$datepoint = parent::createFromFormat($format, $datetime, $timezone); |
71
|
3 |
|
if (false !== $datepoint) { |
72
|
3 |
|
return self::create($datepoint); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $datepoint; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
* |
81
|
|
|
* @param DateTime $datetime |
82
|
|
|
* |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
3 |
|
public static function createFromMutable($datetime): self |
86
|
|
|
{ |
87
|
3 |
|
return self::create(parent::createFromMutable($datetime)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/************************************************** |
91
|
|
|
* interval constructors |
92
|
|
|
**************************************************/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a Period instance. |
96
|
|
|
* |
97
|
|
|
* - the starting datepoint represents the beginning of the current datepoint second |
98
|
|
|
* - the duration is equal to 1 second |
99
|
|
|
*/ |
100
|
6 |
|
public function getSecond(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
101
|
|
|
{ |
102
|
6 |
|
$datepoint = $this->setTime( |
103
|
6 |
|
(int) $this->format('H'), |
104
|
6 |
|
(int) $this->format('i'), |
105
|
6 |
|
(int) $this->format('s') |
106
|
|
|
); |
107
|
|
|
|
108
|
6 |
|
return new Period($datepoint, $datepoint->add(new DateInterval('PT1S')), $boundaryType); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns a Period instance. |
113
|
|
|
* |
114
|
|
|
* - the starting datepoint represents the beginning of the current datepoint minute |
115
|
|
|
* - the duration is equal to 1 minute |
116
|
|
|
*/ |
117
|
6 |
|
public function getMinute(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
118
|
|
|
{ |
119
|
6 |
|
$datepoint = $this->setTime((int) $this->format('H'), (int) $this->format('i'), 0); |
120
|
|
|
|
121
|
6 |
|
return new Period($datepoint, $datepoint->add(new DateInterval('PT1M')), $boundaryType); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a Period instance. |
126
|
|
|
* |
127
|
|
|
* - the starting datepoint represents the beginning of the current datepoint hour |
128
|
|
|
* - the duration is equal to 1 hour |
129
|
|
|
*/ |
130
|
9 |
|
public function getHour(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
131
|
|
|
{ |
132
|
9 |
|
$datepoint = $this->setTime((int) $this->format('H'), 0); |
133
|
|
|
|
134
|
9 |
|
return new Period($datepoint, $datepoint->add(new DateInterval('PT1H')), $boundaryType); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns a Period instance. |
139
|
|
|
* |
140
|
|
|
* - the starting datepoint represents the beginning of the current datepoint day |
141
|
|
|
* - the duration is equal to 1 day |
142
|
|
|
*/ |
143
|
57 |
|
public function getDay(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
144
|
|
|
{ |
145
|
57 |
|
$datepoint = $this->setTime(0, 0); |
146
|
|
|
|
147
|
57 |
|
return new Period($datepoint, $datepoint->add(new DateInterval('P1D')), $boundaryType); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns a Period instance. |
152
|
|
|
* |
153
|
|
|
* - the starting datepoint represents the beginning of the current datepoint iso week |
154
|
|
|
* - the duration is equal to 7 days |
155
|
|
|
*/ |
156
|
6 |
|
public function getIsoWeek(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
157
|
|
|
{ |
158
|
|
|
$startDate = $this |
159
|
6 |
|
->setTime(0, 0) |
160
|
6 |
|
->setISODate((int) $this->format('o'), (int) $this->format('W'), 1); |
161
|
|
|
|
162
|
6 |
|
return new Period($startDate, $startDate->add(new DateInterval('P7D')), $boundaryType); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns a Period instance. |
167
|
|
|
* |
168
|
|
|
* - the starting datepoint represents the beginning of the current datepoint month |
169
|
|
|
* - the duration is equal to 1 month |
170
|
|
|
*/ |
171
|
15 |
|
public function getMonth(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
172
|
|
|
{ |
173
|
|
|
$startDate = $this |
174
|
15 |
|
->setTime(0, 0) |
175
|
15 |
|
->setDate((int) $this->format('Y'), (int) $this->format('n'), 1); |
176
|
|
|
|
177
|
15 |
|
return new Period($startDate, $startDate->add(new DateInterval('P1M')), $boundaryType); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns a Period instance. |
182
|
|
|
* |
183
|
|
|
* - the starting datepoint represents the beginning of the current datepoint quarter |
184
|
|
|
* - the duration is equal to 3 months |
185
|
|
|
*/ |
186
|
6 |
|
public function getQuarter(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
187
|
|
|
{ |
188
|
|
|
$startDate = $this |
189
|
6 |
|
->setTime(0, 0) |
190
|
6 |
|
->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 3) * 3) + 1, 1); |
191
|
|
|
|
192
|
6 |
|
return new Period($startDate, $startDate->add(new DateInterval('P3M')), $boundaryType); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns a Period instance. |
197
|
|
|
* |
198
|
|
|
* - the starting datepoint represents the beginning of the current datepoint semester |
199
|
|
|
* - the duration is equal to 6 months |
200
|
|
|
*/ |
201
|
6 |
|
public function getSemester(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
202
|
|
|
{ |
203
|
|
|
$startDate = $this |
204
|
6 |
|
->setTime(0, 0) |
205
|
6 |
|
->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 6) * 6) + 1, 1); |
206
|
|
|
|
207
|
6 |
|
return new Period($startDate, $startDate->add(new DateInterval('P6M')), $boundaryType); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns a Period instance. |
212
|
|
|
* |
213
|
|
|
* - the starting datepoint represents the beginning of the current datepoint year |
214
|
|
|
* - the duration is equal to 1 year |
215
|
|
|
*/ |
216
|
15 |
|
public function getYear(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
217
|
|
|
{ |
218
|
15 |
|
$year = (int) $this->format('Y'); |
219
|
15 |
|
$datepoint = $this->setTime(0, 0); |
220
|
|
|
|
221
|
15 |
|
return new Period($datepoint->setDate($year, 1, 1), $datepoint->setDate(++$year, 1, 1), $boundaryType); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns a Period instance. |
226
|
|
|
* |
227
|
|
|
* - the starting datepoint represents the beginning of the current datepoint iso year |
228
|
|
|
* - the duration is equal to 1 iso year |
229
|
|
|
*/ |
230
|
6 |
|
public function getIsoYear(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period |
231
|
|
|
{ |
232
|
6 |
|
$year = (int) $this->format('o'); |
233
|
6 |
|
$datepoint = $this->setTime(0, 0); |
234
|
|
|
|
235
|
6 |
|
return new Period($datepoint->setISODate($year, 1, 1), $datepoint->setISODate(++$year, 1, 1), $boundaryType); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/************************************************** |
239
|
|
|
* relation methods |
240
|
|
|
**************************************************/ |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Tells whether the datepoint is before the interval. |
244
|
|
|
*/ |
245
|
12 |
|
public function isBefore(Period $interval): bool |
246
|
|
|
{ |
247
|
12 |
|
return $interval->isAfter($this); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Tell whether the datepoint borders on start the interval. |
252
|
|
|
*/ |
253
|
3 |
|
public function bordersOnStart(Period $interval): bool |
254
|
|
|
{ |
255
|
3 |
|
return $this == $interval->getStartDate() && $interval->isStartExcluded(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Tells whether the datepoint starts the interval. |
260
|
|
|
*/ |
261
|
12 |
|
public function isStarting(Period $interval): bool |
262
|
|
|
{ |
263
|
12 |
|
return $interval->isStartedBy($this); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Tells whether the datepoint is contained within the interval. |
268
|
|
|
*/ |
269
|
27 |
|
public function isDuring(Period $interval): bool |
270
|
|
|
{ |
271
|
27 |
|
return $interval->contains($this); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Tells whether the datepoint ends the interval. |
276
|
|
|
*/ |
277
|
6 |
|
public function isEnding(Period $interval): bool |
278
|
|
|
{ |
279
|
6 |
|
return $interval->isEndedBy($this); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Tells whether the datepoint borders on end the interval. |
284
|
|
|
*/ |
285
|
3 |
|
public function bordersOnEnd(Period $interval): bool |
286
|
|
|
{ |
287
|
3 |
|
return $this == $interval->getEndDate() && $interval->isEndExcluded(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Tells whether the datepoint abuts the interval. |
292
|
|
|
*/ |
293
|
3 |
|
public function abuts(Period $interval): bool |
294
|
|
|
{ |
295
|
3 |
|
return $this->bordersOnEnd($interval) || $this->bordersOnStart($interval); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Tells whether the datepoint is after the interval. |
300
|
|
|
*/ |
301
|
18 |
|
public function isAfter(Period $interval): bool |
302
|
|
|
{ |
303
|
18 |
|
return $interval->isBefore($this); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|