Passed
Push — develop ( 30cf64...589229 )
by Guillaume
06:18 queued 04:10
created

Date   F

Complexity

Total Complexity 282

Size/Duplication

Total Lines 2083
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 789
dl 0
loc 2083
rs 1.811
c 0
b 0
f 0
wmc 282

How to fix   Complexity   

Complex Class

Complex classes like Date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Date, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <[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
namespace Carbon\Traits;
12
13
use BadMethodCallException;
14
use Carbon\Carbon;
15
use Carbon\CarbonInterface;
16
use Carbon\CarbonPeriod;
17
use Carbon\CarbonTimeZone;
18
use Carbon\Exceptions\BadComparisonUnitException;
19
use Carbon\Exceptions\ImmutableException;
20
use Carbon\Exceptions\InvalidTimeZoneException;
21
use Carbon\Exceptions\InvalidTypeException;
22
use Carbon\Exceptions\UnknownGetterException;
23
use Carbon\Exceptions\UnknownMethodException;
24
use Carbon\Exceptions\UnknownSetterException;
25
use Carbon\Exceptions\UnknownUnitException;
26
use Closure;
27
use DateInterval;
28
use DatePeriod;
29
use DateTime;
30
use DateTimeInterface;
31
use DateTimeZone;
32
use InvalidArgumentException;
33
use ReflectionException;
34
use Throwable;
35
36
/**
37
 * A simple API extension for DateTime.
38
 *
39
 * <autodoc generated by `composer phpdoc`>
40
 *
41
 * @property      int             $year
42
 * @property      int             $yearIso
43
 * @property      int             $month
44
 * @property      int             $day
45
 * @property      int             $hour
46
 * @property      int             $minute
47
 * @property      int             $second
48
 * @property      int             $micro
49
 * @property      int             $microsecond
50
 * @property      int             $timestamp                                                                         seconds since the Unix Epoch
51
 * @property      string          $englishDayOfWeek                                                                  the day of week in English
52
 * @property      string          $shortEnglishDayOfWeek                                                             the abbreviated day of week in English
53
 * @property      string          $englishMonth                                                                      the month in English
54
 * @property      string          $shortEnglishMonth                                                                 the abbreviated month in English
55
 * @property      string          $localeDayOfWeek                                                                   the day of week in current locale LC_TIME
56
 * @property      string          $shortLocaleDayOfWeek                                                              the abbreviated day of week in current locale LC_TIME
57
 * @property      string          $localeMonth                                                                       the month in current locale LC_TIME
58
 * @property      string          $shortLocaleMonth                                                                  the abbreviated month in current locale LC_TIME
59
 * @property      int             $milliseconds
60
 * @property      int             $millisecond
61
 * @property      int             $milli
62
 * @property      int             $week                                                                              1 through 53
63
 * @property      int             $isoWeek                                                                           1 through 53
64
 * @property      int             $weekYear                                                                          year according to week format
65
 * @property      int             $isoWeekYear                                                                       year according to ISO week format
66
 * @property      int             $dayOfYear                                                                         1 through 366
67
 * @property      int             $age                                                                               does a diffInYears() with default parameters
68
 * @property      int             $offset                                                                            the timezone offset in seconds from UTC
69
 * @property      int             $offsetMinutes                                                                     the timezone offset in minutes from UTC
70
 * @property      int             $offsetHours                                                                       the timezone offset in hours from UTC
71
 * @property      CarbonTimeZone  $timezone                                                                          the current timezone
72
 * @property      CarbonTimeZone  $tz                                                                                alias of $timezone
73
 * @property-read int             $dayOfWeek                                                                         0 (for Sunday) through 6 (for Saturday)
74
 * @property-read int             $dayOfWeekIso                                                                      1 (for Monday) through 7 (for Sunday)
75
 * @property-read int             $weekOfYear                                                                        ISO-8601 week number of year, weeks starting on Monday
76
 * @property-read int             $daysInMonth                                                                       number of days in the given month
77
 * @property-read string          $latinMeridiem                                                                     "am"/"pm" (Ante meridiem or Post meridiem latin lowercase mark)
78
 * @property-read string          $latinUpperMeridiem                                                                "AM"/"PM" (Ante meridiem or Post meridiem latin uppercase mark)
79
 * @property-read string          $timezoneAbbreviatedName                                                           the current timezone abbreviated name
80
 * @property-read string          $tzAbbrName                                                                        alias of $timezoneAbbreviatedName
81
 * @property-read string          $dayName                                                                           long name of weekday translated according to Carbon locale, in english if no translation available for current language
82
 * @property-read string          $shortDayName                                                                      short name of weekday translated according to Carbon locale, in english if no translation available for current language
83
 * @property-read string          $minDayName                                                                        very short name of weekday translated according to Carbon locale, in english if no translation available for current language
84
 * @property-read string          $monthName                                                                         long name of month translated according to Carbon locale, in english if no translation available for current language
85
 * @property-read string          $shortMonthName                                                                    short name of month translated according to Carbon locale, in english if no translation available for current language
86
 * @property-read string          $meridiem                                                                          lowercase meridiem mark translated according to Carbon locale, in latin if no translation available for current language
87
 * @property-read string          $upperMeridiem                                                                     uppercase meridiem mark translated according to Carbon locale, in latin if no translation available for current language
88
 * @property-read int             $noZeroHour                                                                        current hour from 1 to 24
89
 * @property-read int             $weeksInYear                                                                       51 through 53
90
 * @property-read int             $isoWeeksInYear                                                                    51 through 53
91
 * @property-read int             $weekOfMonth                                                                       1 through 5
92
 * @property-read int             $weekNumberInMonth                                                                 1 through 5
93
 * @property-read int             $firstWeekDay                                                                      0 through 6
94
 * @property-read int             $lastWeekDay                                                                       0 through 6
95
 * @property-read int             $daysInYear                                                                        365 or 366
96
 * @property-read int             $quarter                                                                           the quarter of this instance, 1 - 4
97
 * @property-read int             $decade                                                                            the decade of this instance
98
 * @property-read int             $century                                                                           the century of this instance
99
 * @property-read int             $millennium                                                                        the millennium of this instance
100
 * @property-read bool            $dst                                                                               daylight savings time indicator, true if DST, false otherwise
101
 * @property-read bool            $local                                                                             checks if the timezone is local, true if local, false otherwise
102
 * @property-read bool            $utc                                                                               checks if the timezone is UTC, true if UTC, false otherwise
103
 * @property-read string          $timezoneName                                                                      the current timezone name
104
 * @property-read string          $tzName                                                                            alias of $timezoneName
105
 * @property-read string          $locale                                                                            locale of the current instance
106
 *
107
 * @method        bool            isUtc()                                                                            Check if the current instance has UTC timezone. (Both isUtc and isUTC cases are valid.)
108
 * @method        bool            isLocal()                                                                          Check if the current instance has non-UTC timezone.
109
 * @method        bool            isValid()                                                                          Check if the current instance is a valid date.
110
 * @method        bool            isDST()                                                                            Check if the current instance is in a daylight saving time.
111
 * @method        bool            isSunday()                                                                         Checks if the instance day is sunday.
112
 * @method        bool            isMonday()                                                                         Checks if the instance day is monday.
113
 * @method        bool            isTuesday()                                                                        Checks if the instance day is tuesday.
114
 * @method        bool            isWednesday()                                                                      Checks if the instance day is wednesday.
115
 * @method        bool            isThursday()                                                                       Checks if the instance day is thursday.
116
 * @method        bool            isFriday()                                                                         Checks if the instance day is friday.
117
 * @method        bool            isSaturday()                                                                       Checks if the instance day is saturday.
118
 * @method        bool            isSameYear(Carbon|DateTimeInterface|string|null $date = null)                      Checks if the given date is in the same year as the instance. If null passed, compare to now (with the same timezone).
119
 * @method        bool            isCurrentYear()                                                                    Checks if the instance is in the same year as the current moment.
120
 * @method        bool            isNextYear()                                                                       Checks if the instance is in the same year as the current moment next year.
121
 * @method        bool            isLastYear()                                                                       Checks if the instance is in the same year as the current moment last year.
122
 * @method        bool            isSameWeek(Carbon|DateTimeInterface|string|null $date = null)                      Checks if the given date is in the same week as the instance. If null passed, compare to now (with the same timezone).
123
 * @method        bool            isCurrentWeek()                                                                    Checks if the instance is in the same week as the current moment.
124
 * @method        bool            isNextWeek()                                                                       Checks if the instance is in the same week as the current moment next week.
125
 * @method        bool            isLastWeek()                                                                       Checks if the instance is in the same week as the current moment last week.
126
 * @method        bool            isSameDay(Carbon|DateTimeInterface|string|null $date = null)                       Checks if the given date is in the same day as the instance. If null passed, compare to now (with the same timezone).
127
 * @method        bool            isCurrentDay()                                                                     Checks if the instance is in the same day as the current moment.
128
 * @method        bool            isNextDay()                                                                        Checks if the instance is in the same day as the current moment next day.
129
 * @method        bool            isLastDay()                                                                        Checks if the instance is in the same day as the current moment last day.
130
 * @method        bool            isSameHour(Carbon|DateTimeInterface|string|null $date = null)                      Checks if the given date is in the same hour as the instance. If null passed, compare to now (with the same timezone).
131
 * @method        bool            isCurrentHour()                                                                    Checks if the instance is in the same hour as the current moment.
132
 * @method        bool            isNextHour()                                                                       Checks if the instance is in the same hour as the current moment next hour.
133
 * @method        bool            isLastHour()                                                                       Checks if the instance is in the same hour as the current moment last hour.
134
 * @method        bool            isSameMinute(Carbon|DateTimeInterface|string|null $date = null)                    Checks if the given date is in the same minute as the instance. If null passed, compare to now (with the same timezone).
135
 * @method        bool            isCurrentMinute()                                                                  Checks if the instance is in the same minute as the current moment.
136
 * @method        bool            isNextMinute()                                                                     Checks if the instance is in the same minute as the current moment next minute.
137
 * @method        bool            isLastMinute()                                                                     Checks if the instance is in the same minute as the current moment last minute.
138
 * @method        bool            isSameSecond(Carbon|DateTimeInterface|string|null $date = null)                    Checks if the given date is in the same second as the instance. If null passed, compare to now (with the same timezone).
139
 * @method        bool            isCurrentSecond()                                                                  Checks if the instance is in the same second as the current moment.
140
 * @method        bool            isNextSecond()                                                                     Checks if the instance is in the same second as the current moment next second.
141
 * @method        bool            isLastSecond()                                                                     Checks if the instance is in the same second as the current moment last second.
142
 * @method        bool            isSameMicro(Carbon|DateTimeInterface|string|null $date = null)                     Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone).
143
 * @method        bool            isCurrentMicro()                                                                   Checks if the instance is in the same microsecond as the current moment.
144
 * @method        bool            isNextMicro()                                                                      Checks if the instance is in the same microsecond as the current moment next microsecond.
145
 * @method        bool            isLastMicro()                                                                      Checks if the instance is in the same microsecond as the current moment last microsecond.
146
 * @method        bool            isSameMicrosecond(Carbon|DateTimeInterface|string|null $date = null)               Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone).
147
 * @method        bool            isCurrentMicrosecond()                                                             Checks if the instance is in the same microsecond as the current moment.
148
 * @method        bool            isNextMicrosecond()                                                                Checks if the instance is in the same microsecond as the current moment next microsecond.
149
 * @method        bool            isLastMicrosecond()                                                                Checks if the instance is in the same microsecond as the current moment last microsecond.
150
 * @method        bool            isCurrentMonth()                                                                   Checks if the instance is in the same month as the current moment.
151
 * @method        bool            isNextMonth()                                                                      Checks if the instance is in the same month as the current moment next month.
152
 * @method        bool            isLastMonth()                                                                      Checks if the instance is in the same month as the current moment last month.
153
 * @method        bool            isCurrentQuarter()                                                                 Checks if the instance is in the same quarter as the current moment.
154
 * @method        bool            isNextQuarter()                                                                    Checks if the instance is in the same quarter as the current moment next quarter.
155
 * @method        bool            isLastQuarter()                                                                    Checks if the instance is in the same quarter as the current moment last quarter.
156
 * @method        bool            isSameDecade(Carbon|DateTimeInterface|string|null $date = null)                    Checks if the given date is in the same decade as the instance. If null passed, compare to now (with the same timezone).
157
 * @method        bool            isCurrentDecade()                                                                  Checks if the instance is in the same decade as the current moment.
158
 * @method        bool            isNextDecade()                                                                     Checks if the instance is in the same decade as the current moment next decade.
159
 * @method        bool            isLastDecade()                                                                     Checks if the instance is in the same decade as the current moment last decade.
160
 * @method        bool            isSameCentury(Carbon|DateTimeInterface|string|null $date = null)                   Checks if the given date is in the same century as the instance. If null passed, compare to now (with the same timezone).
161
 * @method        bool            isCurrentCentury()                                                                 Checks if the instance is in the same century as the current moment.
162
 * @method        bool            isNextCentury()                                                                    Checks if the instance is in the same century as the current moment next century.
163
 * @method        bool            isLastCentury()                                                                    Checks if the instance is in the same century as the current moment last century.
164
 * @method        bool            isSameMillennium(Carbon|DateTimeInterface|string|null $date = null)                Checks if the given date is in the same millennium as the instance. If null passed, compare to now (with the same timezone).
165
 * @method        bool            isCurrentMillennium()                                                              Checks if the instance is in the same millennium as the current moment.
166
 * @method        bool            isNextMillennium()                                                                 Checks if the instance is in the same millennium as the current moment next millennium.
167
 * @method        bool            isLastMillennium()                                                                 Checks if the instance is in the same millennium as the current moment last millennium.
168
 * @method        CarbonInterface years(int $value)                                                                  Set current instance year to the given value.
169
 * @method        CarbonInterface year(int $value)                                                                   Set current instance year to the given value.
170
 * @method        CarbonInterface setYears(int $value)                                                               Set current instance year to the given value.
171
 * @method        CarbonInterface setYear(int $value)                                                                Set current instance year to the given value.
172
 * @method        CarbonInterface months(int $value)                                                                 Set current instance month to the given value.
173
 * @method        CarbonInterface month(int $value)                                                                  Set current instance month to the given value.
174
 * @method        CarbonInterface setMonths(int $value)                                                              Set current instance month to the given value.
175
 * @method        CarbonInterface setMonth(int $value)                                                               Set current instance month to the given value.
176
 * @method        CarbonInterface days(int $value)                                                                   Set current instance day to the given value.
177
 * @method        CarbonInterface day(int $value)                                                                    Set current instance day to the given value.
178
 * @method        CarbonInterface setDays(int $value)                                                                Set current instance day to the given value.
179
 * @method        CarbonInterface setDay(int $value)                                                                 Set current instance day to the given value.
180
 * @method        CarbonInterface hours(int $value)                                                                  Set current instance hour to the given value.
181
 * @method        CarbonInterface hour(int $value)                                                                   Set current instance hour to the given value.
182
 * @method        CarbonInterface setHours(int $value)                                                               Set current instance hour to the given value.
183
 * @method        CarbonInterface setHour(int $value)                                                                Set current instance hour to the given value.
184
 * @method        CarbonInterface minutes(int $value)                                                                Set current instance minute to the given value.
185
 * @method        CarbonInterface minute(int $value)                                                                 Set current instance minute to the given value.
186
 * @method        CarbonInterface setMinutes(int $value)                                                             Set current instance minute to the given value.
187
 * @method        CarbonInterface setMinute(int $value)                                                              Set current instance minute to the given value.
188
 * @method        CarbonInterface seconds(int $value)                                                                Set current instance second to the given value.
189
 * @method        CarbonInterface second(int $value)                                                                 Set current instance second to the given value.
190
 * @method        CarbonInterface setSeconds(int $value)                                                             Set current instance second to the given value.
191
 * @method        CarbonInterface setSecond(int $value)                                                              Set current instance second to the given value.
192
 * @method        CarbonInterface millis(int $value)                                                                 Set current instance millisecond to the given value.
193
 * @method        CarbonInterface milli(int $value)                                                                  Set current instance millisecond to the given value.
194
 * @method        CarbonInterface setMillis(int $value)                                                              Set current instance millisecond to the given value.
195
 * @method        CarbonInterface setMilli(int $value)                                                               Set current instance millisecond to the given value.
196
 * @method        CarbonInterface milliseconds(int $value)                                                           Set current instance millisecond to the given value.
197
 * @method        CarbonInterface millisecond(int $value)                                                            Set current instance millisecond to the given value.
198
 * @method        CarbonInterface setMilliseconds(int $value)                                                        Set current instance millisecond to the given value.
199
 * @method        CarbonInterface setMillisecond(int $value)                                                         Set current instance millisecond to the given value.
200
 * @method        CarbonInterface micros(int $value)                                                                 Set current instance microsecond to the given value.
201
 * @method        CarbonInterface micro(int $value)                                                                  Set current instance microsecond to the given value.
202
 * @method        CarbonInterface setMicros(int $value)                                                              Set current instance microsecond to the given value.
203
 * @method        CarbonInterface setMicro(int $value)                                                               Set current instance microsecond to the given value.
204
 * @method        CarbonInterface microseconds(int $value)                                                           Set current instance microsecond to the given value.
205
 * @method        CarbonInterface microsecond(int $value)                                                            Set current instance microsecond to the given value.
206
 * @method        CarbonInterface setMicroseconds(int $value)                                                        Set current instance microsecond to the given value.
207
 * @method        CarbonInterface setMicrosecond(int $value)                                                         Set current instance microsecond to the given value.
208
 * @method        CarbonInterface addYears(int $value = 1)                                                           Add years (the $value count passed in) to the instance (using date interval).
209
 * @method        CarbonInterface addYear()                                                                          Add one year to the instance (using date interval).
210
 * @method        CarbonInterface subYears(int $value = 1)                                                           Sub years (the $value count passed in) to the instance (using date interval).
211
 * @method        CarbonInterface subYear()                                                                          Sub one year to the instance (using date interval).
212
 * @method        CarbonInterface addYearsWithOverflow(int $value = 1)                                               Add years (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
213
 * @method        CarbonInterface addYearWithOverflow()                                                              Add one year to the instance (using date interval) with overflow explicitly allowed.
214
 * @method        CarbonInterface subYearsWithOverflow(int $value = 1)                                               Sub years (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
215
 * @method        CarbonInterface subYearWithOverflow()                                                              Sub one year to the instance (using date interval) with overflow explicitly allowed.
216
 * @method        CarbonInterface addYearsWithoutOverflow(int $value = 1)                                            Add years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
217
 * @method        CarbonInterface addYearWithoutOverflow()                                                           Add one year to the instance (using date interval) with overflow explicitly forbidden.
218
 * @method        CarbonInterface subYearsWithoutOverflow(int $value = 1)                                            Sub years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
219
 * @method        CarbonInterface subYearWithoutOverflow()                                                           Sub one year to the instance (using date interval) with overflow explicitly forbidden.
220
 * @method        CarbonInterface addYearsWithNoOverflow(int $value = 1)                                             Add years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
221
 * @method        CarbonInterface addYearWithNoOverflow()                                                            Add one year to the instance (using date interval) with overflow explicitly forbidden.
222
 * @method        CarbonInterface subYearsWithNoOverflow(int $value = 1)                                             Sub years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
223
 * @method        CarbonInterface subYearWithNoOverflow()                                                            Sub one year to the instance (using date interval) with overflow explicitly forbidden.
224
 * @method        CarbonInterface addYearsNoOverflow(int $value = 1)                                                 Add years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
225
 * @method        CarbonInterface addYearNoOverflow()                                                                Add one year to the instance (using date interval) with overflow explicitly forbidden.
226
 * @method        CarbonInterface subYearsNoOverflow(int $value = 1)                                                 Sub years (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
227
 * @method        CarbonInterface subYearNoOverflow()                                                                Sub one year to the instance (using date interval) with overflow explicitly forbidden.
228
 * @method        CarbonInterface addMonths(int $value = 1)                                                          Add months (the $value count passed in) to the instance (using date interval).
229
 * @method        CarbonInterface addMonth()                                                                         Add one month to the instance (using date interval).
230
 * @method        CarbonInterface subMonths(int $value = 1)                                                          Sub months (the $value count passed in) to the instance (using date interval).
231
 * @method        CarbonInterface subMonth()                                                                         Sub one month to the instance (using date interval).
232
 * @method        CarbonInterface addMonthsWithOverflow(int $value = 1)                                              Add months (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
233
 * @method        CarbonInterface addMonthWithOverflow()                                                             Add one month to the instance (using date interval) with overflow explicitly allowed.
234
 * @method        CarbonInterface subMonthsWithOverflow(int $value = 1)                                              Sub months (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
235
 * @method        CarbonInterface subMonthWithOverflow()                                                             Sub one month to the instance (using date interval) with overflow explicitly allowed.
236
 * @method        CarbonInterface addMonthsWithoutOverflow(int $value = 1)                                           Add months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
237
 * @method        CarbonInterface addMonthWithoutOverflow()                                                          Add one month to the instance (using date interval) with overflow explicitly forbidden.
238
 * @method        CarbonInterface subMonthsWithoutOverflow(int $value = 1)                                           Sub months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
239
 * @method        CarbonInterface subMonthWithoutOverflow()                                                          Sub one month to the instance (using date interval) with overflow explicitly forbidden.
240
 * @method        CarbonInterface addMonthsWithNoOverflow(int $value = 1)                                            Add months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
241
 * @method        CarbonInterface addMonthWithNoOverflow()                                                           Add one month to the instance (using date interval) with overflow explicitly forbidden.
242
 * @method        CarbonInterface subMonthsWithNoOverflow(int $value = 1)                                            Sub months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
243
 * @method        CarbonInterface subMonthWithNoOverflow()                                                           Sub one month to the instance (using date interval) with overflow explicitly forbidden.
244
 * @method        CarbonInterface addMonthsNoOverflow(int $value = 1)                                                Add months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
245
 * @method        CarbonInterface addMonthNoOverflow()                                                               Add one month to the instance (using date interval) with overflow explicitly forbidden.
246
 * @method        CarbonInterface subMonthsNoOverflow(int $value = 1)                                                Sub months (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
247
 * @method        CarbonInterface subMonthNoOverflow()                                                               Sub one month to the instance (using date interval) with overflow explicitly forbidden.
248
 * @method        CarbonInterface addDays(int $value = 1)                                                            Add days (the $value count passed in) to the instance (using date interval).
249
 * @method        CarbonInterface addDay()                                                                           Add one day to the instance (using date interval).
250
 * @method        CarbonInterface subDays(int $value = 1)                                                            Sub days (the $value count passed in) to the instance (using date interval).
251
 * @method        CarbonInterface subDay()                                                                           Sub one day to the instance (using date interval).
252
 * @method        CarbonInterface addHours(int $value = 1)                                                           Add hours (the $value count passed in) to the instance (using date interval).
253
 * @method        CarbonInterface addHour()                                                                          Add one hour to the instance (using date interval).
254
 * @method        CarbonInterface subHours(int $value = 1)                                                           Sub hours (the $value count passed in) to the instance (using date interval).
255
 * @method        CarbonInterface subHour()                                                                          Sub one hour to the instance (using date interval).
256
 * @method        CarbonInterface addMinutes(int $value = 1)                                                         Add minutes (the $value count passed in) to the instance (using date interval).
257
 * @method        CarbonInterface addMinute()                                                                        Add one minute to the instance (using date interval).
258
 * @method        CarbonInterface subMinutes(int $value = 1)                                                         Sub minutes (the $value count passed in) to the instance (using date interval).
259
 * @method        CarbonInterface subMinute()                                                                        Sub one minute to the instance (using date interval).
260
 * @method        CarbonInterface addSeconds(int $value = 1)                                                         Add seconds (the $value count passed in) to the instance (using date interval).
261
 * @method        CarbonInterface addSecond()                                                                        Add one second to the instance (using date interval).
262
 * @method        CarbonInterface subSeconds(int $value = 1)                                                         Sub seconds (the $value count passed in) to the instance (using date interval).
263
 * @method        CarbonInterface subSecond()                                                                        Sub one second to the instance (using date interval).
264
 * @method        CarbonInterface addMillis(int $value = 1)                                                          Add milliseconds (the $value count passed in) to the instance (using date interval).
265
 * @method        CarbonInterface addMilli()                                                                         Add one millisecond to the instance (using date interval).
266
 * @method        CarbonInterface subMillis(int $value = 1)                                                          Sub milliseconds (the $value count passed in) to the instance (using date interval).
267
 * @method        CarbonInterface subMilli()                                                                         Sub one millisecond to the instance (using date interval).
268
 * @method        CarbonInterface addMilliseconds(int $value = 1)                                                    Add milliseconds (the $value count passed in) to the instance (using date interval).
269
 * @method        CarbonInterface addMillisecond()                                                                   Add one millisecond to the instance (using date interval).
270
 * @method        CarbonInterface subMilliseconds(int $value = 1)                                                    Sub milliseconds (the $value count passed in) to the instance (using date interval).
271
 * @method        CarbonInterface subMillisecond()                                                                   Sub one millisecond to the instance (using date interval).
272
 * @method        CarbonInterface addMicros(int $value = 1)                                                          Add microseconds (the $value count passed in) to the instance (using date interval).
273
 * @method        CarbonInterface addMicro()                                                                         Add one microsecond to the instance (using date interval).
274
 * @method        CarbonInterface subMicros(int $value = 1)                                                          Sub microseconds (the $value count passed in) to the instance (using date interval).
275
 * @method        CarbonInterface subMicro()                                                                         Sub one microsecond to the instance (using date interval).
276
 * @method        CarbonInterface addMicroseconds(int $value = 1)                                                    Add microseconds (the $value count passed in) to the instance (using date interval).
277
 * @method        CarbonInterface addMicrosecond()                                                                   Add one microsecond to the instance (using date interval).
278
 * @method        CarbonInterface subMicroseconds(int $value = 1)                                                    Sub microseconds (the $value count passed in) to the instance (using date interval).
279
 * @method        CarbonInterface subMicrosecond()                                                                   Sub one microsecond to the instance (using date interval).
280
 * @method        CarbonInterface addMillennia(int $value = 1)                                                       Add millennia (the $value count passed in) to the instance (using date interval).
281
 * @method        CarbonInterface addMillennium()                                                                    Add one millennium to the instance (using date interval).
282
 * @method        CarbonInterface subMillennia(int $value = 1)                                                       Sub millennia (the $value count passed in) to the instance (using date interval).
283
 * @method        CarbonInterface subMillennium()                                                                    Sub one millennium to the instance (using date interval).
284
 * @method        CarbonInterface addMillenniaWithOverflow(int $value = 1)                                           Add millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
285
 * @method        CarbonInterface addMillenniumWithOverflow()                                                        Add one millennium to the instance (using date interval) with overflow explicitly allowed.
286
 * @method        CarbonInterface subMillenniaWithOverflow(int $value = 1)                                           Sub millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
287
 * @method        CarbonInterface subMillenniumWithOverflow()                                                        Sub one millennium to the instance (using date interval) with overflow explicitly allowed.
288
 * @method        CarbonInterface addMillenniaWithoutOverflow(int $value = 1)                                        Add millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
289
 * @method        CarbonInterface addMillenniumWithoutOverflow()                                                     Add one millennium to the instance (using date interval) with overflow explicitly forbidden.
290
 * @method        CarbonInterface subMillenniaWithoutOverflow(int $value = 1)                                        Sub millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
291
 * @method        CarbonInterface subMillenniumWithoutOverflow()                                                     Sub one millennium to the instance (using date interval) with overflow explicitly forbidden.
292
 * @method        CarbonInterface addMillenniaWithNoOverflow(int $value = 1)                                         Add millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
293
 * @method        CarbonInterface addMillenniumWithNoOverflow()                                                      Add one millennium to the instance (using date interval) with overflow explicitly forbidden.
294
 * @method        CarbonInterface subMillenniaWithNoOverflow(int $value = 1)                                         Sub millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
295
 * @method        CarbonInterface subMillenniumWithNoOverflow()                                                      Sub one millennium to the instance (using date interval) with overflow explicitly forbidden.
296
 * @method        CarbonInterface addMillenniaNoOverflow(int $value = 1)                                             Add millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
297
 * @method        CarbonInterface addMillenniumNoOverflow()                                                          Add one millennium to the instance (using date interval) with overflow explicitly forbidden.
298
 * @method        CarbonInterface subMillenniaNoOverflow(int $value = 1)                                             Sub millennia (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
299
 * @method        CarbonInterface subMillenniumNoOverflow()                                                          Sub one millennium to the instance (using date interval) with overflow explicitly forbidden.
300
 * @method        CarbonInterface addCenturies(int $value = 1)                                                       Add centuries (the $value count passed in) to the instance (using date interval).
301
 * @method        CarbonInterface addCentury()                                                                       Add one century to the instance (using date interval).
302
 * @method        CarbonInterface subCenturies(int $value = 1)                                                       Sub centuries (the $value count passed in) to the instance (using date interval).
303
 * @method        CarbonInterface subCentury()                                                                       Sub one century to the instance (using date interval).
304
 * @method        CarbonInterface addCenturiesWithOverflow(int $value = 1)                                           Add centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
305
 * @method        CarbonInterface addCenturyWithOverflow()                                                           Add one century to the instance (using date interval) with overflow explicitly allowed.
306
 * @method        CarbonInterface subCenturiesWithOverflow(int $value = 1)                                           Sub centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
307
 * @method        CarbonInterface subCenturyWithOverflow()                                                           Sub one century to the instance (using date interval) with overflow explicitly allowed.
308
 * @method        CarbonInterface addCenturiesWithoutOverflow(int $value = 1)                                        Add centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
309
 * @method        CarbonInterface addCenturyWithoutOverflow()                                                        Add one century to the instance (using date interval) with overflow explicitly forbidden.
310
 * @method        CarbonInterface subCenturiesWithoutOverflow(int $value = 1)                                        Sub centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
311
 * @method        CarbonInterface subCenturyWithoutOverflow()                                                        Sub one century to the instance (using date interval) with overflow explicitly forbidden.
312
 * @method        CarbonInterface addCenturiesWithNoOverflow(int $value = 1)                                         Add centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
313
 * @method        CarbonInterface addCenturyWithNoOverflow()                                                         Add one century to the instance (using date interval) with overflow explicitly forbidden.
314
 * @method        CarbonInterface subCenturiesWithNoOverflow(int $value = 1)                                         Sub centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
315
 * @method        CarbonInterface subCenturyWithNoOverflow()                                                         Sub one century to the instance (using date interval) with overflow explicitly forbidden.
316
 * @method        CarbonInterface addCenturiesNoOverflow(int $value = 1)                                             Add centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
317
 * @method        CarbonInterface addCenturyNoOverflow()                                                             Add one century to the instance (using date interval) with overflow explicitly forbidden.
318
 * @method        CarbonInterface subCenturiesNoOverflow(int $value = 1)                                             Sub centuries (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
319
 * @method        CarbonInterface subCenturyNoOverflow()                                                             Sub one century to the instance (using date interval) with overflow explicitly forbidden.
320
 * @method        CarbonInterface addDecades(int $value = 1)                                                         Add decades (the $value count passed in) to the instance (using date interval).
321
 * @method        CarbonInterface addDecade()                                                                        Add one decade to the instance (using date interval).
322
 * @method        CarbonInterface subDecades(int $value = 1)                                                         Sub decades (the $value count passed in) to the instance (using date interval).
323
 * @method        CarbonInterface subDecade()                                                                        Sub one decade to the instance (using date interval).
324
 * @method        CarbonInterface addDecadesWithOverflow(int $value = 1)                                             Add decades (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
325
 * @method        CarbonInterface addDecadeWithOverflow()                                                            Add one decade to the instance (using date interval) with overflow explicitly allowed.
326
 * @method        CarbonInterface subDecadesWithOverflow(int $value = 1)                                             Sub decades (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
327
 * @method        CarbonInterface subDecadeWithOverflow()                                                            Sub one decade to the instance (using date interval) with overflow explicitly allowed.
328
 * @method        CarbonInterface addDecadesWithoutOverflow(int $value = 1)                                          Add decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
329
 * @method        CarbonInterface addDecadeWithoutOverflow()                                                         Add one decade to the instance (using date interval) with overflow explicitly forbidden.
330
 * @method        CarbonInterface subDecadesWithoutOverflow(int $value = 1)                                          Sub decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
331
 * @method        CarbonInterface subDecadeWithoutOverflow()                                                         Sub one decade to the instance (using date interval) with overflow explicitly forbidden.
332
 * @method        CarbonInterface addDecadesWithNoOverflow(int $value = 1)                                           Add decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
333
 * @method        CarbonInterface addDecadeWithNoOverflow()                                                          Add one decade to the instance (using date interval) with overflow explicitly forbidden.
334
 * @method        CarbonInterface subDecadesWithNoOverflow(int $value = 1)                                           Sub decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
335
 * @method        CarbonInterface subDecadeWithNoOverflow()                                                          Sub one decade to the instance (using date interval) with overflow explicitly forbidden.
336
 * @method        CarbonInterface addDecadesNoOverflow(int $value = 1)                                               Add decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
337
 * @method        CarbonInterface addDecadeNoOverflow()                                                              Add one decade to the instance (using date interval) with overflow explicitly forbidden.
338
 * @method        CarbonInterface subDecadesNoOverflow(int $value = 1)                                               Sub decades (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
339
 * @method        CarbonInterface subDecadeNoOverflow()                                                              Sub one decade to the instance (using date interval) with overflow explicitly forbidden.
340
 * @method        CarbonInterface addQuarters(int $value = 1)                                                        Add quarters (the $value count passed in) to the instance (using date interval).
341
 * @method        CarbonInterface addQuarter()                                                                       Add one quarter to the instance (using date interval).
342
 * @method        CarbonInterface subQuarters(int $value = 1)                                                        Sub quarters (the $value count passed in) to the instance (using date interval).
343
 * @method        CarbonInterface subQuarter()                                                                       Sub one quarter to the instance (using date interval).
344
 * @method        CarbonInterface addQuartersWithOverflow(int $value = 1)                                            Add quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
345
 * @method        CarbonInterface addQuarterWithOverflow()                                                           Add one quarter to the instance (using date interval) with overflow explicitly allowed.
346
 * @method        CarbonInterface subQuartersWithOverflow(int $value = 1)                                            Sub quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly allowed.
347
 * @method        CarbonInterface subQuarterWithOverflow()                                                           Sub one quarter to the instance (using date interval) with overflow explicitly allowed.
348
 * @method        CarbonInterface addQuartersWithoutOverflow(int $value = 1)                                         Add quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
349
 * @method        CarbonInterface addQuarterWithoutOverflow()                                                        Add one quarter to the instance (using date interval) with overflow explicitly forbidden.
350
 * @method        CarbonInterface subQuartersWithoutOverflow(int $value = 1)                                         Sub quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
351
 * @method        CarbonInterface subQuarterWithoutOverflow()                                                        Sub one quarter to the instance (using date interval) with overflow explicitly forbidden.
352
 * @method        CarbonInterface addQuartersWithNoOverflow(int $value = 1)                                          Add quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
353
 * @method        CarbonInterface addQuarterWithNoOverflow()                                                         Add one quarter to the instance (using date interval) with overflow explicitly forbidden.
354
 * @method        CarbonInterface subQuartersWithNoOverflow(int $value = 1)                                          Sub quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
355
 * @method        CarbonInterface subQuarterWithNoOverflow()                                                         Sub one quarter to the instance (using date interval) with overflow explicitly forbidden.
356
 * @method        CarbonInterface addQuartersNoOverflow(int $value = 1)                                              Add quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
357
 * @method        CarbonInterface addQuarterNoOverflow()                                                             Add one quarter to the instance (using date interval) with overflow explicitly forbidden.
358
 * @method        CarbonInterface subQuartersNoOverflow(int $value = 1)                                              Sub quarters (the $value count passed in) to the instance (using date interval) with overflow explicitly forbidden.
359
 * @method        CarbonInterface subQuarterNoOverflow()                                                             Sub one quarter to the instance (using date interval) with overflow explicitly forbidden.
360
 * @method        CarbonInterface addWeeks(int $value = 1)                                                           Add weeks (the $value count passed in) to the instance (using date interval).
361
 * @method        CarbonInterface addWeek()                                                                          Add one week to the instance (using date interval).
362
 * @method        CarbonInterface subWeeks(int $value = 1)                                                           Sub weeks (the $value count passed in) to the instance (using date interval).
363
 * @method        CarbonInterface subWeek()                                                                          Sub one week to the instance (using date interval).
364
 * @method        CarbonInterface addWeekdays(int $value = 1)                                                        Add weekdays (the $value count passed in) to the instance (using date interval).
365
 * @method        CarbonInterface addWeekday()                                                                       Add one weekday to the instance (using date interval).
366
 * @method        CarbonInterface subWeekdays(int $value = 1)                                                        Sub weekdays (the $value count passed in) to the instance (using date interval).
367
 * @method        CarbonInterface subWeekday()                                                                       Sub one weekday to the instance (using date interval).
368
 * @method        CarbonInterface addRealMicros(int $value = 1)                                                      Add microseconds (the $value count passed in) to the instance (using timestamp).
369
 * @method        CarbonInterface addRealMicro()                                                                     Add one microsecond to the instance (using timestamp).
370
 * @method        CarbonInterface subRealMicros(int $value = 1)                                                      Sub microseconds (the $value count passed in) to the instance (using timestamp).
371
 * @method        CarbonInterface subRealMicro()                                                                     Sub one microsecond to the instance (using timestamp).
372
 * @method        CarbonPeriod    microsUntil($endDate = null, int $factor = 1)                                      Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each microsecond or every X microseconds if a factor is given.
373
 * @method        CarbonInterface addRealMicroseconds(int $value = 1)                                                Add microseconds (the $value count passed in) to the instance (using timestamp).
374
 * @method        CarbonInterface addRealMicrosecond()                                                               Add one microsecond to the instance (using timestamp).
375
 * @method        CarbonInterface subRealMicroseconds(int $value = 1)                                                Sub microseconds (the $value count passed in) to the instance (using timestamp).
376
 * @method        CarbonInterface subRealMicrosecond()                                                               Sub one microsecond to the instance (using timestamp).
377
 * @method        CarbonPeriod    microsecondsUntil($endDate = null, int $factor = 1)                                Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each microsecond or every X microseconds if a factor is given.
378
 * @method        CarbonInterface addRealMillis(int $value = 1)                                                      Add milliseconds (the $value count passed in) to the instance (using timestamp).
379
 * @method        CarbonInterface addRealMilli()                                                                     Add one millisecond to the instance (using timestamp).
380
 * @method        CarbonInterface subRealMillis(int $value = 1)                                                      Sub milliseconds (the $value count passed in) to the instance (using timestamp).
381
 * @method        CarbonInterface subRealMilli()                                                                     Sub one millisecond to the instance (using timestamp).
382
 * @method        CarbonPeriod    millisUntil($endDate = null, int $factor = 1)                                      Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each millisecond or every X milliseconds if a factor is given.
383
 * @method        CarbonInterface addRealMilliseconds(int $value = 1)                                                Add milliseconds (the $value count passed in) to the instance (using timestamp).
384
 * @method        CarbonInterface addRealMillisecond()                                                               Add one millisecond to the instance (using timestamp).
385
 * @method        CarbonInterface subRealMilliseconds(int $value = 1)                                                Sub milliseconds (the $value count passed in) to the instance (using timestamp).
386
 * @method        CarbonInterface subRealMillisecond()                                                               Sub one millisecond to the instance (using timestamp).
387
 * @method        CarbonPeriod    millisecondsUntil($endDate = null, int $factor = 1)                                Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each millisecond or every X milliseconds if a factor is given.
388
 * @method        CarbonInterface addRealSeconds(int $value = 1)                                                     Add seconds (the $value count passed in) to the instance (using timestamp).
389
 * @method        CarbonInterface addRealSecond()                                                                    Add one second to the instance (using timestamp).
390
 * @method        CarbonInterface subRealSeconds(int $value = 1)                                                     Sub seconds (the $value count passed in) to the instance (using timestamp).
391
 * @method        CarbonInterface subRealSecond()                                                                    Sub one second to the instance (using timestamp).
392
 * @method        CarbonPeriod    secondsUntil($endDate = null, int $factor = 1)                                     Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each second or every X seconds if a factor is given.
393
 * @method        CarbonInterface addRealMinutes(int $value = 1)                                                     Add minutes (the $value count passed in) to the instance (using timestamp).
394
 * @method        CarbonInterface addRealMinute()                                                                    Add one minute to the instance (using timestamp).
395
 * @method        CarbonInterface subRealMinutes(int $value = 1)                                                     Sub minutes (the $value count passed in) to the instance (using timestamp).
396
 * @method        CarbonInterface subRealMinute()                                                                    Sub one minute to the instance (using timestamp).
397
 * @method        CarbonPeriod    minutesUntil($endDate = null, int $factor = 1)                                     Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each minute or every X minutes if a factor is given.
398
 * @method        CarbonInterface addRealHours(int $value = 1)                                                       Add hours (the $value count passed in) to the instance (using timestamp).
399
 * @method        CarbonInterface addRealHour()                                                                      Add one hour to the instance (using timestamp).
400
 * @method        CarbonInterface subRealHours(int $value = 1)                                                       Sub hours (the $value count passed in) to the instance (using timestamp).
401
 * @method        CarbonInterface subRealHour()                                                                      Sub one hour to the instance (using timestamp).
402
 * @method        CarbonPeriod    hoursUntil($endDate = null, int $factor = 1)                                       Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each hour or every X hours if a factor is given.
403
 * @method        CarbonInterface addRealDays(int $value = 1)                                                        Add days (the $value count passed in) to the instance (using timestamp).
404
 * @method        CarbonInterface addRealDay()                                                                       Add one day to the instance (using timestamp).
405
 * @method        CarbonInterface subRealDays(int $value = 1)                                                        Sub days (the $value count passed in) to the instance (using timestamp).
406
 * @method        CarbonInterface subRealDay()                                                                       Sub one day to the instance (using timestamp).
407
 * @method        CarbonPeriod    daysUntil($endDate = null, int $factor = 1)                                        Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each day or every X days if a factor is given.
408
 * @method        CarbonInterface addRealWeeks(int $value = 1)                                                       Add weeks (the $value count passed in) to the instance (using timestamp).
409
 * @method        CarbonInterface addRealWeek()                                                                      Add one week to the instance (using timestamp).
410
 * @method        CarbonInterface subRealWeeks(int $value = 1)                                                       Sub weeks (the $value count passed in) to the instance (using timestamp).
411
 * @method        CarbonInterface subRealWeek()                                                                      Sub one week to the instance (using timestamp).
412
 * @method        CarbonPeriod    weeksUntil($endDate = null, int $factor = 1)                                       Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each week or every X weeks if a factor is given.
413
 * @method        CarbonInterface addRealMonths(int $value = 1)                                                      Add months (the $value count passed in) to the instance (using timestamp).
414
 * @method        CarbonInterface addRealMonth()                                                                     Add one month to the instance (using timestamp).
415
 * @method        CarbonInterface subRealMonths(int $value = 1)                                                      Sub months (the $value count passed in) to the instance (using timestamp).
416
 * @method        CarbonInterface subRealMonth()                                                                     Sub one month to the instance (using timestamp).
417
 * @method        CarbonPeriod    monthsUntil($endDate = null, int $factor = 1)                                      Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each month or every X months if a factor is given.
418
 * @method        CarbonInterface addRealQuarters(int $value = 1)                                                    Add quarters (the $value count passed in) to the instance (using timestamp).
419
 * @method        CarbonInterface addRealQuarter()                                                                   Add one quarter to the instance (using timestamp).
420
 * @method        CarbonInterface subRealQuarters(int $value = 1)                                                    Sub quarters (the $value count passed in) to the instance (using timestamp).
421
 * @method        CarbonInterface subRealQuarter()                                                                   Sub one quarter to the instance (using timestamp).
422
 * @method        CarbonPeriod    quartersUntil($endDate = null, int $factor = 1)                                    Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each quarter or every X quarters if a factor is given.
423
 * @method        CarbonInterface addRealYears(int $value = 1)                                                       Add years (the $value count passed in) to the instance (using timestamp).
424
 * @method        CarbonInterface addRealYear()                                                                      Add one year to the instance (using timestamp).
425
 * @method        CarbonInterface subRealYears(int $value = 1)                                                       Sub years (the $value count passed in) to the instance (using timestamp).
426
 * @method        CarbonInterface subRealYear()                                                                      Sub one year to the instance (using timestamp).
427
 * @method        CarbonPeriod    yearsUntil($endDate = null, int $factor = 1)                                       Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each year or every X years if a factor is given.
428
 * @method        CarbonInterface addRealDecades(int $value = 1)                                                     Add decades (the $value count passed in) to the instance (using timestamp).
429
 * @method        CarbonInterface addRealDecade()                                                                    Add one decade to the instance (using timestamp).
430
 * @method        CarbonInterface subRealDecades(int $value = 1)                                                     Sub decades (the $value count passed in) to the instance (using timestamp).
431
 * @method        CarbonInterface subRealDecade()                                                                    Sub one decade to the instance (using timestamp).
432
 * @method        CarbonPeriod    decadesUntil($endDate = null, int $factor = 1)                                     Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each decade or every X decades if a factor is given.
433
 * @method        CarbonInterface addRealCenturies(int $value = 1)                                                   Add centuries (the $value count passed in) to the instance (using timestamp).
434
 * @method        CarbonInterface addRealCentury()                                                                   Add one century to the instance (using timestamp).
435
 * @method        CarbonInterface subRealCenturies(int $value = 1)                                                   Sub centuries (the $value count passed in) to the instance (using timestamp).
436
 * @method        CarbonInterface subRealCentury()                                                                   Sub one century to the instance (using timestamp).
437
 * @method        CarbonPeriod    centuriesUntil($endDate = null, int $factor = 1)                                   Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each century or every X centuries if a factor is given.
438
 * @method        CarbonInterface addRealMillennia(int $value = 1)                                                   Add millennia (the $value count passed in) to the instance (using timestamp).
439
 * @method        CarbonInterface addRealMillennium()                                                                Add one millennium to the instance (using timestamp).
440
 * @method        CarbonInterface subRealMillennia(int $value = 1)                                                   Sub millennia (the $value count passed in) to the instance (using timestamp).
441
 * @method        CarbonInterface subRealMillennium()                                                                Sub one millennium to the instance (using timestamp).
442
 * @method        CarbonPeriod    millenniaUntil($endDate = null, int $factor = 1)                                   Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each millennium or every X millennia if a factor is given.
443
 * @method        CarbonInterface roundYear(float $precision = 1, string $function = "round")                        Round the current instance year with given precision using the given function.
444
 * @method        CarbonInterface roundYears(float $precision = 1, string $function = "round")                       Round the current instance year with given precision using the given function.
445
 * @method        CarbonInterface floorYear(float $precision = 1)                                                    Truncate the current instance year with given precision.
446
 * @method        CarbonInterface floorYears(float $precision = 1)                                                   Truncate the current instance year with given precision.
447
 * @method        CarbonInterface ceilYear(float $precision = 1)                                                     Ceil the current instance year with given precision.
448
 * @method        CarbonInterface ceilYears(float $precision = 1)                                                    Ceil the current instance year with given precision.
449
 * @method        CarbonInterface roundMonth(float $precision = 1, string $function = "round")                       Round the current instance month with given precision using the given function.
450
 * @method        CarbonInterface roundMonths(float $precision = 1, string $function = "round")                      Round the current instance month with given precision using the given function.
451
 * @method        CarbonInterface floorMonth(float $precision = 1)                                                   Truncate the current instance month with given precision.
452
 * @method        CarbonInterface floorMonths(float $precision = 1)                                                  Truncate the current instance month with given precision.
453
 * @method        CarbonInterface ceilMonth(float $precision = 1)                                                    Ceil the current instance month with given precision.
454
 * @method        CarbonInterface ceilMonths(float $precision = 1)                                                   Ceil the current instance month with given precision.
455
 * @method        CarbonInterface roundDay(float $precision = 1, string $function = "round")                         Round the current instance day with given precision using the given function.
456
 * @method        CarbonInterface roundDays(float $precision = 1, string $function = "round")                        Round the current instance day with given precision using the given function.
457
 * @method        CarbonInterface floorDay(float $precision = 1)                                                     Truncate the current instance day with given precision.
458
 * @method        CarbonInterface floorDays(float $precision = 1)                                                    Truncate the current instance day with given precision.
459
 * @method        CarbonInterface ceilDay(float $precision = 1)                                                      Ceil the current instance day with given precision.
460
 * @method        CarbonInterface ceilDays(float $precision = 1)                                                     Ceil the current instance day with given precision.
461
 * @method        CarbonInterface roundHour(float $precision = 1, string $function = "round")                        Round the current instance hour with given precision using the given function.
462
 * @method        CarbonInterface roundHours(float $precision = 1, string $function = "round")                       Round the current instance hour with given precision using the given function.
463
 * @method        CarbonInterface floorHour(float $precision = 1)                                                    Truncate the current instance hour with given precision.
464
 * @method        CarbonInterface floorHours(float $precision = 1)                                                   Truncate the current instance hour with given precision.
465
 * @method        CarbonInterface ceilHour(float $precision = 1)                                                     Ceil the current instance hour with given precision.
466
 * @method        CarbonInterface ceilHours(float $precision = 1)                                                    Ceil the current instance hour with given precision.
467
 * @method        CarbonInterface roundMinute(float $precision = 1, string $function = "round")                      Round the current instance minute with given precision using the given function.
468
 * @method        CarbonInterface roundMinutes(float $precision = 1, string $function = "round")                     Round the current instance minute with given precision using the given function.
469
 * @method        CarbonInterface floorMinute(float $precision = 1)                                                  Truncate the current instance minute with given precision.
470
 * @method        CarbonInterface floorMinutes(float $precision = 1)                                                 Truncate the current instance minute with given precision.
471
 * @method        CarbonInterface ceilMinute(float $precision = 1)                                                   Ceil the current instance minute with given precision.
472
 * @method        CarbonInterface ceilMinutes(float $precision = 1)                                                  Ceil the current instance minute with given precision.
473
 * @method        CarbonInterface roundSecond(float $precision = 1, string $function = "round")                      Round the current instance second with given precision using the given function.
474
 * @method        CarbonInterface roundSeconds(float $precision = 1, string $function = "round")                     Round the current instance second with given precision using the given function.
475
 * @method        CarbonInterface floorSecond(float $precision = 1)                                                  Truncate the current instance second with given precision.
476
 * @method        CarbonInterface floorSeconds(float $precision = 1)                                                 Truncate the current instance second with given precision.
477
 * @method        CarbonInterface ceilSecond(float $precision = 1)                                                   Ceil the current instance second with given precision.
478
 * @method        CarbonInterface ceilSeconds(float $precision = 1)                                                  Ceil the current instance second with given precision.
479
 * @method        CarbonInterface roundMillennium(float $precision = 1, string $function = "round")                  Round the current instance millennium with given precision using the given function.
480
 * @method        CarbonInterface roundMillennia(float $precision = 1, string $function = "round")                   Round the current instance millennium with given precision using the given function.
481
 * @method        CarbonInterface floorMillennium(float $precision = 1)                                              Truncate the current instance millennium with given precision.
482
 * @method        CarbonInterface floorMillennia(float $precision = 1)                                               Truncate the current instance millennium with given precision.
483
 * @method        CarbonInterface ceilMillennium(float $precision = 1)                                               Ceil the current instance millennium with given precision.
484
 * @method        CarbonInterface ceilMillennia(float $precision = 1)                                                Ceil the current instance millennium with given precision.
485
 * @method        CarbonInterface roundCentury(float $precision = 1, string $function = "round")                     Round the current instance century with given precision using the given function.
486
 * @method        CarbonInterface roundCenturies(float $precision = 1, string $function = "round")                   Round the current instance century with given precision using the given function.
487
 * @method        CarbonInterface floorCentury(float $precision = 1)                                                 Truncate the current instance century with given precision.
488
 * @method        CarbonInterface floorCenturies(float $precision = 1)                                               Truncate the current instance century with given precision.
489
 * @method        CarbonInterface ceilCentury(float $precision = 1)                                                  Ceil the current instance century with given precision.
490
 * @method        CarbonInterface ceilCenturies(float $precision = 1)                                                Ceil the current instance century with given precision.
491
 * @method        CarbonInterface roundDecade(float $precision = 1, string $function = "round")                      Round the current instance decade with given precision using the given function.
492
 * @method        CarbonInterface roundDecades(float $precision = 1, string $function = "round")                     Round the current instance decade with given precision using the given function.
493
 * @method        CarbonInterface floorDecade(float $precision = 1)                                                  Truncate the current instance decade with given precision.
494
 * @method        CarbonInterface floorDecades(float $precision = 1)                                                 Truncate the current instance decade with given precision.
495
 * @method        CarbonInterface ceilDecade(float $precision = 1)                                                   Ceil the current instance decade with given precision.
496
 * @method        CarbonInterface ceilDecades(float $precision = 1)                                                  Ceil the current instance decade with given precision.
497
 * @method        CarbonInterface roundQuarter(float $precision = 1, string $function = "round")                     Round the current instance quarter with given precision using the given function.
498
 * @method        CarbonInterface roundQuarters(float $precision = 1, string $function = "round")                    Round the current instance quarter with given precision using the given function.
499
 * @method        CarbonInterface floorQuarter(float $precision = 1)                                                 Truncate the current instance quarter with given precision.
500
 * @method        CarbonInterface floorQuarters(float $precision = 1)                                                Truncate the current instance quarter with given precision.
501
 * @method        CarbonInterface ceilQuarter(float $precision = 1)                                                  Ceil the current instance quarter with given precision.
502
 * @method        CarbonInterface ceilQuarters(float $precision = 1)                                                 Ceil the current instance quarter with given precision.
503
 * @method        CarbonInterface roundMillisecond(float $precision = 1, string $function = "round")                 Round the current instance millisecond with given precision using the given function.
504
 * @method        CarbonInterface roundMilliseconds(float $precision = 1, string $function = "round")                Round the current instance millisecond with given precision using the given function.
505
 * @method        CarbonInterface floorMillisecond(float $precision = 1)                                             Truncate the current instance millisecond with given precision.
506
 * @method        CarbonInterface floorMilliseconds(float $precision = 1)                                            Truncate the current instance millisecond with given precision.
507
 * @method        CarbonInterface ceilMillisecond(float $precision = 1)                                              Ceil the current instance millisecond with given precision.
508
 * @method        CarbonInterface ceilMilliseconds(float $precision = 1)                                             Ceil the current instance millisecond with given precision.
509
 * @method        CarbonInterface roundMicrosecond(float $precision = 1, string $function = "round")                 Round the current instance microsecond with given precision using the given function.
510
 * @method        CarbonInterface roundMicroseconds(float $precision = 1, string $function = "round")                Round the current instance microsecond with given precision using the given function.
511
 * @method        CarbonInterface floorMicrosecond(float $precision = 1)                                             Truncate the current instance microsecond with given precision.
512
 * @method        CarbonInterface floorMicroseconds(float $precision = 1)                                            Truncate the current instance microsecond with given precision.
513
 * @method        CarbonInterface ceilMicrosecond(float $precision = 1)                                              Ceil the current instance microsecond with given precision.
514
 * @method        CarbonInterface ceilMicroseconds(float $precision = 1)                                             Ceil the current instance microsecond with given precision.
515
 * @method        string          shortAbsoluteDiffForHumans(DateTimeInterface $other = null, int $parts = 1)        Get the difference (short format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
516
 * @method        string          longAbsoluteDiffForHumans(DateTimeInterface $other = null, int $parts = 1)         Get the difference (long format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
517
 * @method        string          shortRelativeDiffForHumans(DateTimeInterface $other = null, int $parts = 1)        Get the difference (short format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
518
 * @method        string          longRelativeDiffForHumans(DateTimeInterface $other = null, int $parts = 1)         Get the difference (long format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
519
 * @method        string          shortRelativeToNowDiffForHumans(DateTimeInterface $other = null, int $parts = 1)   Get the difference (short format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
520
 * @method        string          longRelativeToNowDiffForHumans(DateTimeInterface $other = null, int $parts = 1)    Get the difference (long format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
521
 * @method        string          shortRelativeToOtherDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
522
 * @method        string          longRelativeToOtherDiffForHumans(DateTimeInterface $other = null, int $parts = 1)  Get the difference (long format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.)
523
 *
524
 * </autodoc>
525
 */
526
trait Date
527
{
528
    use Boundaries;
529
    use Comparison;
530
    use Converter;
531
    use Creator;
532
    use Difference;
533
    use Macro;
534
    use Modifiers;
535
    use Mutability;
536
    use ObjectInitialisation;
537
    use Options;
538
    use Rounding;
539
    use Serialization;
540
    use Test;
541
    use Timestamp;
542
    use Units;
543
    use Week;
544
545
    /**
546
     * Names of days of the week.
547
     *
548
     * @var array
549
     */
550
    protected static $days = [
551
        // @call isDayOfWeek
552
        CarbonInterface::SUNDAY => 'Sunday',
553
        // @call isDayOfWeek
554
        CarbonInterface::MONDAY => 'Monday',
555
        // @call isDayOfWeek
556
        CarbonInterface::TUESDAY => 'Tuesday',
557
        // @call isDayOfWeek
558
        CarbonInterface::WEDNESDAY => 'Wednesday',
559
        // @call isDayOfWeek
560
        CarbonInterface::THURSDAY => 'Thursday',
561
        // @call isDayOfWeek
562
        CarbonInterface::FRIDAY => 'Friday',
563
        // @call isDayOfWeek
564
        CarbonInterface::SATURDAY => 'Saturday',
565
    ];
566
567
    /**
568
     * Will UTF8 encoding be used to print localized date/time ?
569
     *
570
     * @var bool
571
     */
572
    protected static $utf8 = false;
573
574
    /**
575
     * List of unit and magic methods associated as doc-comments.
576
     *
577
     * @var array
578
     */
579
    protected static $units = [
580
        // @call setUnit
581
        // @call addUnit
582
        'year',
583
        // @call setUnit
584
        // @call addUnit
585
        'month',
586
        // @call setUnit
587
        // @call addUnit
588
        'day',
589
        // @call setUnit
590
        // @call addUnit
591
        'hour',
592
        // @call setUnit
593
        // @call addUnit
594
        'minute',
595
        // @call setUnit
596
        // @call addUnit
597
        'second',
598
        // @call setUnit
599
        // @call addUnit
600
        'milli',
601
        // @call setUnit
602
        // @call addUnit
603
        'millisecond',
604
        // @call setUnit
605
        // @call addUnit
606
        'micro',
607
        // @call setUnit
608
        // @call addUnit
609
        'microsecond',
610
    ];
611
612
    /**
613
     * Creates a DateTimeZone from a string, DateTimeZone or integer offset.
614
     *
615
     * @param DateTimeZone|string|int|null $object     original value to get CarbonTimeZone from it.
616
     * @param DateTimeZone|string|int|null $objectDump dump of the object for error messages.
617
     *
618
     * @throws InvalidTimeZoneException
619
     *
620
     * @return CarbonTimeZone|false
621
     */
622
    protected static function safeCreateDateTimeZone($object, $objectDump = null)
623
    {
624
        return CarbonTimeZone::instance($object, $objectDump);
625
    }
626
627
    /**
628
     * Get the TimeZone associated with the Carbon instance (as CarbonTimeZone).
629
     *
630
     * @return CarbonTimeZone
631
     *
632
     * @link http://php.net/manual/en/datetime.gettimezone.php
633
     */
634
    public function getTimezone()
635
    {
636
        return CarbonTimeZone::instance(parent::getTimezone());
637
    }
638
639
    /**
640
     * List of minimum and maximums for each unit.
641
     *
642
     * @return array
643
     */
644
    protected static function getRangesByUnit()
645
    {
646
        return [
647
            // @call roundUnit
648
            'year' => [1, 9999],
649
            // @call roundUnit
650
            'month' => [1, static::MONTHS_PER_YEAR],
651
            // @call roundUnit
652
            'day' => [1, 31],
653
            // @call roundUnit
654
            'hour' => [0, static::HOURS_PER_DAY - 1],
655
            // @call roundUnit
656
            'minute' => [0, static::MINUTES_PER_HOUR - 1],
657
            // @call roundUnit
658
            'second' => [0, static::SECONDS_PER_MINUTE - 1],
659
        ];
660
    }
661
662
    /**
663
     * Get a copy of the instance.
664
     *
665
     * @return static
666
     */
667
    public function copy()
668
    {
669
        return clone $this;
670
    }
671
672
    /**
673
     * @alias copy
674
     *
675
     * Get a copy of the instance.
676
     *
677
     * @return static
678
     */
679
    public function clone()
680
    {
681
        return clone $this;
682
    }
683
684
    /**
685
     * Returns a present instance in the same timezone.
686
     *
687
     * @return static
688
     */
689
    public function nowWithSameTz()
690
    {
691
        return static::now($this->getTimezone());
692
    }
693
694
    /**
695
     * Throws an exception if the given object is not a DateTime and does not implement DateTimeInterface.
696
     *
697
     * @param mixed        $date
698
     * @param string|array $other
699
     *
700
     * @throws InvalidTypeException
701
     */
702
    protected static function expectDateTime($date, $other = [])
703
    {
704
        $message = 'Expected ';
705
        foreach ((array) $other as $expect) {
706
            $message .= "$expect, ";
707
        }
708
709
        if (!$date instanceof DateTime && !$date instanceof DateTimeInterface) {
710
            throw new InvalidTypeException(
711
                $message.'DateTime or DateTimeInterface, '.
712
                (is_object($date) ? get_class($date) : gettype($date)).' given'
713
            );
714
        }
715
    }
716
717
    /**
718
     * Return the Carbon instance passed through, a now instance in the same timezone
719
     * if null given or parse the input if string given.
720
     *
721
     * @param Carbon|DateTimeInterface|string|null $date
722
     *
723
     * @return static
724
     */
725
    protected function resolveCarbon($date = null)
726
    {
727
        if (!$date) {
728
            return $this->nowWithSameTz();
729
        }
730
731
        if (is_string($date)) {
732
            return static::parse($date, $this->getTimezone());
733
        }
734
735
        static::expectDateTime($date, ['null', 'string']);
736
737
        return $date instanceof self ? $date : static::instance($date);
738
    }
739
740
    /**
741
     * Return the Carbon instance passed through, a now instance in the same timezone
742
     * if null given or parse the input if string given.
743
     *
744
     * @param Carbon|\Carbon\CarbonPeriod|\Carbon\CarbonInterval|\DateInterval|\DatePeriod|DateTimeInterface|string|null $date
745
     *
746
     * @return static
747
     */
748
    public function carbonize($date = null)
749
    {
750
        if ($date instanceof DateInterval) {
751
            return $this->copy()->add($date);
752
        }
753
754
        if ($date instanceof DatePeriod || $date instanceof CarbonPeriod) {
755
            $date = $date->getStartDate();
756
        }
757
758
        return $this->resolveCarbon($date);
759
    }
760
761
    ///////////////////////////////////////////////////////////////////
762
    ///////////////////////// GETTERS AND SETTERS /////////////////////
763
    ///////////////////////////////////////////////////////////////////
764
765
    /**
766
     * Get a part of the Carbon object
767
     *
768
     * @param string $name
769
     *
770
     * @throws UnknownGetterException
771
     *
772
     * @return string|int|bool|DateTimeZone|null
773
     */
774
    public function __get($name)
775
    {
776
        return $this->get($name);
777
    }
778
779
    /**
780
     * Get a part of the Carbon object
781
     *
782
     * @param string $name
783
     *
784
     * @throws UnknownGetterException
785
     *
786
     * @return string|int|bool|DateTimeZone|null
787
     */
788
    public function get($name)
789
    {
790
        static $formats = [
791
            // @property int
792
            'year' => 'Y',
793
            // @property int
794
            'yearIso' => 'o',
795
            // @property int
796
            // @call isSameUnit
797
            'month' => 'n',
798
            // @property int
799
            'day' => 'j',
800
            // @property int
801
            'hour' => 'G',
802
            // @property int
803
            'minute' => 'i',
804
            // @property int
805
            'second' => 's',
806
            // @property int
807
            'micro' => 'u',
808
            // @property int
809
            'microsecond' => 'u',
810
            // @property-read int 0 (for Sunday) through 6 (for Saturday)
811
            'dayOfWeek' => 'w',
812
            // @property-read int 1 (for Monday) through 7 (for Sunday)
813
            'dayOfWeekIso' => 'N',
814
            // @property-read int ISO-8601 week number of year, weeks starting on Monday
815
            'weekOfYear' => 'W',
816
            // @property-read int number of days in the given month
817
            'daysInMonth' => 't',
818
            // @property int seconds since the Unix Epoch
819
            'timestamp' => 'U',
820
            // @property-read string "am"/"pm" (Ante meridiem or Post meridiem latin lowercase mark)
821
            'latinMeridiem' => 'a',
822
            // @property-read string "AM"/"PM" (Ante meridiem or Post meridiem latin uppercase mark)
823
            'latinUpperMeridiem' => 'A',
824
            // @property string the day of week in English
825
            'englishDayOfWeek' => 'l',
826
            // @property string the abbreviated day of week in English
827
            'shortEnglishDayOfWeek' => 'D',
828
            // @property string the month in English
829
            'englishMonth' => 'F',
830
            // @property string the abbreviated month in English
831
            'shortEnglishMonth' => 'M',
832
            // @property string the day of week in current locale LC_TIME
833
            'localeDayOfWeek' => '%A',
834
            // @property string the abbreviated day of week in current locale LC_TIME
835
            'shortLocaleDayOfWeek' => '%a',
836
            // @property string the month in current locale LC_TIME
837
            'localeMonth' => '%B',
838
            // @property string the abbreviated month in current locale LC_TIME
839
            'shortLocaleMonth' => '%b',
840
            // @property-read string $timezoneAbbreviatedName the current timezone abbreviated name
841
            'timezoneAbbreviatedName' => 'T',
842
            // @property-read string $tzAbbrName alias of $timezoneAbbreviatedName
843
            'tzAbbrName' => 'T',
844
        ];
845
846
        switch (true) {
847
            case isset($formats[$name]):
848
                $format = $formats[$name];
849
                $method = substr($format, 0, 1) === '%' ? 'formatLocalized' : 'rawFormat';
850
                $value = $this->$method($format);
851
852
                return is_numeric($value) ? (int) $value : $value;
853
854
            // @property-read string long name of weekday translated according to Carbon locale, in english if no translation available for current language
855
            case $name === 'dayName':
856
                return $this->getTranslatedDayName();
857
            // @property-read string short name of weekday translated according to Carbon locale, in english if no translation available for current language
858
            case $name === 'shortDayName':
859
                return $this->getTranslatedShortDayName();
860
            // @property-read string very short name of weekday translated according to Carbon locale, in english if no translation available for current language
861
            case $name === 'minDayName':
862
                return $this->getTranslatedMinDayName();
863
            // @property-read string long name of month translated according to Carbon locale, in english if no translation available for current language
864
            case $name === 'monthName':
865
                return $this->getTranslatedMonthName();
866
            // @property-read string short name of month translated according to Carbon locale, in english if no translation available for current language
867
            case $name === 'shortMonthName':
868
                return $this->getTranslatedShortMonthName();
869
            // @property-read string lowercase meridiem mark translated according to Carbon locale, in latin if no translation available for current language
870
            case $name === 'meridiem':
871
                return $this->meridiem(true);
872
            // @property-read string uppercase meridiem mark translated according to Carbon locale, in latin if no translation available for current language
873
            case $name === 'upperMeridiem':
874
                return $this->meridiem();
875
            // @property-read int current hour from 1 to 24
876
            case $name === 'noZeroHour':
877
                return $this->hour ?: 24;
878
            // @property int
879
            case $name === 'milliseconds':
880
                // @property int
881
            case $name === 'millisecond':
882
            // @property int
883
            case $name === 'milli':
884
                return (int) floor($this->rawFormat('u') / 1000);
885
886
            // @property int 1 through 53
887
            case $name === 'week':
888
                return (int) $this->week();
889
890
            // @property int 1 through 53
891
            case $name === 'isoWeek':
892
                return (int) $this->isoWeek();
893
894
            // @property int year according to week format
895
            case $name === 'weekYear':
896
                return (int) $this->weekYear();
897
898
            // @property int year according to ISO week format
899
            case $name === 'isoWeekYear':
900
                return (int) $this->isoWeekYear();
901
902
            // @property-read int 51 through 53
903
            case $name === 'weeksInYear':
904
                return (int) $this->weeksInYear();
905
906
            // @property-read int 51 through 53
907
            case $name === 'isoWeeksInYear':
908
                return (int) $this->isoWeeksInYear();
909
910
            // @property-read int 1 through 5
911
            case $name === 'weekOfMonth':
912
                return (int) ceil($this->day / static::DAYS_PER_WEEK);
913
914
            // @property-read int 1 through 5
915
            case $name === 'weekNumberInMonth':
916
                return (int) ceil(($this->day + $this->copy()->startOfMonth()->dayOfWeekIso - 1) / static::DAYS_PER_WEEK);
917
918
            // @property-read int 0 through 6
919
            case $name === 'firstWeekDay':
920
                return $this->localTranslator ? ($this->getTranslationMessage('first_day_of_week') ?? 0) : static::getWeekStartsAt();
921
922
            // @property-read int 0 through 6
923
            case $name === 'lastWeekDay':
924
                return $this->localTranslator ? (($this->getTranslationMessage('first_day_of_week') ?? 0) + static::DAYS_PER_WEEK - 1) % static::DAYS_PER_WEEK : static::getWeekEndsAt();
925
926
            // @property int 1 through 366
927
            case $name === 'dayOfYear':
928
                return 1 + intval($this->rawFormat('z'));
929
930
            // @property-read int 365 or 366
931
            case $name === 'daysInYear':
932
                return $this->isLeapYear() ? 366 : 365;
933
934
            // @property int does a diffInYears() with default parameters
935
            case $name === 'age':
936
                return $this->diffInYears();
937
938
            // @property-read int the quarter of this instance, 1 - 4
939
            // @call isSameUnit
940
            case $name === 'quarter':
941
                return (int) ceil($this->month / static::MONTHS_PER_QUARTER);
942
943
            // @property-read int the decade of this instance
944
            // @call isSameUnit
945
            case $name === 'decade':
946
                return (int) ceil($this->year / static::YEARS_PER_DECADE);
947
948
            // @property-read int the century of this instance
949
            // @call isSameUnit
950
            case $name === 'century':
951
                $factor = 1;
952
                $year = $this->year;
953
                if ($year < 0) {
954
                    $year = -$year;
955
                    $factor = -1;
956
                }
957
958
                return (int) ($factor * ceil($year / static::YEARS_PER_CENTURY));
959
960
            // @property-read int the millennium of this instance
961
            // @call isSameUnit
962
            case $name === 'millennium':
963
                $factor = 1;
964
                $year = $this->year;
965
                if ($year < 0) {
966
                    $year = -$year;
967
                    $factor = -1;
968
                }
969
970
                return (int) ($factor * ceil($year / static::YEARS_PER_MILLENNIUM));
971
972
            // @property int the timezone offset in seconds from UTC
973
            case $name === 'offset':
974
                return $this->getOffset();
975
976
            // @property int the timezone offset in minutes from UTC
977
            case $name === 'offsetMinutes':
978
                return $this->getOffset() / static::SECONDS_PER_MINUTE;
979
980
            // @property int the timezone offset in hours from UTC
981
            case $name === 'offsetHours':
982
                return $this->getOffset() / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR;
983
984
            // @property-read bool daylight savings time indicator, true if DST, false otherwise
985
            case $name === 'dst':
986
                return $this->rawFormat('I') === '1';
987
988
            // @property-read bool checks if the timezone is local, true if local, false otherwise
989
            case $name === 'local':
990
                return $this->getOffset() === $this->copy()->setTimezone(date_default_timezone_get())->getOffset();
991
992
            // @property-read bool checks if the timezone is UTC, true if UTC, false otherwise
993
            case $name === 'utc':
994
                return $this->getOffset() === 0;
995
996
            // @property CarbonTimeZone $timezone the current timezone
997
            // @property CarbonTimeZone $tz alias of $timezone
998
            case $name === 'timezone' || $name === 'tz':
999
                return CarbonTimeZone::instance($this->getTimezone());
1000
1001
            // @property-read string $timezoneName the current timezone name
1002
            // @property-read string $tzName alias of $timezoneName
1003
            case $name === 'timezoneName' || $name === 'tzName':
1004
                return $this->getTimezone()->getName();
1005
1006
            // @property-read string locale of the current instance
1007
            case $name === 'locale':
1008
                return $this->getTranslatorLocale();
1009
1010
            default:
1011
                $macro = $this->getLocalMacro('get'.ucfirst($name));
1012
1013
                if ($macro) {
1014
                    return $this->executeCallableWithContext($macro);
1015
                }
1016
1017
                throw new UnknownGetterException($name);
1018
        }
1019
    }
1020
1021
    /**
1022
     * Check if an attribute exists on the object
1023
     *
1024
     * @param string $name
1025
     *
1026
     * @return bool
1027
     */
1028
    public function __isset($name)
1029
    {
1030
        try {
1031
            $this->__get($name);
1032
        } catch (UnknownGetterException | ReflectionException $e) {
1033
            return false;
1034
        }
1035
1036
        return true;
1037
    }
1038
1039
    /**
1040
     * Set a part of the Carbon object
1041
     *
1042
     * @param string                  $name
1043
     * @param string|int|DateTimeZone $value
1044
     *
1045
     * @throws UnknownSetterException|ReflectionException
1046
     *
1047
     * @return void
1048
     */
1049
    public function __set($name, $value)
1050
    {
1051
        if ($this->constructedObjectId === spl_object_hash($this)) {
1052
            $this->set($name, $value);
1053
1054
            return;
1055
        }
1056
1057
        $this->$name = $value;
1058
    }
1059
1060
    /**
1061
     * Set a part of the Carbon object
1062
     *
1063
     * @param string|array            $name
1064
     * @param string|int|DateTimeZone $value
1065
     *
1066
     * @throws ImmutableException|UnknownSetterException
1067
     *
1068
     * @return $this
1069
     */
1070
    public function set($name, $value = null)
1071
    {
1072
        if ($this->isImmutable()) {
1073
            throw new ImmutableException(sprintf('%s class', static::class));
1074
        }
1075
1076
        if (is_array($name)) {
1077
            foreach ($name as $key => $value) {
1078
                $this->set($key, $value);
1079
            }
1080
1081
            return $this;
1082
        }
1083
1084
        switch ($name) {
1085
            case 'milliseconds':
1086
            case 'millisecond':
1087
            case 'milli':
1088
            case 'microseconds':
1089
            case 'microsecond':
1090
            case 'micro':
1091
                if (substr($name, 0, 5) === 'milli') {
1092
                    $value *= 1000;
1093
                }
1094
1095
                while ($value < 0) {
1096
                    $this->subSecond();
1097
                    $value += static::MICROSECONDS_PER_SECOND;
1098
                }
1099
1100
                while ($value >= static::MICROSECONDS_PER_SECOND) {
1101
                    $this->addSecond();
1102
                    $value -= static::MICROSECONDS_PER_SECOND;
1103
                }
1104
1105
                $this->modify($this->rawFormat('H:i:s.').str_pad((string) round($value), 6, '0', STR_PAD_LEFT));
1106
1107
                break;
1108
1109
            case 'year':
1110
            case 'month':
1111
            case 'day':
1112
            case 'hour':
1113
            case 'minute':
1114
            case 'second':
1115
                [$year, $month, $day, $hour, $minute, $second] = array_map('intval', explode('-', $this->rawFormat('Y-n-j-G-i-s')));
1116
                $$name = $value;
1117
                $this->setDateTime($year, $month, $day, $hour, $minute, $second);
1118
1119
                break;
1120
1121
            case 'week':
1122
                $this->week($value);
1123
1124
                break;
1125
1126
            case 'isoWeek':
1127
                $this->isoWeek($value);
1128
1129
                break;
1130
1131
            case 'weekYear':
1132
                $this->weekYear($value);
1133
1134
                break;
1135
1136
            case 'isoWeekYear':
1137
                $this->isoWeekYear($value);
1138
1139
                break;
1140
1141
            case 'dayOfYear':
1142
                $this->addDays($value - $this->dayOfYear);
1143
1144
                return $this;
1145
1146
            case 'timestamp':
1147
                parent::setTimestamp((int) $value);
1148
1149
                break;
1150
1151
            case 'offset':
1152
                $this->setTimezone(static::safeCreateDateTimeZone($value / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR));
1153
1154
                break;
1155
1156
            case 'offsetMinutes':
1157
                $this->setTimezone(static::safeCreateDateTimeZone($value / static::MINUTES_PER_HOUR));
1158
1159
                break;
1160
1161
            case 'offsetHours':
1162
                $this->setTimezone(static::safeCreateDateTimeZone($value));
1163
1164
                break;
1165
1166
            case 'timezone':
1167
            case 'tz':
1168
                $this->setTimezone($value);
1169
1170
                break;
1171
1172
            default:
1173
                $macro = $this->getLocalMacro('set'.ucfirst($name));
1174
1175
                if ($macro) {
1176
                    $this->executeCallableWithContext($macro, $value);
1177
1178
                    break;
1179
                }
1180
1181
                if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) {
1182
                    throw new UnknownSetterException($name);
1183
                }
1184
1185
                $this->$name = $value;
1186
        }
1187
1188
        return $this;
1189
    }
1190
1191
    protected function getTranslatedFormByRegExp($baseKey, $keySuffix, $context, $subKey, $defaultValue)
1192
    {
1193
        $key = $baseKey.$keySuffix;
1194
        $standaloneKey = "${key}_standalone";
1195
        $baseTranslation = $this->getTranslationMessage($key);
1196
1197
        if ($baseTranslation instanceof Closure) {
1198
            return $baseTranslation($this, $context, $subKey) ?: $defaultValue;
1199
        }
1200
1201
        if (
1202
            $this->getTranslationMessage("$standaloneKey.$subKey") &&
1203
            (!$context || ($regExp = $this->getTranslationMessage("${baseKey}_regexp")) && !preg_match($regExp, $context))
1204
        ) {
1205
            $key = $standaloneKey;
1206
        }
1207
1208
        return $this->getTranslationMessage("$key.$subKey", null, $defaultValue);
1209
    }
1210
1211
    /**
1212
     * Get the translation of the current week day name (with context for languages with multiple forms).
1213
     *
1214
     * @param string|null $context      whole format string
1215
     * @param string      $keySuffix    "", "_short" or "_min"
1216
     * @param string|null $defaultValue default value if translation missing
1217
     *
1218
     * @return string
1219
     */
1220
    public function getTranslatedDayName($context = null, $keySuffix = '', $defaultValue = null)
1221
    {
1222
        return $this->getTranslatedFormByRegExp('weekdays', $keySuffix, $context, $this->dayOfWeek, $defaultValue ?: $this->englishDayOfWeek);
1223
    }
1224
1225
    /**
1226
     * Get the translation of the current short week day name (with context for languages with multiple forms).
1227
     *
1228
     * @param string|null $context whole format string
1229
     *
1230
     * @return string
1231
     */
1232
    public function getTranslatedShortDayName($context = null)
1233
    {
1234
        return $this->getTranslatedDayName($context, '_short', $this->shortEnglishDayOfWeek);
1235
    }
1236
1237
    /**
1238
     * Get the translation of the current abbreviated week day name (with context for languages with multiple forms).
1239
     *
1240
     * @param string|null $context whole format string
1241
     *
1242
     * @return string
1243
     */
1244
    public function getTranslatedMinDayName($context = null)
1245
    {
1246
        return $this->getTranslatedDayName($context, '_min', $this->shortEnglishDayOfWeek);
1247
    }
1248
1249
    /**
1250
     * Get the translation of the current month day name (with context for languages with multiple forms).
1251
     *
1252
     * @param string|null $context      whole format string
1253
     * @param string      $keySuffix    "" or "_short"
1254
     * @param string|null $defaultValue default value if translation missing
1255
     *
1256
     * @return string
1257
     */
1258
    public function getTranslatedMonthName($context = null, $keySuffix = '', $defaultValue = null)
1259
    {
1260
        return $this->getTranslatedFormByRegExp('months', $keySuffix, $context, $this->month - 1, $defaultValue ?: $this->englishMonth);
1261
    }
1262
1263
    /**
1264
     * Get the translation of the current short month day name (with context for languages with multiple forms).
1265
     *
1266
     * @param string|null $context whole format string
1267
     *
1268
     * @return string
1269
     */
1270
    public function getTranslatedShortMonthName($context = null)
1271
    {
1272
        return $this->getTranslatedMonthName($context, '_short', $this->shortEnglishMonth);
1273
    }
1274
1275
    /**
1276
     * Get/set the day of year.
1277
     *
1278
     * @param int|null $value new value for day of year if using as setter.
1279
     *
1280
     * @return static|int
1281
     */
1282
    public function dayOfYear($value = null)
1283
    {
1284
        $dayOfYear = $this->dayOfYear;
1285
1286
        return is_null($value) ? $dayOfYear : $this->addDays($value - $dayOfYear);
1287
    }
1288
1289
    /**
1290
     * Get/set the weekday from 0 (Sunday) to 6 (Saturday).
1291
     *
1292
     * @param int|null $value new value for weekday if using as setter.
1293
     *
1294
     * @return static|int
1295
     */
1296
    public function weekday($value = null)
1297
    {
1298
        $dayOfWeek = ($this->dayOfWeek + 7 - intval($this->getTranslationMessage('first_day_of_week') ?? 0)) % 7;
1299
1300
        return is_null($value) ? $dayOfWeek : $this->addDays($value - $dayOfWeek);
1301
    }
1302
1303
    /**
1304
     * Get/set the ISO weekday from 1 (Monday) to 7 (Sunday).
1305
     *
1306
     * @param int|null $value new value for weekday if using as setter.
1307
     *
1308
     * @return static|int
1309
     */
1310
    public function isoWeekday($value = null)
1311
    {
1312
        $dayOfWeekIso = $this->dayOfWeekIso;
1313
1314
        return is_null($value) ? $dayOfWeekIso : $this->addDays($value - $dayOfWeekIso);
1315
    }
1316
1317
    /**
1318
     * Set any unit to a new value without overflowing current other unit given.
1319
     *
1320
     * @param string $valueUnit    unit name to modify
1321
     * @param int    $value        new value for the input unit
1322
     * @param string $overflowUnit unit name to not overflow
1323
     *
1324
     * @return static
1325
     */
1326
    public function setUnitNoOverflow($valueUnit, $value, $overflowUnit)
1327
    {
1328
        try {
1329
            $original = $this->copy();
1330
            /** @var static $date */
1331
            $date = $this->$valueUnit($value);
1332
            $end = $original->copy()->endOf($overflowUnit);
1333
            $start = $original->copy()->startOf($overflowUnit);
1334
            if ($date < $start) {
1335
                $date = $date->setDateTimeFrom($start);
1336
            } elseif ($date > $end) {
1337
                $date = $date->setDateTimeFrom($end);
1338
            }
1339
1340
            return $date;
1341
        } catch (BadMethodCallException | ReflectionException $exception) {
1342
            throw new UnknownUnitException($valueUnit, 0, $exception);
1343
        }
1344
    }
1345
1346
    /**
1347
     * Add any unit to a new value without overflowing current other unit given.
1348
     *
1349
     * @param string $valueUnit    unit name to modify
1350
     * @param int    $value        amount to add to the input unit
1351
     * @param string $overflowUnit unit name to not overflow
1352
     *
1353
     * @return static
1354
     */
1355
    public function addUnitNoOverflow($valueUnit, $value, $overflowUnit)
1356
    {
1357
        return $this->setUnitNoOverflow($valueUnit, $this->$valueUnit + $value, $overflowUnit);
1358
    }
1359
1360
    /**
1361
     * Subtract any unit to a new value without overflowing current other unit given.
1362
     *
1363
     * @param string $valueUnit    unit name to modify
1364
     * @param int    $value        amount to subtract to the input unit
1365
     * @param string $overflowUnit unit name to not overflow
1366
     *
1367
     * @return static
1368
     */
1369
    public function subUnitNoOverflow($valueUnit, $value, $overflowUnit)
1370
    {
1371
        return $this->setUnitNoOverflow($valueUnit, $this->$valueUnit - $value, $overflowUnit);
1372
    }
1373
1374
    /**
1375
     * Returns the minutes offset to UTC if no arguments passed, else set the timezone with given minutes shift passed.
1376
     *
1377
     * @param int|null $offset
1378
     *
1379
     * @return int|static
1380
     */
1381
    public function utcOffset(int $offset = null)
1382
    {
1383
        if (func_num_args() < 1) {
1384
            return $this->offsetMinutes;
1385
        }
1386
1387
        return $this->setTimezone(static::safeCreateDateTimeZone($offset / static::MINUTES_PER_HOUR));
1388
    }
1389
1390
    /**
1391
     * Set the date with gregorian year, month and day numbers.
1392
     *
1393
     * @see https://php.net/manual/en/datetime.setdate.php
1394
     *
1395
     * @param int $year
1396
     * @param int $month
1397
     * @param int $day
1398
     *
1399
     * @return static
1400
     */
1401
    public function setDate($year, $month, $day)
1402
    {
1403
        return parent::setDate((int) $year, (int) $month, (int) $day);
1404
    }
1405
1406
    /**
1407
     * Set a date according to the ISO 8601 standard - using weeks and day offsets rather than specific dates.
1408
     *
1409
     * @see https://php.net/manual/en/datetime.setisodate.php
1410
     *
1411
     * @param int $year
1412
     * @param int $week
1413
     * @param int $day
1414
     *
1415
     * @return static
1416
     */
1417
    public function setISODate($year, $week, $day = 1)
1418
    {
1419
        return parent::setISODate((int) $year, (int) $week, (int) $day);
1420
    }
1421
1422
    /**
1423
     * Set the date and time all together.
1424
     *
1425
     * @param int $year
1426
     * @param int $month
1427
     * @param int $day
1428
     * @param int $hour
1429
     * @param int $minute
1430
     * @param int $second
1431
     * @param int $microseconds
1432
     *
1433
     * @return static
1434
     */
1435
    public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0)
