-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathLocalDateTime.php
More file actions
803 lines (679 loc) · 21.9 KB
/
LocalDateTime.php
File metadata and controls
803 lines (679 loc) · 21.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
<?php
declare(strict_types=1);
namespace Brick\DateTime;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\Parser\DateTimeParser;
use Brick\DateTime\Parser\DateTimeParseResult;
use Brick\DateTime\Parser\IsoParsers;
use Brick\DateTime\Utility\Math;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use JsonSerializable;
use Stringable;
use function intdiv;
/**
* A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
*
* This class is immutable.
*/
final class LocalDateTime implements JsonSerializable, Stringable
{
public function __construct(
private readonly LocalDate $date,
private readonly LocalTime $time,
) {
}
/**
* @param int $year The year, from MIN_YEAR to MAX_YEAR.
* @param int|Month $month The month-of-year, from 1 (January) to 12 (December).
* @param int $day The day-of-month, from 1 to 31.
* @param int $hour The hour-of-day, from 0 to 23.
* @param int $minute The minute-of-hour, from 0 to 59.
* @param int $second The second-of-minute, from 0 to 59.
* @param int $nano The nano-of-second, from 0 to 999,999,999.
*
* @throws DateTimeException If the date or time is not valid.
*/
public static function of(int $year, int|Month $month, int $day, int $hour = 0, int $minute = 0, int $second = 0, int $nano = 0): LocalDateTime
{
$date = LocalDate::of($year, $month, $day);
$time = LocalTime::of($hour, $minute, $second, $nano);
return new LocalDateTime($date, $time);
}
/**
* Returns the current local date-time in the given time-zone, according to the given clock.
*
* If no clock is provided, the system clock is used.
*/
public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalDateTime
{
return ZonedDateTime::now($timeZone, $clock)->getDateTime();
}
/**
* @throws DateTimeException If the date-time is not valid.
* @throws DateTimeParseException If required fields are missing from the result.
*/
public static function from(DateTimeParseResult $result): LocalDateTime
{
return new LocalDateTime(
LocalDate::from($result),
LocalTime::from($result),
);
}
/**
* Obtains an instance of `LocalDateTime` from a text string.
*
* @param string $text The text to parse, such as `2007-12-03T10:15:30`.
* @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser.
*
* @throws DateTimeException If the date-time is not valid.
* @throws DateTimeParseException If the text string does not follow the expected format.
*/
public static function parse(string $text, ?DateTimeParser $parser = null): LocalDateTime
{
if ($parser === null) {
$parser = IsoParsers::localDateTime();
}
return LocalDateTime::from($parser->parse($text));
}
/**
* Creates a LocalDateTime from a native DateTime or DateTimeImmutable object.
*/
public static function fromNativeDateTime(DateTimeInterface $dateTime): LocalDateTime
{
return new LocalDateTime(
LocalDate::fromNativeDateTime($dateTime),
LocalTime::fromNativeDateTime($dateTime),
);
}
/**
* Returns the smallest possible value for LocalDateTime.
*/
public static function min(): LocalDateTime
{
/** @var LocalDateTime|null $min */
static $min = null;
return $min ??= new LocalDateTime(LocalDate::min(), LocalTime::min());
}
/**
* Returns the highest possible value for LocalDateTime.
*/
public static function max(): LocalDateTime
{
/** @var LocalDateTime|null $max */
static $max = null;
return $max ??= new LocalDateTime(LocalDate::max(), LocalTime::max());
}
/**
* Returns the smallest LocalDateTime among the given values.
*
* @param LocalDateTime ...$times The LocalDateTime objects to compare.
*
* @return LocalDateTime The earliest LocalDateTime object.
*
* @throws DateTimeException If the array is empty.
*/
public static function minOf(LocalDateTime ...$times): LocalDateTime
{
if ($times === []) {
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}
$min = null;
foreach ($times as $time) {
if ($min === null || $time->isBefore($min)) {
$min = $time;
}
}
return $min;
}
/**
* Returns the highest LocalDateTime among the given values.
*
* @param LocalDateTime ...$times The LocalDateTime objects to compare.
*
* @return LocalDateTime The latest LocalDateTime object.
*
* @throws DateTimeException If the array is empty.
*/
public static function maxOf(LocalDateTime ...$times): LocalDateTime
{
if ($times === []) {
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}
$max = null;
foreach ($times as $time) {
if ($max === null || $time->isAfter($max)) {
$max = $time;
}
}
return $max;
}
public function getDate(): LocalDate
{
return $this->date;
}
public function getTime(): LocalTime
{
return $this->time;
}
public function getYear(): int
{
return $this->date->getYear();
}
/**
* Returns the month-of-year as a Month enum.
*/
public function getMonth(): Month
{
return $this->date->getMonth();
}
/**
* Returns the month-of-year value from 1 to 12.
*
* @return int<1, 12>
*/
public function getMonthValue(): int
{
return $this->date->getMonthValue();
}
/**
* @return int<1, 31>
*/
public function getDayOfMonth(): int
{
return $this->date->getDayOfMonth();
}
public function getDayOfWeek(): DayOfWeek
{
return $this->date->getDayOfWeek();
}
/**
* @return int<1, 366>
*/
public function getDayOfYear(): int
{
return $this->date->getDayOfYear();
}
public function getHour(): int
{
return $this->time->getHour();
}
public function getMinute(): int
{
return $this->time->getMinute();
}
public function getSecond(): int
{
return $this->time->getSecond();
}
public function getNano(): int
{
return $this->time->getNano();
}
/**
* Returns a copy of this LocalDateTime with the date altered.
*/
public function withDate(LocalDate $date): LocalDateTime
{
if ($date->isEqualTo($this->date)) {
return $this;
}
return new LocalDateTime($date, $this->time);
}
/**
* Returns a copy of this LocalDateTime with the time altered.
*/
public function withTime(LocalTime $time): LocalDateTime
{
if ($time->isEqualTo($this->time)) {
return $this;
}
return new LocalDateTime($this->date, $time);
}
/**
* Returns a copy of this LocalDateTime with the year altered.
*
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
*
* @throws DateTimeException If the year is outside the valid range.
*/
public function withYear(int $year): LocalDateTime
{
$date = $this->date->withYear($year);
if ($date === $this->date) {
return $this;
}
return new LocalDateTime($date, $this->time);
}
/**
* Returns a copy of this LocalDateTime with the month-of-year altered.
*
* If the day-of-month is invalid for the month and year, it will be changed to the last valid day of the month.
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int|Month $month): LocalDateTime
{
$date = $this->date->withMonth($month);
if ($date === $this->date) {
return $this;
}
return new LocalDateTime($date, $this->time);
}
/**
* Returns a copy of this LocalDateTime with the day-of-month altered.
*
* If the resulting date is invalid, an exception is thrown.
*
* @throws DateTimeException If the day is invalid for the current year and month.
*/
public function withDay(int $day): LocalDateTime
{
$date = $this->date->withDay($day);
if ($date === $this->date) {
return $this;
}
return new LocalDateTime($date, $this->time);
}
/**
* Returns a copy of this LocalDateTime with the hour-of-day altered.
*
* @throws DateTimeException If the hour is invalid.
*/
public function withHour(int $hour): LocalDateTime
{
$time = $this->time->withHour($hour);
if ($time === $this->time) {
return $this;
}
return new LocalDateTime($this->date, $time);
}
/**
* Returns a copy of this LocalDateTime with the minute-of-hour altered.
*
* @throws DateTimeException If the minute-of-hour if not valid.
*/
public function withMinute(int $minute): LocalDateTime
{
$time = $this->time->withMinute($minute);
if ($time === $this->time) {
return $this;
}
return new LocalDateTime($this->date, $time);
}
/**
* Returns a copy of this LocalDateTime with the second-of-minute altered.
*
* @throws DateTimeException If the second-of-minute if not valid.
*/
public function withSecond(int $second): LocalDateTime
{
$time = $this->time->withSecond($second);
if ($time === $this->time) {
return $this;
}
return new LocalDateTime($this->date, $time);
}
/**
* Returns a copy of this LocalDateTime with the nano-of-second altered.
*
* @throws DateTimeException If the nano-of-second if not valid.
*/
public function withNano(int $nano): LocalDateTime
{
$time = $this->time->withNano($nano);
if ($time === $this->time) {
return $this;
}
return new LocalDateTime($this->date, $time);
}
/**
* Returns a zoned date-time formed from this date-time and the specified time-zone.
*
* @param TimeZone $zone The zime-zone to use.
*
* @return ZonedDateTime The zoned date-time formed from this date-time.
*/
public function atTimeZone(TimeZone $zone): ZonedDateTime
{
return ZonedDateTime::of($this, $zone);
}
/**
* Returns a copy of this LocalDateTime with the specified Period added.
*/
public function plusPeriod(Period $period): LocalDateTime
{
$date = $this->date->plusPeriod($period);
if ($date === $this->date) {
return $this;
}
return new LocalDateTime($date, $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specific Duration added.
*/
public function plusDuration(Duration $duration): LocalDateTime
{
if ($duration->isZero()) {
return $this;
}
$days = Math::floorDiv($this->time->toSecondOfDay() + $duration->getSeconds(), LocalTime::SECONDS_PER_DAY);
return new LocalDateTime($this->date->plusDays($days), $this->time->plusDuration($duration));
}
/**
* Returns a copy of this LocalDateTime with the specified period in years added.
*
* @throws DateTimeException If the resulting year is out of range.
*/
public function plusYears(int $years): LocalDateTime
{
if ($years === 0) {
return $this;
}
return new LocalDateTime($this->date->plusYears($years), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in months added.
*/
public function plusMonths(int $months): LocalDateTime
{
if ($months === 0) {
return $this;
}
return new LocalDateTime($this->date->plusMonths($months), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in weeks added.
*/
public function plusWeeks(int $weeks): LocalDateTime
{
if ($weeks === 0) {
return $this;
}
return new LocalDateTime($this->date->plusWeeks($weeks), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in days added.
*/
public function plusDays(int $days): LocalDateTime
{
if ($days === 0) {
return $this;
}
return new LocalDateTime($this->date->plusDays($days), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in hours added.
*/
public function plusHours(int $hours): LocalDateTime
{
if ($hours === 0) {
return $this;
}
return $this->plusWithOverflow($hours, 0, 0, 0, 1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in minutes added.
*/
public function plusMinutes(int $minutes): LocalDateTime
{
if ($minutes === 0) {
return $this;
}
return $this->plusWithOverflow(0, $minutes, 0, 0, 1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in seconds added.
*/
public function plusSeconds(int $seconds): LocalDateTime
{
if ($seconds === 0) {
return $this;
}
return $this->plusWithOverflow(0, 0, $seconds, 0, 1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in nanoseconds added.
*/
public function plusNanos(int $nanos): LocalDateTime
{
if ($nanos === 0) {
return $this;
}
return $this->plusWithOverflow(0, 0, 0, $nanos, 1);
}
/**
* Returns a copy of this LocalDateTime with the specified Period subtracted.
*/
public function minusPeriod(Period $period): LocalDateTime
{
return $this->plusPeriod($period->negated());
}
/**
* Returns a copy of this LocalDateTime with the specific Duration subtracted.
*/
public function minusDuration(Duration $duration): LocalDateTime
{
return $this->plusDuration($duration->negated());
}
/**
* Returns a copy of this LocalDateTime with the specified period in years subtracted.
*/
public function minusYears(int $years): LocalDateTime
{
if ($years === 0) {
return $this;
}
return new LocalDateTime($this->date->minusYears($years), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in months subtracted.
*/
public function minusMonths(int $months): LocalDateTime
{
if ($months === 0) {
return $this;
}
return new LocalDateTime($this->date->minusMonths($months), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in weeks subtracted.
*/
public function minusWeeks(int $weeks): LocalDateTime
{
if ($weeks === 0) {
return $this;
}
return new LocalDateTime($this->date->minusWeeks($weeks), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in days subtracted.
*/
public function minusDays(int $days): LocalDateTime
{
if ($days === 0) {
return $this;
}
return new LocalDateTime($this->date->minusDays($days), $this->time);
}
/**
* Returns a copy of this LocalDateTime with the specified period in hours subtracted.
*/
public function minusHours(int $hours): LocalDateTime
{
if ($hours === 0) {
return $this;
}
return $this->plusWithOverflow($hours, 0, 0, 0, -1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in minutes subtracted.
*/
public function minusMinutes(int $minutes): LocalDateTime
{
if ($minutes === 0) {
return $this;
}
return $this->plusWithOverflow(0, $minutes, 0, 0, -1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in seconds subtracted.
*/
public function minusSeconds(int $seconds): LocalDateTime
{
if ($seconds === 0) {
return $this;
}
return $this->plusWithOverflow(0, 0, $seconds, 0, -1);
}
/**
* Returns a copy of this LocalDateTime with the specified period in nanoseconds subtracted.
*/
public function minusNanos(int $nanos): LocalDateTime
{
if ($nanos === 0) {
return $this;
}
return $this->plusWithOverflow(0, 0, 0, $nanos, -1);
}
/**
* Compares this date-time to another date-time.
*
* @param LocalDateTime $that The date-time to compare to.
*
* @return int [-1,0,1] If this date-time is before, on, or after the given date-time.
*
* @psalm-return -1|0|1
*/
public function compareTo(LocalDateTime $that): int
{
$cmp = $this->date->compareTo($that->date);
if ($cmp !== 0) {
return $cmp;
}
return $this->time->compareTo($that->time);
}
public function isEqualTo(LocalDateTime $that): bool
{
return $this->compareTo($that) === 0;
}
public function isBefore(LocalDateTime $that): bool
{
return $this->compareTo($that) === -1;
}
public function isBeforeOrEqualTo(LocalDateTime $that): bool
{
return $this->compareTo($that) <= 0;
}
public function isAfter(LocalDateTime $that): bool
{
return $this->compareTo($that) === 1;
}
public function isAfterOrEqualTo(LocalDateTime $that): bool
{
return $this->compareTo($that) >= 0;
}
/**
* Returns whether this LocalDateTime is in the future, in the given time-zone, according to the given clock.
*
* If no clock is provided, the system clock is used.
*/
public function isFuture(TimeZone $timeZone, ?Clock $clock = null): bool
{
return $this->isAfter(LocalDateTime::now($timeZone, $clock));
}
/**
* Returns whether this LocalDateTime is in the past, in the given time-zone, according to the given clock.
*
* If no clock is provided, the system clock is used.
*/
public function isPast(TimeZone $timeZone, ?Clock $clock = null): bool
{
return $this->isBefore(LocalDateTime::now($timeZone, $clock));
}
/**
* Converts this LocalDateTime to a native DateTime object.
*
* The result is a DateTime in the UTC time-zone.
*
* Note that the native DateTime object supports a precision up to the microsecond,
* so the nanoseconds are rounded down to the nearest microsecond.
*/
public function toNativeDateTime(): DateTime
{
return $this->atTimeZone(TimeZoneOffset::utc())->toNativeDateTime();
}
/**
* Converts this LocalDateTime to a native DateTimeImmutable object.
*
* The result is a DateTimeImmutable in the UTC time-zone.
*
* Note that the native DateTimeImmutable object supports a precision up to the microsecond,
* so the nanoseconds are rounded down to the nearest microsecond.
*/
public function toNativeDateTimeImmutable(): DateTimeImmutable
{
return DateTimeImmutable::createFromMutable($this->toNativeDateTime());
}
/**
* Serializes as a string using {@see LocalDateTime::toISOString()}.
*
* @psalm-return non-empty-string
*/
public function jsonSerialize(): string
{
return $this->toISOString();
}
/**
* Returns the ISO 8601 representation of this date time.
*
* @psalm-return non-empty-string
*/
public function toISOString(): string
{
return $this->date . 'T' . $this->time;
}
/**
* {@see LocalDateTime::toISOString()}.
*
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->toISOString();
}
/**
* Returns a copy of this `LocalDateTime` with the specified period added.
*
* @param int $hours The hours to add. May be negative.
* @param int $minutes The minutes to add. May be negative.
* @param int $seconds The seconds to add. May be negative.
* @param int $nanos The nanos to add. May be negative.
* @param int $sign The sign, validated as `1` to add or `-1` to subtract.
*
* @return LocalDateTime The combined result.
*/
private function plusWithOverflow(int $hours, int $minutes, int $seconds, int $nanos, int $sign): LocalDateTime
{
$totDays =
intdiv($hours, LocalTime::HOURS_PER_DAY) +
intdiv($minutes, LocalTime::MINUTES_PER_DAY) +
intdiv($seconds, LocalTime::SECONDS_PER_DAY);
$totDays *= $sign;
$totSeconds =
($seconds % LocalTime::SECONDS_PER_DAY) +
($minutes % LocalTime::MINUTES_PER_DAY) * LocalTime::SECONDS_PER_MINUTE +
($hours % LocalTime::HOURS_PER_DAY) * LocalTime::SECONDS_PER_HOUR;
$curSoD = $this->time->toSecondOfDay();
$totSeconds = $totSeconds * $sign + $curSoD;
$totNanos = $nanos * $sign + $this->time->getNano();
$totSeconds += Math::floorDiv($totNanos, LocalTime::NANOS_PER_SECOND);
$newNano = Math::floorMod($totNanos, LocalTime::NANOS_PER_SECOND);
$totDays += Math::floorDiv($totSeconds, LocalTime::SECONDS_PER_DAY);
$newSoD = Math::floorMod($totSeconds, LocalTime::SECONDS_PER_DAY);
$newDate = $this->date->plusDays($totDays);
$newTime = ($newSoD === $curSoD ? $this->time : LocalTime::ofSecondOfDay($newSoD, $newNano));
return new LocalDateTime($newDate, $newTime);
}
}