/
Chronos.php
2515 lines (2244 loc) · 71.4 KB
/
Chronos.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
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateInterval;
use DatePeriod;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
/**
* An Immutable extension on the native DateTime object.
*
* Adds a number of convenience APIs methods and the ability
* to easily convert into a mutable object.
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $micro
* @property-read int $microsecond
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read \DateTimeZone $timezone the current timezone
* @property-read \DateTimeZone $tz alias of timezone
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
* @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read int $daysInMonth number of days in the given month
* @property-read int $age does a diffInYears() with default parameters
* @property-read int $quarter the quarter of this instance, 1 - 4
* @property-read int $offset the timezone offset in seconds from UTC
* @property-read int $offsetHours the timezone offset in hours from UTC
* @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
* @property-read bool $local checks if the timezone is local, true if local, false otherwise
* @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
* @psalm-immutable
* @psalm-consistent-constructor
*/
class Chronos
{
use Traits\FormattingTrait;
use Traits\RelativeKeywordTrait;
/**
* @var int
*/
public const MONDAY = 1;
/**
* @var int
*/
public const TUESDAY = 2;
/**
* @var int
*/
public const WEDNESDAY = 3;
/**
* @var int
*/
public const THURSDAY = 4;
/**
* @var int
*/
public const FRIDAY = 5;
/**
* @var int
*/
public const SATURDAY = 6;
/**
* @var int
*/
public const SUNDAY = 7;
/**
* @var int
*/
public const YEARS_PER_CENTURY = 100;
/**
* @var int
*/
public const YEARS_PER_DECADE = 10;
/**
* @var int
*/
public const MONTHS_PER_YEAR = 12;
/**
* @var int
*/
public const MONTHS_PER_QUARTER = 3;
/**
* @var int
*/
public const WEEKS_PER_YEAR = 52;
/**
* @var int
*/
public const DAYS_PER_WEEK = 7;
/**
* @var int
*/
public const HOURS_PER_DAY = 24;
/**
* @var int
*/
public const MINUTES_PER_HOUR = 60;
/**
* @var int
*/
public const SECONDS_PER_MINUTE = 60;
/**
* Default format to use for __toString method when type juggling occurs.
*
* @var string
*/
public const DEFAULT_TO_STRING_FORMAT = 'Y-m-d H:i:s';
/**
* A test Chronos instance to be returned when now instances are created
*
* There is a single test now for all date/time classes provided by Chronos.
* This aims to emulate stubbing out 'now' which is a single global fact.
*
* @var \Cake\Chronos\Chronos|null
*/
protected static ?Chronos $testNow = null;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
/**
* Days of weekend
*
* @var array
*/
protected static array $weekendDays = [Chronos::SATURDAY, Chronos::SUNDAY];
/**
* Names of days of the week.
*
* @var array
*/
protected static array $days = [
Chronos::MONDAY => 'Monday',
Chronos::TUESDAY => 'Tuesday',
Chronos::WEDNESDAY => 'Wednesday',
Chronos::THURSDAY => 'Thursday',
Chronos::FRIDAY => 'Friday',
Chronos::SATURDAY => 'Saturday',
Chronos::SUNDAY => 'Sunday',
];
/**
* First day of week
*
* @var int
*/
protected static int $weekStartsAt = Chronos::MONDAY;
/**
* Last day of week
*
* @var int
*/
protected static int $weekEndsAt = Chronos::SUNDAY;
/**
* Instance of the diff formatting object.
*
* @var \Cake\Chronos\DifferenceFormatterInterface|null
*/
protected static ?DifferenceFormatterInterface $diffFormatter = null;
/**
* @var \DateTimeImmutable
*/
protected DateTimeImmutable $native;
/**
* Create a new Chronos instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param \Cake\Chronos\Chronos|\Cake\Chronos\ChronosDate|\DateTimeInterface|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $timezone The timezone for the instance
*/
public function __construct(
Chronos|ChronosDate|DateTimeInterface|string|int|null $time = 'now',
DateTimeZone|string|null $timezone = null
) {
$this->native = $this->createNative($time, $timezone);
}
/**
* Initializes the PHP DateTimeImmutable object.
*
* @param \Cake\Chronos\Chronos|\Cake\Chronos\ChronosDate|\DateTimeInterface|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $timezone The timezone for the instance
* @return \DateTimeImmutable
*/
protected function createNative(
Chronos|ChronosDate|DateTimeInterface|string|int|null $time,
DateTimeZone|string|null $timezone = null
): DateTimeImmutable {
if (is_int($time) || (is_string($time) && ctype_digit($time))) {
return new DateTimeImmutable("@{$time}");
}
if ($timezone !== null) {
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
}
if (is_object($time)) {
if (!$time instanceof ChronosDate) {
$timezone = $time->getTimezone();
}
$time = $time->format('Y-m-d H:i:s.u');
}
$testNow = static::getTestNow();
if ($testNow === null) {
return new DateTimeImmutable($time ?? 'now', $timezone);
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
return new DateTimeImmutable($time, $timezone);
}
$testNow = clone $testNow;
$relativetime = self::isTimeExpression($time);
if (!$relativetime && $timezone !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($timezone ?? date_default_timezone_get());
}
if ($relative) {
$testNow = $testNow->modify($time);
}
return new DateTimeImmutable($testNow->format('Y-m-d H:i:s.u'), $timezone);
}
/**
* Set a Chronos instance (real or mock) to be returned when a "now"
* instance is created. The provided instance will be returned
* specifically under the following conditions:
* - A call to the static now() method, ex. Chronos::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Chronos('now')
* - When a string containing the desired time is passed to Chronos::parse()
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param \Cake\Chronos\Chronos|string|null $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow(Chronos|string|null $testNow = null): void
{
static::$testNow = is_string($testNow) ? static::parse($testNow) : $testNow;
}
/**
* Get the Chronos instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return \Cake\Chronos\Chronos|null The current instance used for testing
*/
public static function getTestNow(): ?Chronos
{
return static::$testNow;
}
/**
* Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
*
* @return bool True if there is a test instance, otherwise false
*/
public static function hasTestNow(): bool
{
return static::$testNow !== null;
}
/**
* Get weekend days
*
* @return array
*/
public static function getWeekendDays(): array
{
return static::$weekendDays;
}
/**
* Set weekend days
*
* @param array $days Which days are 'weekends'.
* @return void
*/
public static function setWeekendDays(array $days): void
{
static::$weekendDays = $days;
}
/**
* Get the first day of week
*
* @return int
*/
public static function getWeekStartsAt(): int
{
return static::$weekStartsAt;
}
/**
* Set the first day of week
*
* @param int $day The day the week starts with.
* @return void
*/
public static function setWeekStartsAt(int $day): void
{
static::$weekStartsAt = $day;
}
/**
* Get the last day of week
*
* @return int
*/
public static function getWeekEndsAt(): int
{
return static::$weekEndsAt;
}
/**
* Set the last day of week
*
* @param int $day The day the week ends with.
* @return void
*/
public static function setWeekEndsAt(int $day): void
{
static::$weekEndsAt = $day;
}
/**
* Get the difference formatter instance or overwrite the current one.
*
* @param \Cake\Chronos\DifferenceFormatterInterface|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatterInterface The formatter instance.
*/
public static function diffFormatter(?DifferenceFormatterInterface $formatter = null): DifferenceFormatterInterface
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
static::$diffFormatter = new DifferenceFormatter();
}
return static::$diffFormatter;
}
return static::$diffFormatter = $formatter;
}
/**
* Create an instance from a DateTimeInterface
*
* @param \DateTimeInterface $other The datetime instance to convert.
* @return static
*/
public static function instance(DateTimeInterface $other): static
{
return new static($other->format('Y-m-d H:i:s.u'), $other->getTimezone());
}
/**
* Create an instance from a string. This is an alias for the
* constructor that allows better fluent syntax as it allows you to do
* Chronos::parse('Monday next week')->fn() rather than
* (new Chronos('Monday next week'))->fn()
*
* @param \Cake\Chronos\Chronos|\Cake\Chronos\ChronosDate|\DateTimeInterface|string|int|null $time The strtotime compatible string to parse
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name.
* @return static
*/
public static function parse(
Chronos|ChronosDate|DateTimeInterface|string|int|null $time = 'now',
DateTimeZone|string|null $timezone = null
): static {
return new static($time, $timezone);
}
/**
* Get an instance for the current date and time
*
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name.
* @return static
*/
public static function now(DateTimeZone|string|null $timezone = null): static
{
return new static('now', $timezone);
}
/**
* Create an instance for today
*
* @param \DateTimeZone|string|null $timezone The timezone to use.
* @return static
*/
public static function today(DateTimeZone|string|null $timezone = null): static
{
return new static('midnight', $timezone);
}
/**
* Create an instance for tomorrow
*
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function tomorrow(DateTimeZone|string|null $timezone = null): static
{
return new static('tomorrow, midnight', $timezone);
}
/**
* Create an instance for yesterday
*
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function yesterday(DateTimeZone|string|null $timezone = null): static
{
return new static('yesterday, midnight', $timezone);
}
/**
* Create an instance for the greatest supported date.
*
* @return static
*/
public static function maxValue(): static
{
return static::createFromTimestampUTC(PHP_INT_MAX);
}
/**
* Create an instance for the lowest supported date.
*
* @return static
*/
public static function minValue(): static
{
$max = PHP_INT_SIZE === 4 ? PHP_INT_MAX : PHP_INT_MAX / 10;
return static::createFromTimestampUTC(~$max);
}
/**
* Create an instance from a specific date and time.
*
* If any of $year, $month or $day are set to null their now() values
* will be used.
*
* If $hour is null it will be set to its now() value and the default values
* for $minute, $second and $microsecond will be their now() values.
* If $hour is not null then the default values for $minute, $second
* and $microsecond will be 0.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
* @param int|null $day The day to create an instance with.
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param int|null $microsecond The microsecond to create an instance with.
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function create(
?int $year = null,
?int $month = null,
?int $day = null,
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
DateTimeZone|string|null $timezone = null
): static {
$now = static::now();
$year = $year ?? (int)$now->format('Y');
$month = $month ?? $now->format('m');
$day = $day ?? $now->format('d');
if ($hour === null) {
$hour = $now->format('H');
$minute = $minute ?? $now->format('i');
$second = $second ?? $now->format('s');
$microsecond = $microsecond ?? $now->format('u');
} else {
$minute = $minute ?? 0;
$second = $second ?? 0;
$microsecond = $microsecond ?? 0;
}
$instance = static::createFromFormat(
'Y-m-d H:i:s.u',
sprintf('%s-%s-%s %s:%02s:%02s.%06s', 0, $month, $day, $hour, $minute, $second, $microsecond),
$timezone
);
return $instance->addYears($year);
}
/**
* Create an instance from just a date. The time portion is set to now.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
* @param int|null $day The day to create an instance with.
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromDate(
?int $year = null,
?int $month = null,
?int $day = null,
DateTimeZone|string|null $timezone = null
): static {
return static::create($year, $month, $day, null, null, null, null, $timezone);
}
/**
* Create an instance from just a time. The date portion is set to today.
*
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param int|null $microsecond The microsecond to create an instance with.
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTime(
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
DateTimeZone|string|null $timezone = null
): static {
return static::create(null, null, null, $hour, $minute, $second, $microsecond, $timezone);
}
/**
* Create an instance from a specific format
*
* @param string $format The date() compatible format string.
* @param string $time The formatted date string to interpret.
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
* @throws \InvalidArgumentException
*/
public static function createFromFormat(
string $format,
string $time,
DateTimeZone|string|null $timezone = null
): static {
if ($timezone !== null) {
$dateTime = DateTimeImmutable::createFromFormat($format, $time, static::safeCreateDateTimeZone($timezone));
} else {
$dateTime = DateTimeImmutable::createFromFormat($format, $time);
}
$errors = DateTimeImmutable::getLastErrors();
if (!$dateTime) {
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
$dateTime = new static($dateTime->format('Y-m-d H:i:s.u'), $dateTime->getTimezone());
return $dateTime;
}
/**
* Creates an instance from an array of date and time values.
*
* The 'year', 'month' and 'day' values must all be set for a date. The time
* values all default to 0.
*
* The 'timezone' value can be any format supported by `\DateTimeZone`.
*
* Allowed values:
* - year
* - month
* - day
* - hour
* - minute
* - second
* - microsecond
* - meridian ('am' or 'pm')
* - timezone
*
* @param array<int|string> $values Array of date and time values.
* @return static
*/
public static function createFromArray(array $values): static
{
$values += ['hour' => 0, 'minute' => 0, 'second' => 0, 'microsecond' => 0, 'timezone' => null];
$formatted = '';
if (
isset($values['year'], $values['month'], $values['day']) &&
(
is_numeric($values['year']) &&
is_numeric($values['month']) &&
is_numeric($values['day'])
)
) {
$formatted .= sprintf('%04d-%02d-%02d ', $values['year'], $values['month'], $values['day']);
}
if (isset($values['meridian']) && (int)$values['hour'] === 12) {
$values['hour'] = 0;
}
if (isset($values['meridian'])) {
$values['hour'] = strtolower($values['meridian']) === 'am' ? $values['hour'] : $values['hour'] + 12;
}
$formatted .= sprintf(
'%02d:%02d:%02d.%06d',
$values['hour'],
$values['minute'],
$values['second'],
$values['microsecond']
);
return static::parse($formatted, $values['timezone']);
}
/**
* Create an instance from a timestamp
*
* @param int $timestamp The timestamp to create an instance from.
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTimestamp(int $timestamp, DateTimeZone|string|null $timezone = null): static
{
return static::now($timezone)->setTimestamp($timestamp);
}
/**
* Create an instance from an UTC timestamp
*
* @param int $timestamp The UTC timestamp to create an instance from.
* @return static
*/
public static function createFromTimestampUTC(int $timestamp): static
{
return new static($timestamp);
}
/**
* Creates a DateTimeZone from a string or a DateTimeZone
*
* @param \DateTimeZone|string|null $object The value to convert.
* @return \DateTimeZone
* @throws \InvalidArgumentException
*/
protected static function safeCreateDateTimeZone(DateTimeZone|string|null $object): DateTimeZone
{
if ($object === null) {
return new DateTimeZone(date_default_timezone_get());
}
if ($object instanceof DateTimeZone) {
return $object;
}
return new DateTimeZone($object);
}
/**
* Create a new DateInterval instance from specified values.
*
* @param int|null $years The year to use.
* @param int|null $months The month to use.
* @param int|null $weeks The week to use.
* @param int|null $days The day to use.
* @param int|null $hours The hours to use.
* @param int|null $minutes The minutes to use.
* @param int|null $seconds The seconds to use.
* @param int|null $microseconds The microseconds to use.
* @return \DateInterval
*/
public static function createInterval(
?int $years = null,
?int $months = null,
?int $weeks = null,
?int $days = null,
?int $hours = null,
?int $minutes = null,
?int $seconds = null,
?int $microseconds = null,
): DateInterval {
$spec = 'P';
if ($years) {
$spec .= $years . 'Y';
}
if ($months) {
$spec .= $months . 'M';
}
if ($weeks) {
$spec .= $weeks . 'W';
}
if ($days) {
$spec .= $days . 'D';
}
if ($hours || $minutes || $seconds) {
$spec .= 'T';
if ($hours) {
$spec .= $hours . 'H';
}
if ($minutes) {
$spec .= $minutes . 'M';
}
if ($seconds) {
$spec .= $seconds . 'S';
}
}
if ($microseconds && $spec === 'P') {
$spec .= 'T0S';
}
$instance = new DateInterval($spec);
if ($microseconds) {
$instance->f = $microseconds / 1000000;
}
return $instance;
}
/**
* Sets the date and time.
*
* @param int $year The year to set.
* @param int $month The month to set.
* @param int $day The day to set.
* @param int $hour The hour to set.
* @param int $minute The minute to set.
* @param int $second The second to set.
* @return static
*/
public function setDateTime(
int $year,
int $month,
int $day,
int $hour,
int $minute,
int $second = 0
): static {
return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second);
}
/**
* Sets the date.
*
* @param int $year The year to set.
* @param int $month The month to set.
* @param int $day The day to set.
* @return static
*/
public function setDate(int $year, int $month, int $day): static
{
$new = clone $this;
$new->native = $new->native->setDate($year, $month, $day);
return $new;
}
/**
* Sets the time.
*
* @param int $hours Hours of the time
* @param int $minutes Minutes of the time
* @param int $seconds Seconds of the time
* @param int $microseconds Microseconds of the time
* @return static
*/
public function setTime(int $hours, int $minutes, int $seconds = 0, int $microseconds = 0): static
{
$new = clone $this;
$new->native = $new->native->setTime($hours, $minutes, $seconds, $microseconds);
return $new;
}
/**
* Creates a new instance with date modified according to DateTimeImmutable::modifier().
*
* @param string $modifier Date modifier
* @return static
* @throws \InvalidArgumentException
* @see https://www.php.net/manual/en/datetimeimmutable.modify.php
*/
public function modify(string $modifier): static
{
$native = $this->native->modify($modifier);
if ($native === false) {
throw new InvalidArgumentException('Unable to modify date using: ' . $modifier);
}
$new = clone $this;
$new->native = $native;
return $new;
}
/**
* Returns the difference between this instance and target.
*
* @param \Cake\Chronos\Chronos|\DateTimeInterface $target Target instance
* @param bool $absolute Whether the interval is forced to be positive
* @return \DateInterval
*/
public function diff(Chronos|DateTimeInterface $target, bool $absolute = false): DateInterval
{
$target = $target instanceof DateTimeInterface ? $target : $target->native;
return $this->native->diff($target, $absolute);
}
/**
* Returns formatted date string according to DateTimeImmutable::format().
*
* @param string $format String format
* @return string
*/
public function format(string $format): string
{
return $this->native->format($format);
}
/**
* Returns the timezone offset.
*
* @return int
*/
public function getOffset(): int
{
return $this->native->getOffset();
}
/**
* Sets the date and time based on a Unix timestamp.
*
* @param int $timestamp Unix timestamp representing the date
* @return static
*/
public function setTimestamp(int $timestamp): static
{
$new = clone $this;
$new->native = $new->native->setTimestamp($timestamp);
return $new;
}
/**
* Gets the Unix timestamp for this instance.
*
* @return int
*/
public function getTimestamp(): int
{
return $this->native->getTimestamp();
}
/**
* Set the instance's timezone from a string or object
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function setTimezone(DateTimeZone|string $value): static
{
$new = clone $this;
$new->native = $new->native->setTimezone(static::safeCreateDateTimeZone($value));
return $new;
}
/**
* Return time zone set for this instance.
*
* @return \DateTimeZone|false
*/
public function getTimezone(): DateTimeZone|false
{
return $this->native->getTimezone();
}
/**
* Set the time by time string
*
* @param string $time Time as string.
* @return static
*/
public function setTimeFromTimeString(string $time): static
{
$time = explode(':', $time);
$hour = $time[0];
$minute = $time[1] ?? 0;
$second = $time[2] ?? 0;
return $this->setTime((int)$hour, (int)$minute, (int)$second);
}
/**
* Set the instance's timestamp
*
* @param int $value The timestamp value to set.
* @return static
*/
public function timestamp(int $value): static
{
return $this->setTimestamp($value);
}
/**
* Set the instance's year
*
* @param int $value The year value.
* @return static
*/
public function year(int $value): static
{
return $this->setDate($value, $this->month, $this->day);
}
/**
* Set the instance's month
*
* @param int $value The month value.
* @return static
*/
public function month(int $value): static
{
return $this->setDate($this->year, $value, $this->day);
}
/**
* Set the instance's day
*
* @param int $value The day value.
* @return static