1436
    {
1437
        return $this->setDate($year, $month, $day)->setTime((int) $hour, (int) $minute, (int) $second, (int) $microseconds);
1438
    }
1439
1440
    /**
1441
     * Resets the current time of the DateTime object to a different time.
1442
     *
1443
     * @see https://php.net/manual/en/datetime.settime.php
1444
     *
1445
     * @param int $hour
1446
     * @param int $minute
1447
     * @param int $second
1448
     * @param int $microseconds
1449
     *
1450
     * @return static
1451
     */
1452
    public function setTime($hour, $minute, $second = 0, $microseconds = 0)
1453
    {
1454
        return parent::setTime((int) $hour, (int) $minute, (int) $second, (int) $microseconds);
1455
    }
1456
1457
    /**
1458
     * Sets the date and time based on an Unix timestamp.
1459
     *
1460
     * @see https://php.net/manual/en/datetime.settimestamp.php
1461
     *
1462
     * @param int $unixtimestamp
1463
     *
1464
     * @return static
1465
     */
1466
    public function setTimestamp($unixtimestamp)
1467
    {
1468
        return parent::setTimestamp((int) $unixtimestamp);
1469
    }
1470
1471
    /**
1472
     * Set the time by time string.
1473
     *
1474
     * @param string $time
1475
     *
1476
     * @return static
1477
     */
