Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Boundaries.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use Carbon\Exceptions\UnknownUnitException;
17 use Carbon\Unit;
18 use Carbon\WeekDay;
19
20 /**
21 * Trait Boundaries.
22 *
23 * startOf, endOf and derived method for each unit.
24 *
25 * Depends on the following properties:
26 *
27 * @property int $year
28 * @property int $month
29 * @property int $daysInMonth
30 * @property int $quarter
31 *
32 * Depends on the following methods:
33 *
34 * @method $this setTime(int $hour, int $minute, int $second = 0, int $microseconds = 0)
35 * @method $this setDate(int $year, int $month, int $day)
36 * @method $this addMonths(int $value = 1)
37 */
38 trait Boundaries
39 {
40 /**
41 * Resets the time to 00:00:00 start of day
42 *
43 * @example
44 * ```
45 * echo Carbon::parse('2018-07-25 12:45:16')->startOfDay();
46 * ```
47 *
48 * @return static
49 */
50 public function startOfDay()
51 {
52 return $this->setTime(0, 0, 0, 0);
53 }
54
55 /**
56 * Resets the time to 23:59:59.999999 end of day
57 *
58 * @example
59 * ```
60 * echo Carbon::parse('2018-07-25 12:45:16')->endOfDay();
61 * ```
62 *
63 * @return static
64 */
65 public function endOfDay()
66 {
67 return $this->setTime(static::HOURS_PER_DAY - 1, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
68 }
69
70 /**
71 * Resets the date to the first day of the month and the time to 00:00:00
72 *
73 * @example
74 * ```
75 * echo Carbon::parse('2018-07-25 12:45:16')->startOfMonth();
76 * ```
77 *
78 * @return static
79 */
80 public function startOfMonth()
81 {
82 return $this->setDate($this->year, $this->month, 1)->startOfDay();
83 }
84
85 /**
86 * Resets the date to end of the month and time to 23:59:59.999999
87 *
88 * @example
89 * ```
90 * echo Carbon::parse('2018-07-25 12:45:16')->endOfMonth();
91 * ```
92 *
93 * @return static
94 */
95 public function endOfMonth()
96 {
97 return $this->setDate($this->year, $this->month, $this->daysInMonth)->endOfDay();
98 }
99
100 /**
101 * Resets the date to the first day of the quarter and the time to 00:00:00
102 *
103 * @example
104 * ```
105 * echo Carbon::parse('2018-07-25 12:45:16')->startOfQuarter();
106 * ```
107 *
108 * @return static
109 */
110 public function startOfQuarter()
111 {
112 $month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1;
113
114 return $this->setDate($this->year, $month, 1)->startOfDay();
115 }
116
117 /**
118 * Resets the date to end of the quarter and time to 23:59:59.999999
119 *
120 * @example
121 * ```
122 * echo Carbon::parse('2018-07-25 12:45:16')->endOfQuarter();
123 * ```
124 *
125 * @return static
126 */
127 public function endOfQuarter()
128 {
129 return $this->startOfQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endOfMonth();
130 }
131
132 /**
133 * Resets the date to the first day of the year and the time to 00:00:00
134 *
135 * @example
136 * ```
137 * echo Carbon::parse('2018-07-25 12:45:16')->startOfYear();
138 * ```
139 *
140 * @return static
141 */
142 public function startOfYear()
143 {
144 return $this->setDate($this->year, 1, 1)->startOfDay();
145 }
146
147 /**
148 * Resets the date to end of the year and time to 23:59:59.999999
149 *
150 * @example
151 * ```
152 * echo Carbon::parse('2018-07-25 12:45:16')->endOfYear();
153 * ```
154 *
155 * @return static
156 */
157 public function endOfYear()
158 {
159 return $this->setDate($this->year, 12, 31)->endOfDay();
160 }
161
162 /**
163 * Resets the date to the first day of the decade and the time to 00:00:00
164 *
165 * @example
166 * ```
167 * echo Carbon::parse('2018-07-25 12:45:16')->startOfDecade();
168 * ```
169 *
170 * @return static
171 */
172 public function startOfDecade()
173 {
174 $year = $this->year - $this->year % static::YEARS_PER_DECADE;
175
176 return $this->setDate($year, 1, 1)->startOfDay();
177 }
178
179 /**
180 * Resets the date to end of the decade and time to 23:59:59.999999
181 *
182 * @example
183 * ```
184 * echo Carbon::parse('2018-07-25 12:45:16')->endOfDecade();
185 * ```
186 *
187 * @return static
188 */
189 public function endOfDecade()
190 {
191 $year = $this->year - $this->year % static::YEARS_PER_DECADE + static::YEARS_PER_DECADE - 1;
192
193 return $this->setDate($year, 12, 31)->endOfDay();
194 }
195
196 /**
197 * Resets the date to the first day of the century and the time to 00:00:00
198 *
199 * @example
200 * ```
201 * echo Carbon::parse('2018-07-25 12:45:16')->startOfCentury();
202 * ```
203 *
204 * @return static
205 */
206 public function startOfCentury()
207 {
208 $year = $this->year - ($this->year - 1) % static::YEARS_PER_CENTURY;
209
210 return $this->setDate($year, 1, 1)->startOfDay();
211 }
212
213 /**
214 * Resets the date to end of the century and time to 23:59:59.999999
215 *
216 * @example
217 * ```
218 * echo Carbon::parse('2018-07-25 12:45:16')->endOfCentury();
219 * ```
220 *
221 * @return static
222 */
223 public function endOfCentury()
224 {
225 $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_CENTURY + static::YEARS_PER_CENTURY;
226
227 return $this->setDate($year, 12, 31)->endOfDay();
228 }
229
230 /**
231 * Resets the date to the first day of the millennium and the time to 00:00:00
232 *
233 * @example
234 * ```
235 * echo Carbon::parse('2018-07-25 12:45:16')->startOfMillennium();
236 * ```
237 *
238 * @return static
239 */
240 public function startOfMillennium()
241 {
242 $year = $this->year - ($this->year - 1) % static::YEARS_PER_MILLENNIUM;
243
244 return $this->setDate($year, 1, 1)->startOfDay();
245 }
246
247 /**
248 * Resets the date to end of the millennium and time to 23:59:59.999999
249 *
250 * @example
251 * ```
252 * echo Carbon::parse('2018-07-25 12:45:16')->endOfMillennium();
253 * ```
254 *
255 * @return static
256 */
257 public function endOfMillennium()
258 {
259 $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_MILLENNIUM + static::YEARS_PER_MILLENNIUM;
260
261 return $this->setDate($year, 12, 31)->endOfDay();
262 }
263
264 /**
265 * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
266 *
267 * @example
268 * ```
269 * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek() . "\n";
270 * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->startOfWeek() . "\n";
271 * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek(Carbon::SUNDAY) . "\n";
272 * ```
273 *
274 * @param WeekDay|int|null $weekStartsAt optional start allow you to specify the day of week to use to start the week
275 *
276 * @return static
277 */
278 public function startOfWeek(WeekDay|int|null $weekStartsAt = null): static
279 {
280 return $this
281 ->subDays(
282 (static::DAYS_PER_WEEK + $this->dayOfWeek - (WeekDay::int($weekStartsAt) ?? $this->firstWeekDay)) %
283 static::DAYS_PER_WEEK,
284 )
285 ->startOfDay();
286 }
287
288 /**
289 * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59.999999
290 *
291 * @example
292 * ```
293 * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek() . "\n";
294 * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->endOfWeek() . "\n";
295 * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek(Carbon::SATURDAY) . "\n";
296 * ```
297 *
298 * @param WeekDay|int|null $weekEndsAt optional end allow you to specify the day of week to use to end the week
299 *
300 * @return static
301 */
302 public function endOfWeek(WeekDay|int|null $weekEndsAt = null): static
303 {
304 return $this
305 ->addDays(
306 (static::DAYS_PER_WEEK - $this->dayOfWeek + (WeekDay::int($weekEndsAt) ?? $this->lastWeekDay)) %
307 static::DAYS_PER_WEEK,
308 )
309 ->endOfDay();
310 }
311
312 /**
313 * Modify to start of current hour, minutes and seconds become 0
314 *
315 * @example
316 * ```
317 * echo Carbon::parse('2018-07-25 12:45:16')->startOfHour();
318 * ```
319 */
320 public function startOfHour(): static
321 {
322 return $this->setTime($this->hour, 0, 0, 0);
323 }
324
325 /**
326 * Modify to end of current hour, minutes and seconds become 59
327 *
328 * @example
329 * ```
330 * echo Carbon::parse('2018-07-25 12:45:16')->endOfHour();
331 * ```
332 */
333 public function endOfHour(): static
334 {
335 return $this->setTime($this->hour, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
336 }
337
338 /**
339 * Modify to start of current minute, seconds become 0
340 *
341 * @example
342 * ```
343 * echo Carbon::parse('2018-07-25 12:45:16')->startOfMinute();
344 * ```
345 */
346 public function startOfMinute(): static
347 {
348 return $this->setTime($this->hour, $this->minute, 0, 0);
349 }
350
351 /**
352 * Modify to end of current minute, seconds become 59
353 *
354 * @example
355 * ```
356 * echo Carbon::parse('2018-07-25 12:45:16')->endOfMinute();
357 * ```
358 */
359 public function endOfMinute(): static
360 {
361 return $this->setTime($this->hour, $this->minute, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
362 }
363
364 /**
365 * Modify to start of current second, microseconds become 0
366 *
367 * @example
368 * ```
369 * echo Carbon::parse('2018-07-25 12:45:16.334455')
370 * ->startOfSecond()
371 * ->format('H:i:s.u');
372 * ```
373 */
374 public function startOfSecond(): static
375 {
376 return $this->setTime($this->hour, $this->minute, $this->second, 0);
377 }
378
379 /**
380 * Modify to end of current second, microseconds become 999999
381 *
382 * @example
383 * ```
384 * echo Carbon::parse('2018-07-25 12:45:16.334455')
385 * ->endOfSecond()
386 * ->format('H:i:s.u');
387 * ```
388 */
389 public function endOfSecond(): static
390 {
391 return $this->setTime($this->hour, $this->minute, $this->second, static::MICROSECONDS_PER_SECOND - 1);
392 }
393
394 /**
395 * Modify to start of current millisecond, microseconds such as 12345 become 123000
396 *
397 * @example
398 * ```
399 * echo Carbon::parse('2018-07-25 12:45:16.334455')
400 * ->startOfSecond()
401 * ->format('H:i:s.u');
402 * ```
403 */
404 public function startOfMillisecond(): static
405 {
406 $millisecond = (int) floor($this->micro / 1000);
407
408 return $this->setTime($this->hour, $this->minute, $this->second, $millisecond * 1000);
409 }
410
411 /**
412 * Modify to end of current millisecond, microseconds such as 12345 become 123999
413 *
414 * @example
415 * ```
416 * echo Carbon::parse('2018-07-25 12:45:16.334455')
417 * ->endOfSecond()
418 * ->format('H:i:s.u');
419 * ```
420 */
421 public function endOfMillisecond(): static
422 {
423 $millisecond = (int) floor($this->micro / 1000);
424
425 return $this->setTime($this->hour, $this->minute, $this->second, $millisecond * 1000 + 999);
426 }
427
428 /**
429 * Modify to start of current given unit.
430 *
431 * @example
432 * ```
433 * echo Carbon::parse('2018-07-25 12:45:16.334455')
434 * ->startOf(Unit::Month)
435 * ->endOf(Unit::Week, Carbon::FRIDAY);
436 * ```
437 */
438 public function startOf(Unit|string $unit, mixed ...$params): static
439 {
440 $ucfUnit = ucfirst($unit instanceof Unit ? $unit->value : static::singularUnit($unit));
441 $method = "startOf$ucfUnit";
442 if (!method_exists($this, $method)) {
443 throw new UnknownUnitException($unit);
444 }
445
446 return $this->$method(...$params);
447 }
448
449 /**
450 * Modify to end of current given unit.
451 *
452 * @example
453 * ```
454 * echo Carbon::parse('2018-07-25 12:45:16.334455')
455 * ->startOf(Unit::Month)
456 * ->endOf(Unit::Week, Carbon::FRIDAY);
457 * ```
458 */
459 public function endOf(Unit|string $unit, mixed ...$params): static
460 {
461 $ucfUnit = ucfirst($unit instanceof Unit ? $unit->value : static::singularUnit($unit));
462 $method = "endOf$ucfUnit";
463 if (!method_exists($this, $method)) {
464 throw new UnknownUnitException($unit);
465 }
466
467 return $this->$method(...$params);
468 }
469 }