1478
    public function setTimeFromTimeString($time)
1479
    {
1480
        if (strpos($time, ':') === false) {
1481
            $time .= ':0';
1482
        }
1483
1484
        return $this->modify($time);
1485
    }
1486
1487
    /**
1488
     * @alias setTimezone
1489
     *
1490
     * @param DateTimeZone|string $value
1491
     *
1492
     * @return static
1493
     */
1494
    public function timezone($value)
1495
    {
1496
        return $this->setTimezone($value);
1497
    }
1498
1499
    /**
1500
     * Set the timezone or returns the timezone name if no arguments passed.
1501
     *
1502
     * @param DateTimeZone|string $value
1503
     *
1504
     * @return static|string
1505
     */
1506
    public function tz($value = null)
1507
    {
1508
        if (func_num_args() < 1) {
1509
            return $this->tzName;
1510
        }
1511
1512
        return $this->setTimezone($value);
1513
    }
1514
1515
    /**
1516
     * Set the instance's timezone from a string or object.
1517
     *
1518
     * @param DateTimeZone|string $value
1519
     *
1520
     * @return static
1521
     */
1522
    public function setTimezone($value)
1523
    {
1524
        return parent::setTimezone(static::safeCreateDateTimeZone($value));
1525
    }
1526
1527
    /**
1528
     * Set the instance's timezone from a string or object and add/subtract the offset difference.
1529
     *
1530
     * @param DateTimeZone|string $value
1531
     *
1532
     * @return static
1533
     */
1534
    public function shiftTimezone($value)
1535
    {
1536
        $offset = $this->offset;
1537
        $date = $this->setTimezone($value);
1538
1539
        return $date->addRealMicroseconds(($offset - $date->offset) * static::MICROSECONDS_PER_SECOND);
1540
    }
1541
1542
    /**
1543
     * Set the instance's timezone to UTC.
1544
     *
1545
     * @return static
1546
     */
1547
    public function utc()
1548
    {
1549
        return $this->setTimezone('UTC');
1550
    }
1551
1552
    /**
1553
     * Set the year, month, and date for this instance to that of the passed instance.
1554
     *
1555
     * @param Carbon|DateTimeInterface $date now if null
1556
     *
1557
     * @return static
1558
     */
1559
    public function setDateFrom($date = null)
1560
    {
1561
        $date = $this->resolveCarbon($date);
1562
1563
        return $this->setDate($date->year, $date->month, $date->day);
1564
    }
1565
1566
    /**
1567
     * Set the hour, minute, second and microseconds for this instance to that of the passed instance.
1568
     *
1569
     * @param Carbon|DateTimeInterface $date now if null
1570
     *
1571
     * @return static
1572
     */
1573
    public function setTimeFrom($date = null)
1574
    {
1575
        $date = $this->resolveCarbon($date);
1576
1577
        return $this->setTime($date->hour, $date->minute, $date->second, $date->microsecond);
1578
    }
1579
1580
    /**
1581
     * Set the date and time for this instance to that of the passed instance.
1582
     *
1583
     * @param Carbon|DateTimeInterface $date
1584
     *
1585
     * @return static
1586
     */
1587
    public function setDateTimeFrom($date = null)
1588
    {
1589
        $date = $this->resolveCarbon($date);
1590
1591
        return $this->modify($date->rawFormat('Y-m-d H:i:s.u'));
1592
    }
1593
1594
    /**
1595
     * Get the days of the week
1596
     *
1597
     * @return array
1598
     */
1599
    public static function getDays()
1600
    {
1601
        return static::$days;
1602
    }
1603
1604
    ///////////////////////////////////////////////////////////////////
1605
    /////////////////////// WEEK SPECIAL DAYS /////////////////////////
1606
    ///////////////////////////////////////////////////////////////////
1607
1608
    private static function getFirstDayOfWeek(): int
1609
    {
1610
        return (int) static::getTranslationMessageWith(
1611
            static::getTranslator(),
1612
            'first_day_of_week'
1613
        );
1614
    }
1615
1616
    /**
1617
     * Get the first day of week
1618
     *
1619
     * @return int
1620
     */
1621
    public static function getWeekStartsAt()
1622
    {
1623
        if (static::$weekStartsAt === static::WEEK_DAY_AUTO) {
1624
            return static::getFirstDayOfWeek();
1625
        }
1626
1627
        return static::$weekStartsAt;
1628
    }
1629
1630
    /**
1631
     * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
1632
     *             Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the
1633
     *             'first_day_of_week' locale setting to change the start of week according to current locale
1634
     *             selected and implicitly the end of week.
1635
     *
1636
     * Set the first day of week
1637
     *
1638
     * @param int|string $day week start day (or 'auto' to get the first day of week from Carbon::getLocale() culture).
1639
     *
1640
     * @return void
1641
     */
1642
    public static function setWeekStartsAt($day)
1643
    {
1644
        static::$weekStartsAt = $day === static::WEEK_DAY_AUTO ? $day : max(0, (7 + $day) % 7);
1645
    }
1646
1647
    /**
1648
     * Get the last day of week
1649
     *
1650
     * @return int
1651
     */
1652
    public static function getWeekEndsAt()
1653
    {
1654
        if (static::$weekStartsAt === static::WEEK_DAY_AUTO) {
1655
            return (int) (static::DAYS_PER_WEEK - 1 + static::getFirstDayOfWeek()) % static::DAYS_PER_WEEK;
1656
        }
1657
1658
        return static::$weekEndsAt;
1659
    }
1660
1661
    /**
1662
     * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
1663
     *             Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek
1664
     *             or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the
1665
     *             start of week according to current locale selected and implicitly the end of week.
1666
     *
1667
     * Set the last day of week
1668
     *
1669
     * @param int|string $day week end day (or 'auto' to get the day before the first day of week
1670
     *                        from Carbon::getLocale() culture).
1671
     *
1672
     * @return void
1673
     */
1674
    public static function setWeekEndsAt($day)
1675
    {
1676
        static::$weekEndsAt = $day === static::WEEK_DAY_AUTO ? $day : max(0, (7 + $day) % 7);
1677
    }
1678
1679
    /**
1680
     * Get weekend days
1681
     *
1682
     * @return array
1683
     */
1684
    public static function getWeekendDays()
1685
    {
1686
        return static::$weekendDays;
1687
    }
1688
1689
    /**
1690
     * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
1691
     *             You should rather consider week-end is always saturday and sunday, and if you have some custom
1692
     *             week-end days to handle, give to those days an other name and create a macro for them:
1693
     *
1694
     *             ```
1695
     *             Carbon::macro('isDayOff', function ($date) {
1696
     *                 return $date->isSunday() || $date->isMonday();
1697
     *             });
1698
     *             Carbon::macro('isNotDayOff', function ($date) {
1699
     *                 return !$date->isDayOff();
1700
     *             });
1701
     *             if ($someDate->isDayOff()) ...
1702
     *             if ($someDate->isNotDayOff()) ...
1703
     *             // Add 5 not-off days
1704
     *             $count = 5;
1705
     *             while ($someDate->isDayOff() || ($count-- > 0)) {
1706
     *                 $someDate->addDay();
1707
     *             }
1708
     *             ```
1709
     *
1710
     * Set weekend days
1711
     *
1712
     * @param array $days
1713
     *
1714
     * @return void
1715
     */
1716
    public static function setWeekendDays($days)
1717
    {
1718
        static::$weekendDays = $days;
1719
    }
1720
1721
    /**
1722
     * Determine if a time string will produce a relative date.
1723
     *
1724
     * @param string $time
1725
     *
1726
     * @return bool true if time match a relative date, false if absolute or invalid time string
1727
     */
1728
    public static function hasRelativeKeywords($time)
1729
    {
1730
        if (!$time || strtotime($time) === false) {
1731
            return false;
1732
        }
1733
1734
        $date1 = new DateTime('2000-01-01T00:00:00Z');
1735
        $date1->modify($time);
1736
        $date2 = new DateTime('2001-12-25T00:00:00Z');
1737
        $date2->modify($time);
1738
1739
        return $date1 != $date2;
1740
    }
1741
1742
    ///////////////////////////////////////////////////////////////////
1743
    /////////////////////// STRING FORMATTING /////////////////////////
1744
    ///////////////////////////////////////////////////////////////////
1745
1746
    /**
1747
     * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
1748
     *             You should rather use UTF-8 language packages on every machine.
1749
     *
1750
     * Set if UTF8 will be used for localized date/time.
1751
     *
1752
     * @param bool $utf8
1753
     */
1754
    public static function setUtf8($utf8)
1755
    {
1756
        static::$utf8 = $utf8;
1757
    }
1758
1759
    /**
1760
     * Format the instance with the current locale.  You can set the current
1761
     * locale using setlocale() http://php.net/setlocale.
1762
     *
1763
     * @param string $format
1764
     *
1765
     * @return string
1766
     */
1767
    public function formatLocalized($format)
1768
    {
1769
        // Check for Windows to find and replace the %e modifier correctly.
1770
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
1771
            $format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format); // @codeCoverageIgnore
1772
        }
1773
1774
        $formatted = strftime($format, strtotime($this->toDateTimeString()));
1775
1776
        return static::$utf8 ? utf8_encode($formatted) : $formatted;
1777
    }
1778
1779
    /**
1780
     * Returns list of locale formats for ISO formatting.
1781
     *
1782
     * @param string|null $locale current locale used if null
1783
     *
1784
     * @return array
1785
     */
1786
    public function getIsoFormats($locale = null)
1787
    {
1788
        return [
1789
            'LT' => $this->getTranslationMessage('formats.LT', $locale, 'h:mm A'),
1790
            'LTS' => $this->getTranslationMessage('formats.LTS', $locale, 'h:mm:ss A'),
1791
            'L' => $this->getTranslationMessage('formats.L', $locale, 'MM/DD/YYYY'),
1792
            'LL' => $this->getTranslationMessage('formats.LL', $locale, 'MMMM D, YYYY'),
1793
            'LLL' => $this->getTranslationMessage('formats.LLL', $locale, 'MMMM D, YYYY h:mm A'),
1794
            'LLLL' => $this->getTranslationMessage('formats.LLLL', $locale, 'dddd, MMMM D, YYYY h:mm A'),
1795
        ];
1796
    }
1797
1798
    /**
1799
     * Returns list of calendar formats for ISO formatting.
1800
     *
1801
     * @param string|null $locale current locale used if null
1802
     *
1803
     * @return array
1804
     */
1805
    public function getCalendarFormats($locale = null)
1806
    {
1807
        return [
1808
            'sameDay' => $this->getTranslationMessage('calendar.sameDay', $locale, '[Today at] LT'),
1809
            'nextDay' => $this->getTranslationMessage('calendar.nextDay', $locale, '[Tomorrow at] LT'),
1810
            'nextWeek' => $this->getTranslationMessage('calendar.nextWeek', $locale, 'dddd [at] LT'),
1811
            'lastDay' => $this->getTranslationMessage('calendar.lastDay', $locale, '[Yesterday at] LT'),
1812
            'lastWeek' => $this->getTranslationMessage('calendar.lastWeek', $locale, '[Last] dddd [at] LT'),
1813
            'sameElse' => $this->getTranslationMessage('calendar.sameElse', $locale, 'L'),
1814
        ];
1815
    }
1816
1817
    /**
1818
     * Returns list of locale units for ISO formatting.
1819
     *
1820
     * @return array
1821
     */
1822
    public static function getIsoUnits()
1823
    {
1824
        static $units = null;
1825
1826
        if ($units === null) {
1827
            $units = [
1828
                'OD' => ['getAltNumber', ['day']],
1829
                'OM' => ['getAltNumber', ['month']],
1830
                'OY' => ['getAltNumber', ['year']],
1831
                'OH' => ['getAltNumber', ['hour']],
1832
                'Oh' => ['getAltNumber', ['h']],
1833
                'Om' => ['getAltNumber', ['minute']],
1834
                'Os' => ['getAltNumber', ['second']],
1835
                'D' => 'day',
1836
                'DD' => ['rawFormat', ['d']],
1837
                'Do' => ['ordinal', ['day', 'D']],
1838
                'd' => 'dayOfWeek',
1839
                'dd' => function (CarbonInterface $date, $originalFormat = null) {
1840
                    return $date->getTranslatedMinDayName($originalFormat);
1841
                },
1842
                'ddd' => function (CarbonInterface $date, $originalFormat = null) {
1843
                    return $date->getTranslatedShortDayName($originalFormat);
1844
                },
1845
                'dddd' => function (CarbonInterface $date, $originalFormat = null) {
1846
                    return $date->getTranslatedDayName($originalFormat);
1847
                },
1848
                'DDD' => 'dayOfYear',
1849
                'DDDD' => ['getPaddedUnit', ['dayOfYear', 3]],
1850
                'DDDo' => ['ordinal', ['dayOfYear', 'DDD']],
1851
                'e' => ['weekday', []],
1852
                'E' => 'dayOfWeekIso',
1853
                'H' => ['rawFormat', ['G']],
1854
                'HH' => ['rawFormat', ['H']],
1855
                'h' => ['rawFormat', ['g']],
1856
                'hh' => ['rawFormat', ['h']],
1857
                'k' => 'noZeroHour',
1858
                'kk' => ['getPaddedUnit', ['noZeroHour']],
1859
                'hmm' => ['rawFormat', ['gi']],
1860
                'hmmss' => ['rawFormat', ['gis']],
1861
                'Hmm' => ['rawFormat', ['Gi']],
1862
                'Hmmss' => ['rawFormat', ['Gis']],
1863
                'm' => 'minute',
1864
                'mm' => ['rawFormat', ['i']],
1865
                'a' => 'meridiem',
1866
                'A' => 'upperMeridiem',
1867
                's' => 'second',
1868
                'ss' => ['getPaddedUnit', ['second']],
1869
                'S' => function (CarbonInterface $date) {
1870
                    return strval((string) floor($date->micro / 100000));
1871
                },
1872
                'SS' => function (CarbonInterface $date) {
1873
                    return str_pad((string) floor($date->micro / 10000), 2, '0', STR_PAD_LEFT);
1874
                },
1875
                'SSS' => function (CarbonInterface $date) {
1876
                    return str_pad((string) floor($date->micro / 1000), 3, '0', STR_PAD_LEFT);
1877
                },
1878
                'SSSS' => function (CarbonInterface $date) {
1879
                    return str_pad((string) floor($date->micro / 100), 4, '0', STR_PAD_LEFT);
1880
                },
1881
                'SSSSS' => function (CarbonInterface $date) {
1882
                    return str_pad((string) floor($date->micro / 10), 5, '0', STR_PAD_LEFT);
1883
                },
1884
                'SSSSSS' => ['getPaddedUnit', ['micro', 6]],
1885
                'SSSSSSS' => function (CarbonInterface $date) {
1886
                    return str_pad((string) floor($date->micro * 10), 7, '0', STR_PAD_LEFT);
1887
                },
1888
                'SSSSSSSS' => function (CarbonInterface $date) {
1889
                    return str_pad((string) floor($date->micro * 100), 8, '0', STR_PAD_LEFT);
1890
                },
1891
                'SSSSSSSSS' => function (CarbonInterface $date) {
1892
                    return str_pad((string) floor($date->micro * 1000), 9, '0', STR_PAD_LEFT);
1893
                },
1894
                'M' => 'month',
1895
                'MM' => ['rawFormat', ['m']],
1896
                'MMM' => function (CarbonInterface $date, $originalFormat = null) {
1897
                    $month = $date->getTranslatedShortMonthName($originalFormat);
1898
                    $suffix = $date->getTranslationMessage('mmm_suffix');
1899
                    if ($suffix && $month !== $date->monthName) {
1900
                        $month .= $suffix;
1901
                    }
1902
1903
                    return $month;
1904
                },
1905
                'MMMM' => function (CarbonInterface $date, $originalFormat = null) {
1906
                    return $date->getTranslatedMonthName($originalFormat);
1907
                },
1908
                'Mo' => ['ordinal', ['month', 'M']],
1909
                'Q' => 'quarter',
1910
                'Qo' => ['ordinal', ['quarter', 'M']],
1911
                'G' => 'isoWeekYear',
1912
                'GG' => ['getPaddedUnit', ['isoWeekYear']],
1913
                'GGG' => ['getPaddedUnit', ['isoWeekYear', 3]],
1914
                'GGGG' => ['getPaddedUnit', ['isoWeekYear', 4]],
1915
                'GGGGG' => ['getPaddedUnit', ['isoWeekYear', 5]],
1916
                'g' => 'weekYear',
1917
                'gg' => ['getPaddedUnit', ['weekYear']],
1918
                'ggg' => ['getPaddedUnit', ['weekYear', 3]],
1919
                'gggg' => ['getPaddedUnit', ['weekYear', 4]],
1920
                'ggggg' => ['getPaddedUnit', ['weekYear', 5]],
1921
                'W' => 'isoWeek',
1922
                'WW' => ['getPaddedUnit', ['isoWeek']],
1923
                'Wo' => ['ordinal', ['isoWeek', 'W']],
1924
                'w' => 'week',
1925
                'ww' => ['getPaddedUnit', ['week']],
1926
                'wo' => ['ordinal', ['week', 'w']],
1927
                'x' => ['valueOf', []],
1928
                'X' => 'timestamp',
1929
                'Y' => 'year',
1930
                'YY' => ['rawFormat', ['y']],
1931
                'YYYY' => ['getPaddedUnit', ['year', 4]],
1932
                'YYYYY' => ['getPaddedUnit', ['year', 5]],
1933
                'YYYYYY' => function (CarbonInterface $date) {
1934
                    return ($date->year < 0 ? '' : '+').$date->getPaddedUnit('year', 6);
1935
                },
1936
                'z' => ['rawFormat', ['T']],
1937
                'zz' => 'tzName',
1938
                'Z' => ['getOffsetString', []],
1939
                'ZZ' => ['getOffsetString', ['']],
1940
            ];
1941
        }
1942
1943
        return $units;
1944
    }
1945
1946
    /**
1947
     * Returns a unit of the instance padded with 0 by default or any other string if specified.
1948
     *
1949
     * @param string $unit      Carbon unit name
1950
     * @param int    $length    Length of the output (2 by default)
1951
     * @param string $padString String to use for padding ("0" by default)
1952
     * @param int    $padType   Side(s) to pad (STR_PAD_LEFT by default)
1953
     *
1954
     * @return string
1955
     */
1956
    public function getPaddedUnit($unit, $length = 2, $padString = '0', $padType = STR_PAD_LEFT)
1957
    {
1958
        return ($this->$unit < 0 ? '-' : '').str_pad((string) abs($this->$unit), $length, $padString, $padType);
1959
    }
1960
1961
    /**
1962
     * Return a property with its ordinal.
1963
     *
1964
     * @param string      $key
1965
     * @param string|null $period
1966
     *
1967
     * @return string
1968
     */
1969
    public function ordinal(string $key, string $period = null): string
1970
    {
1971
        $number = $this->$key;
1972
        $result = $this->translate('ordinal', [
1973
            ':number' => $number,
1974
            ':period' => $period,
1975
        ]);
1976
1977
        return strval($result === 'ordinal' ? $number : $result);
1978
    }
1979
1980
    /**
1981
     * Return the meridiem of the current time in the current locale.
1982
     *
1983
     * @param bool $isLower if true, returns lowercase variant if available in the current locale.
1984
     *
1985
     * @return string
1986
     */
1987
    public function meridiem(bool $isLower = false): string
1988
    {
1989
        $hour = $this->hour;
1990
        $index = $hour < 12 ? 0 : 1;
1991
1992
        if ($isLower) {
1993
            $key = 'meridiem.'.($index + 2);
1994
            $result = $this->translate($key);
1995
1996
            if ($result !== $key) {
1997
                return $result;
1998
            }
1999
        }
2000
2001
        $key = "meridiem.$index";
2002
        $result = $this->translate($key);
2003
        if ($result === $key) {
2004
            $result = $this->translate('meridiem', [
2005
                ':hour' => $this->hour,
2006
                ':minute' => $this->minute,
2007
                ':isLower' => $isLower,
2008
            ]);
2009
2010
            if ($result === 'meridiem') {
2011
                return $isLower ? $this->latinMeridiem : $this->latinUpperMeridiem;
2012
            }
2013
        } elseif ($isLower) {
2014
            $result = mb_strtolower($result);
2015
        }
2016
2017
        return $result;
2018
    }
2019
2020
    /**
2021
     * Returns the alternative number for a given date property if available in the current locale.
2022
     *
2023
     * @param string $key date property
2024
     *
2025
     * @return string
2026
     */
2027
    public function getAltNumber(string $key): string
2028
    {
2029
        return $this->translateNumber(strlen($key) > 1 ? $this->$key : $this->rawFormat('h'));
2030
    }
2031
2032
    /**
2033
     * Format in the current language using ISO replacement patterns.
2034
     *
2035
     * @param string      $format
2036
     * @param string|null $originalFormat provide context if a chunk has been passed alone
2037
     *
2038
     * @return string
2039
     */
2040
    public function isoFormat(string $format, string $originalFormat = null): string
2041
    {
2042
        $result = '';
2043
        $length = mb_strlen($format);
2044
        $originalFormat = $originalFormat ?: $format;
2045
        $inEscaped = false;
2046
        $formats = null;
2047
        $units = null;
2048
2049
        for ($i = 0; $i < $length; $i++) {
2050
            $char = mb_substr($format, $i, 1);
2051
2052
            if ($char === '\\') {
2053
                $result .= mb_substr($format, ++$i, 1);
2054
2055
                continue;
2056
            }
2057
2058
            if ($char === '[' && !$inEscaped) {
2059
                $inEscaped = true;
2060
2061
                continue;
2062
            }
2063
2064
            if ($char === ']' && $inEscaped) {
2065
                $inEscaped = false;
2066
2067
                continue;
2068
            }
2069
2070
            if ($inEscaped) {
2071
                $result .= $char;
2072
2073
                continue;
2074
            }
2075
2076
            $input = mb_substr($format, $i);
2077
2078
            if (preg_match('/^(LTS|LT|[Ll]{1,4})/', $input, $match)) {
2079
                if ($formats === null) {
2080
                    $formats = $this->getIsoFormats();
2081
                }
2082
2083
                $code = $match[0];
2084
                $sequence = $formats[$code] ?? preg_replace_callback(
2085
                    '/MMMM|MM|DD|dddd/',
2086
                    function ($code) {
2087
                        return mb_substr($code[0], 1);
2088
                    },
2089
                    $formats[strtoupper($code)] ?? ''
2090
                );
2091
                $rest = mb_substr($format, $i + mb_strlen($code));
2092
                $format = mb_substr($format, 0, $i).$sequence.$rest;
2093
                $length = mb_strlen($format);
2094
                $input = $sequence.$rest;
2095
            }
2096
2097
            if (preg_match('/^'.CarbonInterface::ISO_FORMAT_REGEXP.'/', $input, $match)) {
2098
                $code = $match[0];
2099
2100
                if ($units === null) {
2101
                    $units = static::getIsoUnits();
2102
                }
2103
2104
                $sequence = $units[$code] ?? '';
2105
2106
                if ($sequence instanceof Closure) {
2107
                    $sequence = $sequence($this, $originalFormat);
2108
                } elseif (is_array($sequence)) {
2109
                    try {
2110
                        $sequence = $this->{$sequence[0]}(...$sequence[1]);
2111
                    } catch (ReflectionException | InvalidArgumentException | BadMethodCallException $e) {
2112
                        $sequence = '';
2113
                    }
2114
                } elseif (is_string($sequence)) {
2115
                    $sequence = $this->$sequence ?? $code;
2116
                }
2117
2118
                $format = mb_substr($format, 0, $i).$sequence.mb_substr($format, $i + mb_strlen($code));
2119
                $i += mb_strlen("$sequence") - 1;
2120
                $length = mb_strlen($format);
2121
                $char = $sequence;
2122
            }
2123
2124
            $result .= $char;
2125
        }
2126
2127
        return $result;
2128
    }
2129
2130
    /**
2131
     * List of replacements from date() format to isoFormat().
2132
     *
2133
     * @return array
2134
     */
2135
    public static function getFormatsToIsoReplacements()
2136
    {
2137
        static $replacements = null;
2138
2139
        if ($replacements === null) {
2140
            $replacements = [
2141
                'd' => true,
2142
                'D' => 'ddd',
2143
                'j' => true,
2144
                'l' => 'dddd',
2145
                'N' => true,
2146
                'S' => function ($date) {
2147
                    $day = $date->rawFormat('j');
2148
2149
                    return str_replace("$day", '', $date->isoFormat('Do'));
2150
                },
2151
                'w' => true,
2152
                'z' => true,
2153
                'W' => true,
2154
                'F' => 'MMMM',
2155
                'm' => true,
2156
                'M' => 'MMM',
2157
                'n' => true,
2158
                't' => true,
2159
                'L' => true,
2160
                'o' => true,
2161
                'Y' => true,
2162
                'y' => true,
2163
                'a' => 'a',
2164
                'A' => 'A',
2165
                'B' => true,
2166
                'g' => true,
2167
                'G' => true,
2168
                'h' => true,
2169
                'H' => true,
2170
                'i' => true,
2171
                's' => true,
2172
                'u' => true,
2173
                'v' => true,
2174
                'E' => true,
2175
                'I' => true,
2176
                'O' => true,
2177
                'P' => true,
2178
                'Z' => true,
2179
                'c' => true,
2180
                'r' => true,
2181
                'U' => true,
2182
            ];
2183
        }
2184
2185
        return $replacements;
2186
    }
2187
2188
    /**
2189
     * Format as ->format() do (using date replacements patterns from http://php.net/manual/fr/function.date.php)
2190
     * but translate words whenever possible (months, day names, etc.) using the current locale.
2191
     *
2192
     * @param string $format
2193
     *
2194
     * @return string
2195
     */
2196
    public function translatedFormat(string $format): string
2197
    {
2198
        $replacements = static::getFormatsToIsoReplacements();
2199
        $context = '';
2200
        $isoFormat = '';
2201
        $length = mb_strlen($format);
2202
2203
        for ($i = 0; $i < $length; $i++) {
2204
            $char = mb_substr($format, $i, 1);
2205
2206
            if ($char === '\\') {
2207
                $replacement = mb_substr($format, $i, 2);
2208
                $isoFormat .= $replacement;
2209
                $i++;
2210
2211
                continue;
2212
            }
2213
2214
            if (!isset($replacements[$char])) {
2215
                $replacement = preg_match('/^[A-Za-z]$/', $char) ? "\\$char" : $char;
2216
                $isoFormat .= $replacement;
2217
                $context .= $replacement;
2218
2219
                continue;
2220
            }
2221
2222
            $replacement = $replacements[$char];
2223
2224
            if ($replacement === true) {
2225
                static $contextReplacements = null;
2226
2227
                if ($contextReplacements === null) {
2228
                    $contextReplacements = [
2229
                        'm' => 'MM',
2230
                        'd' => 'DD',
2231
                        't' => 'D',
2232
                        'j' => 'D',
2233
                        'N' => 'e',
2234
                        'w' => 'e',
2235
                        'n' => 'M',
2236
                        'o' => 'YYYY',
2237
                        'Y' => 'YYYY',
2238
                        'y' => 'YY',
2239
                        'g' => 'h',
2240
                        'G' => 'H',
2241
                        'h' => 'hh',
2242
                        'H' => 'HH',
2243
                        'i' => 'mm',
2244
                        's' => 'ss',
2245
                    ];
2246
                }
2247
2248
                $isoFormat .= '['.$this->rawFormat($char).']';
2249
                $context .= $contextReplacements[$char] ?? ' ';
2250
2251
                continue;
2252
            }
2253
2254
            if ($replacement instanceof Closure) {
2255
                $replacement = '['.$replacement($this).']';
2256
                $isoFormat .= $replacement;
2257
                $context .= $replacement;
2258
2259
                continue;
2260
            }
2261
2262
            $isoFormat .= $replacement;
2263
            $context .= $replacement;
2264
        }
2265
2266
        return $this->isoFormat($isoFormat, $context);
2267
    }
2268
2269
    /**
2270
     * Returns the offset hour and minute formatted with +/- and a given separator (":" by default).
2271
     * For example, if the time zone is 9 hours 30 minutes, you'll get "+09:30", with "@@" as first
2272
     * argument, "+09@@30", with "" as first argument, "+0930". Negative offset will return something
2273
     * like "-12:00".
2274
     *
2275
     * @param string $separator string to place between hours and minutes (":" by default)
2276
     *
2277
     * @return string
2278
     */
2279
    public function getOffsetString($separator = ':')
2280
    {
2281
        $second = $this->getOffset();
2282
        $symbol = $second < 0 ? '-' : '+';
2283
        $minute = abs($second) / static::SECONDS_PER_MINUTE;
2284
        $hour = str_pad((string) floor($minute / static::MINUTES_PER_HOUR), 2, '0', STR_PAD_LEFT);
2285
        $minute = str_pad((string) ($minute % static::MINUTES_PER_HOUR), 2, '0', STR_PAD_LEFT);
2286
2287
        return "$symbol$hour$separator$minute";
2288
    }
2289
2290
    protected static function executeStaticCallable($macro, ...$parameters)
2291
    {
2292
        return static::bindMacroContext(null, function () use (&$macro, &$parameters) {
2293
            if ($macro instanceof Closure) {
2294
                $boundMacro = @Closure::bind($macro, null, static::class);
2295
2296
                return call_user_func_array($boundMacro ?: $macro, $parameters);
2297
            }
2298
2299
            return call_user_func_array($macro, $parameters);
2300
        });
2301
    }
2302
2303
    /**
2304
     * Dynamically handle calls to the class.
2305
     *
2306
     * @param string $method     magic method name called
2307
     * @param array  $parameters parameters list
2308
     *
2309
     * @throws BadMethodCallException
2310
     *
2311
     * @return mixed
2312
     */
2313
    public static function __callStatic($method, $parameters)
2314
    {
2315
        if (!static::hasMacro($method)) {
2316
            foreach (static::getGenericMacros() as $callback) {
2317
                try {
2318
                    return static::executeStaticCallable($callback, $method, ...$parameters);
2319
                } catch (BadMethodCallException $exception) {
2320
                    continue;
2321
                }
2322
            }
2323
            if (static::isStrictModeEnabled()) {
2324
                throw new UnknownMethodException(sprintf('%s::%s', static::class, $method));
2325
            }
2326
2327
            return null;
2328
        }
2329
2330
        return static::executeStaticCallable(static::$globalMacros[$method], ...$parameters);
2331
    }
2332
2333
    /**
2334
     * Set specified unit to new given value.
2335
     *
2336
     * @param string $unit  year, month, day, hour, minute, second or microsecond
2337
     * @param int    $value new value for given unit
2338
     *
2339
     * @return static
2340
     */
2341
    public function setUnit($unit, $value = null)
2342
    {
2343
        $unit = static::singularUnit($unit);
2344
        $dateUnits = ['year', 'month', 'day'];
2345
        if (in_array($unit, $dateUnits)) {
2346
            return $this->setDate(...array_map(function ($name) use ($unit, $value) {
2347
                return (int) ($name === $unit ? $value : $this->$name);
2348
            }, $dateUnits));
2349
        }
2350
2351
        $units = ['hour', 'minute', 'second', 'micro'];
2352
        if ($unit === 'millisecond' || $unit === 'milli') {
2353
            $value *= 1000;
2354
            $unit = 'micro';
2355
        } elseif ($unit === 'microsecond') {
2356
            $unit = 'micro';
2357
        }
2358
2359
        return $this->setTime(...array_map(function ($name) use ($unit, $value) {
2360
            return (int) ($name === $unit ? $value : $this->$name);
2361
        }, $units));
2362
    }
2363
2364
    /**
2365
     * Returns standardized singular of a given singular/plural unit name (in English).
2366
     *
2367
     * @param string $unit
2368
     *
2369
     * @return string
2370
     */
2371
    public static function singularUnit(string $unit): string
2372
    {
2373
        $unit = rtrim(mb_strtolower($unit), 's');
2374
2375
        if ($unit === 'centurie') {
2376
            return 'century';
2377
        }
2378
2379
        if ($unit === 'millennia') {
2380
            return 'millennium';
2381
        }
2382
2383
        return $unit;
2384
    }
2385
2386
    /**
2387
     * Returns standardized plural of a given singular/plural unit name (in English).
2388
     *
2389
     * @param string $unit
2390
     *
2391
     * @return string
2392
     */
2393
    public static function pluralUnit(string $unit): string
2394
    {
2395
        $unit = rtrim(strtolower($unit), 's');
2396
2397
        if ($unit === 'century') {
2398
            return 'centuries';
2399
        }
2400
2401
        if ($unit === 'millennium' || $unit === 'millennia') {
2402
            return 'millennia';
2403
        }
2404
2405
        return "${unit}s";
2406
    }
2407
2408
    protected function executeCallable($macro, ...$parameters)
2409
    {
2410
        if ($macro instanceof Closure) {
2411
            $boundMacro = @$macro->bindTo($this, static::class) ?: @$macro->bindTo(null, static::class);
2412
2413
            return call_user_func_array($boundMacro ?: $macro, $parameters);
2414
        }
2415
2416
        return call_user_func_array($macro, $parameters);
2417
    }
2418
2419
    protected function executeCallableWithContext($macro, ...$parameters)
2420
    {
2421
        return static::bindMacroContext($this, function () use (&$macro, &$parameters) {
2422
            return $this->executeCallable($macro, ...$parameters);
2423
        });
2424
    }
2425
2426
    protected static function getGenericMacros()
2427
    {
2428
        foreach (static::$globalGenericMacros as $list) {
2429
            foreach ($list as $macro) {
2430
                yield $macro;
2431
            }
2432
        }
2433
    }
2434
2435
    /**
2436
     * Dynamically handle calls to the class.
2437
     *
2438
     * @param string $method     magic method name called
2439
     * @param array  $parameters parameters list
2440
     *
2441
     * @throws UnknownMethodException|BadMethodCallException|ReflectionException|Throwable
2442
     *
2443
     * @return mixed
2444
     */
2445
    public function __call($method, $parameters)
2446
    {
2447
        $diffSizes = [
2448
            // @mode diffForHumans
2449
            'short' => true,
2450
            // @mode diffForHumans
2451
            'long' => false,
2452
        ];
2453
        $diffSyntaxModes = [
2454
            // @call diffForHumans
2455
            'Absolute' => CarbonInterface::DIFF_ABSOLUTE,
2456
            // @call diffForHumans
2457
            'Relative' => CarbonInterface::DIFF_RELATIVE_AUTO,
2458
            // @call diffForHumans
2459
            'RelativeToNow' => CarbonInterface::DIFF_RELATIVE_TO_NOW,
2460
            // @call diffForHumans
2461
            'RelativeToOther' => CarbonInterface::DIFF_RELATIVE_TO_OTHER,
2462
        ];
2463
        $sizePattern = implode('|', array_keys($diffSizes));
2464
        $syntaxPattern = implode('|', array_keys($diffSyntaxModes));
2465
2466
        if (preg_match("/^(?<size>$sizePattern)(?<syntax>$syntaxPattern)DiffForHumans$/", $method, $match)) {
2467
            $dates = array_filter($parameters, function ($parameter) {
2468
                return $parameter instanceof DateTimeInterface;
2469
            });
2470
            $other = null;
2471
2472
            if (count($dates)) {
2473
                $key = key($dates);
2474
                $other = current($dates);
2475
                array_splice($parameters, $key, 1);
2476
            }
2477
2478
            return $this->diffForHumans($other, $diffSyntaxModes[$match['syntax']], $diffSizes[$match['size']], ...$parameters);
2479
        }
2480
2481
        $roundedValue = $this->callRoundMethod($method, $parameters);
2482
2483
        if ($roundedValue !== null) {
2484
            return $roundedValue;
2485
        }
2486
2487
        $unit = rtrim($method, 's');
2488
2489
        if (substr($unit, 0, 2) === 'is') {
2490
            $word = substr($unit, 2);
2491
2492
            if (in_array($word, static::$days)) {
2493
                return $this->isDayOfWeek($word);
2494
            }
2495
2496
            switch ($word) {
2497
                // @call is Check if the current instance has UTC timezone. (Both isUtc and isUTC cases are valid.)
2498
                case 'Utc':
2499
                case 'UTC':
2500
                    return $this->utc;
2501
                // @call is Check if the current instance has non-UTC timezone.
2502
                case 'Local':
2503
                    return $this->local;
2504
                // @call is Check if the current instance is a valid date.
2505
                case 'Valid':
2506
                    return $this->year !== 0;
2507
                // @call is Check if the current instance is in a daylight saving time.
2508
                case 'DST':
2509
                    return $this->dst;
2510
            }
2511
        }
2512
2513
        $action = substr($unit, 0, 3);
2514
        $overflow = null;
2515
2516
        if ($action === 'set') {
2517
            $unit = strtolower(substr($unit, 3));
2518
        }
2519
2520
        if (in_array($unit, static::$units)) {
2521
            return $this->setUnit($unit, ...$parameters);
2522
        }
2523
2524
        if ($action === 'add' || $action === 'sub') {
2525
            $unit = substr($unit, 3);
2526
2527
            if (substr($unit, 0, 4) === 'Real') {
2528
                $unit = static::singularUnit(substr($unit, 4));
2529
2530
                return $this->{"${action}RealUnit"}($unit, ...$parameters);
2531
            }
2532
2533
            if (preg_match('/^(Month|Quarter|Year|Decade|Century|Centurie|Millennium|Millennia)s?(No|With|Without|WithNo)Overflow$/', $unit, $match)) {
2534
                $unit = $match[1];
2535
                $overflow = $match[2] === 'With';
2536
            }
2537
2538
            $unit = static::singularUnit($unit);
2539
        }
2540
2541
        if (static::isModifiableUnit($unit)) {
2542
            return $this->{"${action}Unit"}($unit, $parameters[0] ?? 1, $overflow);
2543
        }
2544
2545
        $sixFirstLetters = substr($unit, 0, 6);
2546
        $factor = -1;
2547
2548
        if ($sixFirstLetters === 'isLast') {
2549
            $sixFirstLetters = 'isNext';
2550
            $factor = 1;
2551
        }
2552
2553
        if ($sixFirstLetters === 'isNext') {
2554
            $lowerUnit = strtolower(substr($unit, 6));
2555
2556
            if (static::isModifiableUnit($lowerUnit)) {
2557
                return $this->copy()->addUnit($lowerUnit, $factor, false)->isSameUnit($lowerUnit, ...$parameters);
2558
            }
2559
        }
2560
2561
        if ($sixFirstLetters === 'isSame') {
2562
            try {
2563
                return $this->isSameUnit(strtolower(substr($unit, 6)), ...$parameters);
2564
            } catch (BadComparisonUnitException $exception) {
2565
                // Try next
2566
            }
2567
        }
2568
2569
        if (substr($unit, 0, 9) === 'isCurrent') {
2570
            try {
2571
                return $this->isCurrentUnit(strtolower(substr($unit, 9)));
2572
            } catch (BadComparisonUnitException | BadMethodCallException $exception) {
2573
                // Try next
2574
            }
2575
        }
2576
2577
        if (substr($method, -5) === 'Until') {
2578
            try {
2579
                $unit = static::singularUnit(substr($method, 0, -5));
2580
2581
                return $this->range($parameters[0] ?? $this, $parameters[1] ?? 1, $unit);
2582
            } catch (InvalidArgumentException $exception) {
2583
                // Try macros
2584
            }
2585
        }
2586
2587
        return static::bindMacroContext($this, function () use (&$method, &$parameters) {
2588
            $macro = $this->getLocalMacro($method);
2589
2590
            if (!$macro) {
2591
                foreach ([$this->localGenericMacros ?: [], static::getGenericMacros()] as $list) {
2592
                    foreach ($list as $callback) {
2593
                        try {
2594
                            return $this->executeCallable($callback, $method, ...$parameters);
2595
                        } catch (BadMethodCallException $exception) {
2596
                            continue;
2597
                        }
2598
                    }
2599
                }
2600
2601
                if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) {
2602
                    throw new UnknownMethodException($method);
2603
                }
2604
2605
                return null;
2606
            }
2607
2608
            return $this->executeCallable($macro, ...$parameters);
2609
        });
2610
    }
2611
}
2612