briannesbitt / Carbon
1
<?php
2

3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
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;
12

13
use Carbon\Exceptions\BadFluentConstructorException;
14
use Carbon\Exceptions\BadFluentSetterException;
15
use Carbon\Exceptions\InvalidCastException;
16
use Carbon\Exceptions\InvalidIntervalException;
17
use Carbon\Exceptions\ParseErrorException;
18
use Carbon\Exceptions\UnitNotConfiguredException;
19
use Carbon\Exceptions\UnknownGetterException;
20
use Carbon\Exceptions\UnknownSetterException;
21
use Carbon\Exceptions\UnknownUnitException;
22
use Carbon\Traits\IntervalRounding;
23
use Carbon\Traits\IntervalStep;
24
use Carbon\Traits\Mixin;
25
use Carbon\Traits\Options;
26
use Closure;
27
use DateInterval;
28
use Exception;
29
use ReflectionException;
30
use Symfony\Contracts\Translation\TranslatorInterface;
31
use Throwable;
32

33
/**
34
 * A simple API extension for DateInterval.
35
 * The implementation provides helpers to handle weeks but only days are saved.
36
 * Weeks are calculated based on the total days of the current instance.
37
 *
38
 * @property int $years Total years of the current interval.
39
 * @property int $months Total months of the current interval.
40
 * @property int $weeks Total weeks of the current interval calculated from the days.
41
 * @property int $dayz Total days of the current interval (weeks * 7 + days).
42
 * @property int $hours Total hours of the current interval.
43
 * @property int $minutes Total minutes of the current interval.
44
 * @property int $seconds Total seconds of the current interval.
45
 * @property int $microseconds Total microseconds of the current interval.
46
 * @property int $milliseconds Total microseconds of the current interval.
47
 * @property int $microExcludeMilli Remaining microseconds without the milliseconds.
48
 * @property int $dayzExcludeWeeks Total days remaining in the final week of the current instance (days % 7).
49
 * @property int $daysExcludeWeeks alias of dayzExcludeWeeks
50
 * @property-read float $totalYears Number of years equivalent to the interval.
51
 * @property-read float $totalMonths Number of months equivalent to the interval.
52
 * @property-read float $totalWeeks Number of weeks equivalent to the interval.
53
 * @property-read float $totalDays Number of days equivalent to the interval.
54
 * @property-read float $totalDayz Alias for totalDays.
55
 * @property-read float $totalHours Number of hours equivalent to the interval.
56
 * @property-read float $totalMinutes Number of minutes equivalent to the interval.
57
 * @property-read float $totalSeconds Number of seconds equivalent to the interval.
58
 * @property-read float $totalMilliseconds Number of milliseconds equivalent to the interval.
59
 * @property-read float $totalMicroseconds Number of microseconds equivalent to the interval.
60
 * @property-read string $locale locale of the current instance
61
 *
62
 * @method static CarbonInterval years($years = 1) Create instance specifying a number of years or modify the number of years if called on an instance.
63
 * @method static CarbonInterval year($years = 1) Alias for years()
64
 * @method static CarbonInterval months($months = 1) Create instance specifying a number of months or modify the number of months if called on an instance.
65
 * @method static CarbonInterval month($months = 1) Alias for months()
66
 * @method static CarbonInterval weeks($weeks = 1) Create instance specifying a number of weeks or modify the number of weeks if called on an instance.
67
 * @method static CarbonInterval week($weeks = 1) Alias for weeks()
68
 * @method static CarbonInterval days($days = 1) Create instance specifying a number of days or modify the number of days if called on an instance.
69
 * @method static CarbonInterval dayz($days = 1) Alias for days()
70
 * @method static CarbonInterval daysExcludeWeeks($days = 1) Create instance specifying a number of days or modify the number of days (keeping the current number of weeks) if called on an instance.
71
 * @method static CarbonInterval dayzExcludeWeeks($days = 1) Alias for daysExcludeWeeks()
72
 * @method static CarbonInterval day($days = 1) Alias for days()
73
 * @method static CarbonInterval hours($hours = 1) Create instance specifying a number of hours or modify the number of hours if called on an instance.
74
 * @method static CarbonInterval hour($hours = 1) Alias for hours()
75
 * @method static CarbonInterval minutes($minutes = 1) Create instance specifying a number of minutes or modify the number of minutes if called on an instance.
76
 * @method static CarbonInterval minute($minutes = 1) Alias for minutes()
77
 * @method static CarbonInterval seconds($seconds = 1) Create instance specifying a number of seconds or modify the number of seconds if called on an instance.
78
 * @method static CarbonInterval second($seconds = 1) Alias for seconds()
79
 * @method static CarbonInterval milliseconds($milliseconds = 1) Create instance specifying a number of milliseconds or modify the number of milliseconds if called on an instance.
80
 * @method static CarbonInterval millisecond($milliseconds = 1) Alias for milliseconds()
81
 * @method static CarbonInterval microseconds($microseconds = 1) Create instance specifying a number of microseconds or modify the number of microseconds if called on an instance.
82
 * @method static CarbonInterval microsecond($microseconds = 1) Alias for microseconds()
83
 * @method $this roundYear(float $precision = 1, string $function = "round") Round the current instance year with given precision using the given function.
84
 * @method $this roundYears(float $precision = 1, string $function = "round") Round the current instance year with given precision using the given function.
85
 * @method $this floorYear(float $precision = 1) Truncate the current instance year with given precision.
86
 * @method $this floorYears(float $precision = 1) Truncate the current instance year with given precision.
87
 * @method $this ceilYear(float $precision = 1) Ceil the current instance year with given precision.
88
 * @method $this ceilYears(float $precision = 1) Ceil the current instance year with given precision.
89
 * @method $this roundMonth(float $precision = 1, string $function = "round") Round the current instance month with given precision using the given function.
90
 * @method $this roundMonths(float $precision = 1, string $function = "round") Round the current instance month with given precision using the given function.
91
 * @method $this floorMonth(float $precision = 1) Truncate the current instance month with given precision.
92
 * @method $this floorMonths(float $precision = 1) Truncate the current instance month with given precision.
93
 * @method $this ceilMonth(float $precision = 1) Ceil the current instance month with given precision.
94
 * @method $this ceilMonths(float $precision = 1) Ceil the current instance month with given precision.
95
 * @method $this roundWeek(float $precision = 1, string $function = "round") Round the current instance day with given precision using the given function.
96
 * @method $this roundWeeks(float $precision = 1, string $function = "round") Round the current instance day with given precision using the given function.
97
 * @method $this floorWeek(float $precision = 1) Truncate the current instance day with given precision.
98
 * @method $this floorWeeks(float $precision = 1) Truncate the current instance day with given precision.
99
 * @method $this ceilWeek(float $precision = 1) Ceil the current instance day with given precision.
100
 * @method $this ceilWeeks(float $precision = 1) Ceil the current instance day with given precision.
101
 * @method $this roundDay(float $precision = 1, string $function = "round") Round the current instance day with given precision using the given function.
102
 * @method $this roundDays(float $precision = 1, string $function = "round") Round the current instance day with given precision using the given function.
103
 * @method $this floorDay(float $precision = 1) Truncate the current instance day with given precision.
104
 * @method $this floorDays(float $precision = 1) Truncate the current instance day with given precision.
105
 * @method $this ceilDay(float $precision = 1) Ceil the current instance day with given precision.
106
 * @method $this ceilDays(float $precision = 1) Ceil the current instance day with given precision.
107
 * @method $this roundHour(float $precision = 1, string $function = "round") Round the current instance hour with given precision using the given function.
108
 * @method $this roundHours(float $precision = 1, string $function = "round") Round the current instance hour with given precision using the given function.
109
 * @method $this floorHour(float $precision = 1) Truncate the current instance hour with given precision.
110
 * @method $this floorHours(float $precision = 1) Truncate the current instance hour with given precision.
111
 * @method $this ceilHour(float $precision = 1) Ceil the current instance hour with given precision.
112
 * @method $this ceilHours(float $precision = 1) Ceil the current instance hour with given precision.
113
 * @method $this roundMinute(float $precision = 1, string $function = "round") Round the current instance minute with given precision using the given function.
114
 * @method $this roundMinutes(float $precision = 1, string $function = "round") Round the current instance minute with given precision using the given function.
115
 * @method $this floorMinute(float $precision = 1) Truncate the current instance minute with given precision.
116
 * @method $this floorMinutes(float $precision = 1) Truncate the current instance minute with given precision.
117
 * @method $this ceilMinute(float $precision = 1) Ceil the current instance minute with given precision.
118
 * @method $this ceilMinutes(float $precision = 1) Ceil the current instance minute with given precision.
119
 * @method $this roundSecond(float $precision = 1, string $function = "round") Round the current instance second with given precision using the given function.
120
 * @method $this roundSeconds(float $precision = 1, string $function = "round") Round the current instance second with given precision using the given function.
121
 * @method $this floorSecond(float $precision = 1) Truncate the current instance second with given precision.
122
 * @method $this floorSeconds(float $precision = 1) Truncate the current instance second with given precision.
123
 * @method $this ceilSecond(float $precision = 1) Ceil the current instance second with given precision.
124
 * @method $this ceilSeconds(float $precision = 1) Ceil the current instance second with given precision.
125
 * @method $this roundMillennium(float $precision = 1, string $function = "round") Round the current instance millennium with given precision using the given function.
126
 * @method $this roundMillennia(float $precision = 1, string $function = "round") Round the current instance millennium with given precision using the given function.
127
 * @method $this floorMillennium(float $precision = 1) Truncate the current instance millennium with given precision.
128
 * @method $this floorMillennia(float $precision = 1) Truncate the current instance millennium with given precision.
129
 * @method $this ceilMillennium(float $precision = 1) Ceil the current instance millennium with given precision.
130
 * @method $this ceilMillennia(float $precision = 1) Ceil the current instance millennium with given precision.
131
 * @method $this roundCentury(float $precision = 1, string $function = "round") Round the current instance century with given precision using the given function.
132
 * @method $this roundCenturies(float $precision = 1, string $function = "round") Round the current instance century with given precision using the given function.
133
 * @method $this floorCentury(float $precision = 1) Truncate the current instance century with given precision.
134
 * @method $this floorCenturies(float $precision = 1) Truncate the current instance century with given precision.
135
 * @method $this ceilCentury(float $precision = 1) Ceil the current instance century with given precision.
136
 * @method $this ceilCenturies(float $precision = 1) Ceil the current instance century with given precision.
137
 * @method $this roundDecade(float $precision = 1, string $function = "round") Round the current instance decade with given precision using the given function.
138
 * @method $this roundDecades(float $precision = 1, string $function = "round") Round the current instance decade with given precision using the given function.
139
 * @method $this floorDecade(float $precision = 1) Truncate the current instance decade with given precision.
140
 * @method $this floorDecades(float $precision = 1) Truncate the current instance decade with given precision.
141
 * @method $this ceilDecade(float $precision = 1) Ceil the current instance decade with given precision.
142
 * @method $this ceilDecades(float $precision = 1) Ceil the current instance decade with given precision.
143
 * @method $this roundQuarter(float $precision = 1, string $function = "round") Round the current instance quarter with given precision using the given function.
144
 * @method $this roundQuarters(float $precision = 1, string $function = "round") Round the current instance quarter with given precision using the given function.
145
 * @method $this floorQuarter(float $precision = 1) Truncate the current instance quarter with given precision.
146
 * @method $this floorQuarters(float $precision = 1) Truncate the current instance quarter with given precision.
147
 * @method $this ceilQuarter(float $precision = 1) Ceil the current instance quarter with given precision.
148
 * @method $this ceilQuarters(float $precision = 1) Ceil the current instance quarter with given precision.
149
 * @method $this roundMillisecond(float $precision = 1, string $function = "round") Round the current instance millisecond with given precision using the given function.
150
 * @method $this roundMilliseconds(float $precision = 1, string $function = "round") Round the current instance millisecond with given precision using the given function.
151
 * @method $this floorMillisecond(float $precision = 1) Truncate the current instance millisecond with given precision.
152
 * @method $this floorMilliseconds(float $precision = 1) Truncate the current instance millisecond with given precision.
153
 * @method $this ceilMillisecond(float $precision = 1) Ceil the current instance millisecond with given precision.
154
 * @method $this ceilMilliseconds(float $precision = 1) Ceil the current instance millisecond with given precision.
155
 * @method $this roundMicrosecond(float $precision = 1, string $function = "round") Round the current instance microsecond with given precision using the given function.
156
 * @method $this roundMicroseconds(float $precision = 1, string $function = "round") Round the current instance microsecond with given precision using the given function.
157
 * @method $this floorMicrosecond(float $precision = 1) Truncate the current instance microsecond with given precision.
158
 * @method $this floorMicroseconds(float $precision = 1) Truncate the current instance microsecond with given precision.
159
 * @method $this ceilMicrosecond(float $precision = 1) Ceil the current instance microsecond with given precision.
160
 * @method $this ceilMicroseconds(float $precision = 1) Ceil the current instance microsecond with given precision.
161
 */
162
class CarbonInterval extends DateInterval implements CarbonConverterInterface
163
{
164
    use IntervalRounding;
165
    use IntervalStep;
166
    use Mixin {
167
        Mixin::mixin as baseMixin;
168
    }
169
    use Options;
170

171
    /**
172
     * Unlimited parts for forHumans() method.
173
     *
174
     * INF constant can be used instead.
175
     */
176
    public const NO_LIMIT = -1;
177

178
    public const POSITIVE = 1;
179
    public const NEGATIVE = -1;
180

181
    /**
182
     * Interval spec period designators
183
     */
184
    public const PERIOD_PREFIX = 'P';
185
    public const PERIOD_YEARS = 'Y';
186
    public const PERIOD_MONTHS = 'M';
187
    public const PERIOD_DAYS = 'D';
188
    public const PERIOD_TIME_PREFIX = 'T';
189
    public const PERIOD_HOURS = 'H';
190
    public const PERIOD_MINUTES = 'M';
191
    public const PERIOD_SECONDS = 'S';
192

193
    public const SPECIAL_TRANSLATIONS = [
194
        1 => [
195
            'option' => CarbonInterface::ONE_DAY_WORDS,
196
            'future' => 'diff_tomorrow',
197
            'past' => 'diff_yesterday',
198
        ],
199
        2 => [
200
            'option' => CarbonInterface::TWO_DAY_WORDS,
201
            'future' => 'diff_after_tomorrow',
202
            'past' => 'diff_before_yesterday',
203
        ],
204
    ];
205

206
    /**
207
     * A translator to ... er ... translate stuff
208
     *
209
     * @var TranslatorInterface
210
     */
211
    protected static $translator;
212

213
    /**
214
     * @var array|null
215
     */
216
    protected static $cascadeFactors;
217

218
    /**
219
     * @var array
220
     */
221
    protected static $formats = [
222
        'y' => 'y',
223
        'Y' => 'y',
224
        'o' => 'y',
225
        'm' => 'm',
226
        'n' => 'm',
227
        'W' => 'weeks',
228
        'd' => 'd',
229
        'j' => 'd',
230
        'z' => 'd',
231
        'h' => 'h',
232
        'g' => 'h',
233
        'H' => 'h',
234
        'G' => 'h',
235
        'i' => 'i',
236
        's' => 's',
237
        'u' => 'micro',
238
        'v' => 'milli',
239
    ];
240

241
    /**
242
     * @var array|null
243
     */
244
    private static $flipCascadeFactors;
245

246
    /**
247
     * The registered macros.
248
     *
249
     * @var array
250
     */
251
    protected static $macros = [];
252

253
    /**
254
     * Timezone handler for settings() method.
255
     *
256
     * @var mixed
257
     */
258
    protected $tzName;
259

260
    /**
261
     * The input used to create the interval.
262
     *
263
     * @var mixed
264
     */
265
    protected $originalInput;
266

267
    /**
268
     * Start date if interval was created from a difference between 2 dates.
269
     *
270
     * @var CarbonInterface|null
271
     */
272
    protected $startDate;
273

274
    /**
275
     * End date if interval was created from a difference between 2 dates.
276
     *
277
     * @var CarbonInterface|null
278
     */
279
    protected $endDate;
280

281
    /**
282
     * Set the instance's timezone from a string or object and add/subtract the offset difference.
283
     *
284
     * @param \DateTimeZone|string $tzName
285
     *
286
     * @return static
287
     */
288 1
    public function shiftTimezone($tzName)
289
    {
290 1
        $this->tzName = $tzName;
291

292 1
        return $this;
293
    }
294

295
    /**
296
     * Mapping of units and factors for cascading.
297
     *
298
     * Should only be modified by changing the factors or referenced constants.
299
     *
300
     * @return array
301
     */
302 1
    public static function getCascadeFactors()
303
    {
304 1
        return static::$cascadeFactors ?: [
305 1
            'milliseconds' => [Carbon::MICROSECONDS_PER_MILLISECOND, 'microseconds'],
306
            'seconds' => [Carbon::MILLISECONDS_PER_SECOND, 'milliseconds'],
307
            'minutes' => [Carbon::SECONDS_PER_MINUTE, 'seconds'],
308
            'hours' => [Carbon::MINUTES_PER_HOUR, 'minutes'],
309
            'dayz' => [Carbon::HOURS_PER_DAY, 'hours'],
310
            'weeks' => [Carbon::DAYS_PER_WEEK, 'dayz'],
311
            'months' => [Carbon::WEEKS_PER_MONTH, 'weeks'],
312
            'years' => [Carbon::MONTHS_PER_YEAR, 'months'],
313
        ];
314
    }
315

316
    /**
317
     * Set default cascading factors for ->cascade() method.
318
     *
319
     * @param array $cascadeFactors
320
     */
321 1
    public static function setCascadeFactors(array $cascadeFactors)
322
    {
323 1
        self::$flipCascadeFactors = null;
324 1
        static::$cascadeFactors = $cascadeFactors;
325
    }
326

327
    ///////////////////////////////////////////////////////////////////
328
    //////////////////////////// CONSTRUCTORS /////////////////////////
329
    ///////////////////////////////////////////////////////////////////
330

331
    /**
332
     * Create a new CarbonInterval instance.
333
     *
334
     * @param int|null $years
335
     * @param int|null $months
336
     * @param int|null $weeks
337
     * @param int|null $days
338
     * @param int|null $hours
339
     * @param int|null $minutes
340
     * @param int|null $seconds
341
     * @param int|null $microseconds
342
     *
343
     * @throws Exception when the interval_spec (passed as $years) cannot be parsed as an interval.
344
     */
345 1
    public function __construct($years = null, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null)
346
    {
347 1
        $this->originalInput = \func_num_args() === 1 ? $years : \func_get_args();
348

349 1
        if ($years instanceof Closure) {
350 1
            $this->step = $years;
351 1
            $years = null;
352
        }
353

354 1
        if ($years instanceof DateInterval) {
355 1
            parent::__construct(static::getDateIntervalSpec($years));
356 1
            $this->f = $years->f;
357 1
            static::copyNegativeUnits($years, $this);
358

359 1
            return;
360
        }
361

362 1
        $spec = $years;
363

364 1
        if (!\is_string($spec) || \floatval($years) || preg_match('/^[0-9.]/', $years)) {
365 1
            $spec = static::PERIOD_PREFIX;
366

367 1
            $spec .= $years > 0 ? $years.static::PERIOD_YEARS : '';
368 1
            $spec .= $months > 0 ? $months.static::PERIOD_MONTHS : '';
369

370 1
            $specDays = 0;
371 1
            $specDays += $weeks > 0 ? $weeks * static::getDaysPerWeek() : 0;
372 1
            $specDays += $days > 0 ? $days : 0;
373

374 1
            $spec .= $specDays > 0 ? $specDays.static::PERIOD_DAYS : '';
375

376 1
            if ($hours > 0 || $minutes > 0 || $seconds > 0) {
377 1
                $spec .= static::PERIOD_TIME_PREFIX;
378 1
                $spec .= $hours > 0 ? $hours.static::PERIOD_HOURS : '';
379 1
                $spec .= $minutes > 0 ? $minutes.static::PERIOD_MINUTES : '';
380 1
                $spec .= $seconds > 0 ? $seconds.static::PERIOD_SECONDS : '';
381
            }
382

383 1
            if ($spec === static::PERIOD_PREFIX) {
384
                // Allow the zero interval.
385 1
                $spec .= '0'.static::PERIOD_YEARS;
386
            }
387
        }
388

389 1
        parent::__construct($spec);
390

391 1
        if (!\is_null($microseconds)) {
392 1
            $this->f = $microseconds / Carbon::MICROSECONDS_PER_SECOND;
393
        }
394
    }
395

396
    /**
397
     * Returns the factor for a given source-to-target couple.
398
     *
399
     * @param string $source
400
     * @param string $target
401
     *
402
     * @return int|null
403
     */
404 1
    public static function getFactor($source, $target)
405
    {
406 1
        $source = self::standardizeUnit($source);
407 1
        $target = self::standardizeUnit($target);
408 1
        $factors = static::getFlipCascadeFactors();
409

410 1
        if (isset($factors[$source])) {
411 1
            [$to, $factor] = $factors[$source];
412

413 1
            if ($to === $target) {
414 1
                return $factor;
415
            }
416

417 1
            return $factor * static::getFactor($to, $target);
418
        }
419

420 1
        return null;
421
    }
422

423
    /**
424
     * Returns current config for days per week.
425
     *
426
     * @return int
427
     */
428 1
    public static function getDaysPerWeek()
429
    {
430 1
        return static::getFactor('dayz', 'weeks') ?: Carbon::DAYS_PER_WEEK;
431
    }
432

433
    /**
434
     * Returns current config for hours per day.
435
     *
436
     * @return int
437
     */
438 1
    public static function getHoursPerDay()
439
    {
440 1
        return static::getFactor('hours', 'dayz') ?: Carbon::HOURS_PER_DAY;
441
    }
442

443
    /**
444
     * Returns current config for minutes per hour.
445
     *
446
     * @return int
447
     */
448 1
    public static function getMinutesPerHour()
449
    {
450 1
        return static::getFactor('minutes', 'hours') ?: Carbon::MINUTES_PER_HOUR;
451
    }
452

453
    /**
454
     * Returns current config for seconds per minute.
455
     *
456
     * @return int
457
     */
458 1
    public static function getSecondsPerMinute()
459
    {
460 1
        return static::getFactor('seconds', 'minutes') ?: Carbon::SECONDS_PER_MINUTE;
461
    }
462

463
    /**
464
     * Returns current config for microseconds per second.
465
     *
466
     * @return int
467
     */
468 1
    public static function getMillisecondsPerSecond()
469
    {
470 1
        return static::getFactor('milliseconds', 'seconds') ?: Carbon::MILLISECONDS_PER_SECOND;
471
    }
472

473
    /**
474
     * Returns current config for microseconds per second.
475
     *
476
     * @return int
477
     */
478 1
    public static function getMicrosecondsPerMillisecond()
479
    {
480 1
        return static::getFactor('microseconds', 'milliseconds') ?: Carbon::MICROSECONDS_PER_MILLISECOND;
481
    }
482

483
    /**
484
     * Create a new CarbonInterval instance from specific values.
485
     * This is an alias for the constructor that allows better fluent
486
     * syntax as it allows you to do CarbonInterval::create(1)->fn() rather than
487
     * (new CarbonInterval(1))->fn().
488
     *
489
     * @param int $years
490
     * @param int $months
491
     * @param int $weeks
492
     * @param int $days
493
     * @param int $hours
494
     * @param int $minutes
495
     * @param int $seconds
496
     * @param int $microseconds
497
     *
498
     * @throws Exception when the interval_spec (passed as $years) cannot be parsed as an interval.
499
     *
500
     * @return static
501
     */
502 1
    public static function create($years = null, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null)
503
    {
504 1
        return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $microseconds);
505
    }
506

507
    /**
508
     * Parse a string into a new CarbonInterval object according to the specified format.
509
     *
510
     * @example
511
     * ```
512
     * echo Carboninterval::createFromFormat('H:i', '1:30');
513
     * ```
514
     *
515
     * @param string $format   Format of the $interval input string
516
     * @param string $interval Input string to convert into an interval
517
     *
518
     * @throws Exception when the $interval cannot be parsed as an interval.
519
     *
520
     * @return static
521
     */
522 1
    public static function createFromFormat(string $format, ?string $interval)
523
    {
524 1
        $instance = new static(0);
525 1
        $length = mb_strlen($format);
526

527 1
        if (preg_match('/s([,.])([uv])$/', $format, $match)) {
528 1
            $interval = explode($match[1], $interval);
529 1
            $index = \count($interval) - 1;
530 1
            $interval[$index] = str_pad($interval[$index], $match[2] === 'v' ? 3 : 6, '0');
531 1
            $interval = implode($match[1], $interval);
532
        }
533

534 1
        for ($index = 0; $index < $length; $index++) {
535 1
            $expected = mb_substr($format, $index, 1);
536 1
            $nextCharacter = mb_substr($interval, 0, 1);
537 1
            $unit = static::$formats[$expected] ?? null;
538

539 1
            if ($unit) {
540 1
                if (!preg_match('/^-?\d+/', $interval, $match)) {
541 1
                    throw new ParseErrorException('number', $nextCharacter);
542
                }
543

544 1
                $interval = mb_substr($interval, mb_strlen($match[0]));
545 1
                $instance->$unit += \intval($match[0]);
546

547 1
                continue;
548
            }
549

550 1
            if ($nextCharacter !== $expected) {
551 1
                throw new ParseErrorException(
552 1
                    "'$expected'",
553
                    $nextCharacter,
554 1
                    'Allowed substitutes for interval formats are '.implode(', ', array_keys(static::$formats))."\n".
555 1
                    'See https://www.php.net/manual/en/function.date.php for their meaning'
556
                );
557
            }
558

559 1
            $interval = mb_substr($interval, 1);
560
        }
561

562 1
        if ($interval !== '') {
563 1
            throw new ParseErrorException(
564 1
                'end of string',
565
                $interval
566
            );
567
        }
568

569 1
        return $instance;
570
    }
571

572
    /**
573
     * Return the original source used to create the current interval.
574
     *
575
     * @return array|int|string|DateInterval|mixed|null
576
     */
577 1
    public function original()
578
    {
579 1
        return $this->originalInput;
580
    }
581

582
    /**
583
     * Return the start date if interval was created from a difference between 2 dates.
584
     *
585
     * @return CarbonInterface|null
586
     */
587 1
    public function start(): ?CarbonInterface
588
    {
589 1
        return $this->startDate;
590
    }
591

592
    /**
593
     * Return the end date if interval was created from a difference between 2 dates.
594
     *
595
     * @return CarbonInterface|null
596
     */
597 1
    public function end(): ?CarbonInterface
598
    {
599 1
        return $this->endDate;
600
    }
601

602
    /**
603
     * Get rid of the original input, start date and end date that may be kept in memory.
604
     *
605
     * @return $this
606
     */
607 1
    public function optimize(): self
608
    {
609 1
        $this->originalInput = null;
610 1
        $this->startDate = null;
611 1
        $this->endDate = null;
612

613 1
        return $this;
614
    }
615

616
    /**
617
     * Get a copy of the instance.
618
     *
619
     * @return static
620
     */
621 1
    public function copy()
622
    {
623 1
        $date = new static(0);
624 1
        $date->copyProperties($this);
625 1
        $date->step = $this->step;
626

627 1
        return $date;
628
    }
629

630
    /**
631
     * Get a copy of the instance.
632
     *
633
     * @return static
634
     */
635 1
    public function clone()
636
    {
637 1
        return $this->copy();
638
    }
639

640
    /**
641
     * Provide static helpers to create instances.  Allows CarbonInterval::years(3).
642
     *
643
     * Note: This is done using the magic method to allow static and instance methods to
644
     *       have the same names.
645
     *
646
     * @param string $method     magic method name called
647
     * @param array  $parameters parameters list
648
     *
649
     * @return static|null
650
     */
651 1
    public static function __callStatic($method, $parameters)
652
    {
653
        try {
654 1
            $interval = new static(0);
655 1
            $localStrictModeEnabled = $interval->localStrictModeEnabled;
656 1
            $interval->localStrictModeEnabled = true;
657 1
            $result = $interval->$method(...$parameters);
658 1
            $interval->localStrictModeEnabled = $localStrictModeEnabled;
659

660 1
            return $result;
661 1
        } catch (BadFluentSetterException $exception) {
662 1
            if (Carbon::isStrictModeEnabled()) {
663 1
                throw new BadFluentConstructorException($method, 0, $exception);
664
            }
665

666 1
            return null;
667
        }
668
    }
669

670
    /**
671
     * Creates a CarbonInterval from string.
672
     *
673
     * Format:
674
     *
675
     * Suffix | Unit    | Example | DateInterval expression
676
     * -------|---------|---------|------------------------
677
     * y      | years   |   1y    | P1Y
678
     * mo     | months  |   3mo   | P3M
679
     * w      | weeks   |   2w    | P2W
680
     * d      | days    |  28d    | P28D
681
     * h      | hours   |   4h    | PT4H
682
     * m      | minutes |  12m    | PT12M
683
     * s      | seconds |  59s    | PT59S
684
     *
685
     * e. g. `1w 3d 4h 32m 23s` is converted to 10 days 4 hours 32 minutes and 23 seconds.
686
     *
687
     * Special cases:
688
     *  - An empty string will return a zero interval
689
     *  - Fractions are allowed for weeks, days, hours and minutes and will be converted
690
     *    and rounded to the next smaller value (caution: 0.5w = 4d)
691
     *
692
     * @param string $intervalDefinition
693
     *
694
     * @return static
695
     */
696 1
    public static function fromString($intervalDefinition)
697
    {
698 1
        if (empty($intervalDefinition)) {
699 1
            return self::withOriginal(new static(0), $intervalDefinition);
700
        }
701

702 1
        $years = 0;
703 1
        $months = 0;
704 1
        $weeks = 0;
705 1
        $days = 0;
706 1
        $hours = 0;
707 1
        $minutes = 0;
708 1
        $seconds = 0;
709 1
        $milliseconds = 0;
710 1
        $microseconds = 0;
711

712 1
        $pattern = '/(\d+(?:\.\d+)?)\h*([^\d\h]*)/i';
713 1
        preg_match_all($pattern, $intervalDefinition, $parts, PREG_SET_ORDER);
714

715 1
        while ([$part, $value, $unit] = array_shift($parts)) {
716 1
            $intValue = \intval($value);
717 1
            $fraction = \floatval($value) - $intValue;
718

719
            // Fix calculation precision
720 1
            switch (round($fraction, 6)) {
721 1
                case 1:
722 1
                    $fraction = 0;
723 1
                    $intValue++;
724

725 1
                    break;
726 1
                case 0:
727 1
                    $fraction = 0;
728

729 1
                    break;
730
            }
731

732 1
            switch ($unit === 'µs' ? 'µs' : strtolower($unit)) {
733 1
                case 'millennia':
734 1
                case 'millennium':
735 1
                    $years += $intValue * CarbonInterface::YEARS_PER_MILLENNIUM;
736

737 1
                    break;
738

739 1
                case 'century':
740 1
                case 'centuries':
741 1
                    $years += $intValue * CarbonInterface::YEARS_PER_CENTURY;
742

743 1
                    break;
744

745 1
                case 'decade':
746 1
                case 'decades':
747 1
                    $years += $intValue * CarbonInterface::YEARS_PER_DECADE;
748

749 1
                    break;
750

751 1
                case 'year':
752 1
                case 'years':
753 1
                case 'y':
754 1
                    $years += $intValue;
755

756 1
                    break;
757

758 1
                case 'quarter':
759 1
                case 'quarters':
760 1
                    $months += $intValue * CarbonInterface::MONTHS_PER_QUARTER;
761

762 1
                    break;
763

764 1
                case 'month':
765 1
                case 'months':
766 1
                case 'mo':
767 1
                    $months += $intValue;
768

769 1
                    break;
770

771 1
                case 'week':
772 1
                case 'weeks':
773 1
                case 'w':
774 1
                    $weeks += $intValue;
775

776 1
                    if ($fraction) {
777 1
                        $parts[] = [null, $fraction * static::getDaysPerWeek(), 'd'];
778
                    }
779

780 1
                    break;
781

782 1
                case 'day':
783 1
                case 'days':
784 1
                case 'd':
785 1
                    $days += $intValue;
786

787 1
                    if ($fraction) {
788 1
                        $parts[] = [null, $fraction * static::getHoursPerDay(), 'h'];
789
                    }
790

791 1
                    break;
792

793 1
                case 'hour':
794 1
                case 'hours':
795 1
                case 'h':
796 1
                    $hours += $intValue;
797

798 1
                    if ($fraction) {
799 1
                        $parts[] = [null, $fraction * static::getMinutesPerHour(), 'm'];
800
                    }
801

802 1
                    break;
803

804 1
                case 'minute':
805 1
                case 'minutes':
806 1
                case 'm':
807 1
                    $minutes += $intValue;
808

809 1
                    if ($fraction) {
810 1
                        $parts[] = [null, $fraction * static::getSecondsPerMinute(), 's'];
811
                    }
812

813 1
                    break;
814

815 1
                case 'second':
816 1
                case 'seconds':
817 1
                case 's':
818 1
                    $seconds += $intValue;
819

820 1
                    if ($fraction) {
821 1
                        $parts[] = [null, $fraction * static::getMillisecondsPerSecond(), 'ms'];
822
                    }
823

824 1
                    break;
825

826 1
                case 'millisecond':
827 1
                case 'milliseconds':
828 1
                case 'milli':
829 1
                case 'ms':
830 1
                    $milliseconds += $intValue;
831

832 1
                    if ($fraction) {
833 1
                        $microseconds += round($fraction * static::getMicrosecondsPerMillisecond());
834
                    }
835

836 1
                    break;
837

838 1
                case 'microsecond':
839 1
                case 'microseconds':
840 1
                case 'micro':
841 1
                case 'µs':
842 1
                    $microseconds += $intValue;
843

844 1
                    break;
845

846
                default:
847 1
                    throw new InvalidIntervalException(
848 1
                        sprintf('Invalid part %s in definition %s', $part, $intervalDefinition)
849
                    );
850
            }
851
        }
852

853 1
        return self::withOriginal(
854 1
            new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $milliseconds * Carbon::MICROSECONDS_PER_MILLISECOND + $microseconds),
855
            $intervalDefinition
856
        );
857
    }
858

859
    /**
860
     * Creates a CarbonInterval from string using a different locale.
861
     *
862
     * @param string      $interval interval string in the given language (may also contain English).
863
     * @param string|null $locale   if locale is null or not specified, current global locale will be used instead.
864
     *
865
     * @return static
866
     */
867 1
    public static function parseFromLocale($interval, $locale = null)
868
    {
869 1
        return static::fromString(Carbon::translateTimeString($interval, $locale ?: static::getLocale(), CarbonInterface::DEFAULT_LOCALE));
870
    }
871

872
    /**
873
     * Create an interval from the difference between 2 dates.
874
     *
875
     * @param \Carbon\Carbon|\DateTimeInterface|mixed $start
876
     * @param \Carbon\Carbon|\DateTimeInterface|mixed $end
877
     *
878
     * @return static
879
     */
880 1
    public static function diff($start, $end = null, bool $absolute = false)
881
    {
882 1
        $start = $start instanceof CarbonInterface ? $start : Carbon::make($start);
883 1
        $end = $end instanceof CarbonInterface ? $end : Carbon::make($end);
884 1
        $interval = static::instance($start->diffAsDateInterval($end, $absolute));
885 1
        $interval->fixDiffInterval();
886

887
        // The line below fixes https://bugs.php.net/bug.php?id=77007
888 1
        $interval->abs($absolute);
889

890 1
        $interval->startDate = $start;
891 1
        $interval->endDate = $end;
892

893 1
        return $interval;
894
    }
895

896
    /**
897
     * Invert the interval if it's inverted.
898
     *
899
     * @param bool $absolute do nothing if set to false
900
     *
901
     * @return $this
902
     */
903 1
    public function abs(bool $absolute = false)
904
    {
905 1
        if ($absolute && $this->invert) {
906 1
            $this->invert();
907
        }
908

909 1
        return $this;
910
    }
911

912
    /**
913
     * @alias abs
914
     *
915
     * Invert the interval if it's inverted.
916
     *
917
     * @param bool $absolute do nothing if set to false
918
     *
919
     * @return $this
920
     */
921 1
    public function absolute(bool $absolute = true)
922
    {
923 1
        return $this->abs($absolute);
924
    }
925

926
    /**
927
     * Cast the current instance into the given class.
928
     *
929
     * @param string $className The $className::instance() method will be called to cast the current object.
930
     *
931
     * @return DateInterval
932
     */
933 1
    public function cast(string $className)
934
    {
935 1
        return self::castIntervalToClass($this, $className);
936
    }
937

938
    /**
939
     * Create a CarbonInterval instance from a DateInterval one.  Can not instance
940
     * DateInterval objects created from DateTime::diff() as you can't externally
941
     * set the $days field.
942
     *
943
     * @param DateInterval $interval
944
     *
945
     * @return static
946
     */
947 1
    public static function instance(DateInterval $interval)
948
    {
949 1
        return self::castIntervalToClass($interval, static::class);
950
    }
951

952
    /**
953
     * Make a CarbonInterval instance from given variable if possible.
954
     *
955
     * Always return a new instance. Parse only strings and only these likely to be intervals (skip dates
956
     * and recurrences). Throw an exception for invalid format, but otherwise return null.
957
     *
958
     * @param mixed|int|DateInterval|string|Closure|null $interval interval or number of the given $unit
959
     * @param string|null                                $unit     if specified, $interval must be an integer
960
     *
961
     * @return static|null
962
     */
963 1
    public static function make($interval, $unit = null)
964
    {
965 1
        if ($unit) {
966 1
            $interval = "$interval ".Carbon::pluralUnit($unit);
967
        }
968

969 1
        if ($interval instanceof DateInterval) {
970 1
            return static::instance($interval);
971
        }
972

973 1
        if ($interval instanceof Closure) {
974 1
            return self::withOriginal(new static($interval), $interval);
975
        }
976

977 1
        if (!\is_string($interval)) {
978 1
            return null;
979
        }
980

981 1
        return static::makeFromString($interval);
982
    }
983

984 1
    protected static function makeFromString(string $interval)
985
    {
986 1
        $interval = trim($interval);
987

988 1
        if (preg_match('/^P[T0-9]/', $interval)) {
989 1
            return new static($interval);
990
        }
991

992 1
        if (preg_match('/^(?:\h*\d+(?:\.\d+)?\h*[a-z]+)+$/i', $interval)) {
993 1
            return static::fromString($interval);
994
        }
995

996
        /** @var static $interval */
997 1
        $interval = static::createFromDateString($interval);
998

999 1
        return !$interval || $interval->isEmpty() ? null : $interval;
1000
    }
1001

1002 1
    protected function resolveInterval($interval)
1003
    {
1004 1
        if (!($interval instanceof self)) {
1005 1
            return self::make($interval);
1006
        }
1007

1008 1
        return $interval;
1009
    }
1010

1011
    /**
1012
     * Sets up a DateInterval from the relative parts of the string.
1013
     *
1014
     * @param string $time
1015
     *
1016
     * @return static
1017
     *
1018
     * @link http://php.net/manual/en/dateinterval.createfromdatestring.php
1019
     */
1020 1
    public static function createFromDateString($time)
1021
    {
1022 1
        $interval = @parent::createFromDateString(strtr($time, [
1023 1
            ',' => ' ',
1024
            ' and ' => ' ',
1025
        ]));
1026

1027 1
        if ($interval instanceof DateInterval) {
1028 1
            $interval = static::instance($interval);
1029
        }
1030

1031 1
        return self::withOriginal($interval, $time);
1032
    }
1033

1034
    ///////////////////////////////////////////////////////////////////
1035
    ///////////////////////// GETTERS AND SETTERS /////////////////////
1036
    ///////////////////////////////////////////////////////////////////
1037

1038
    /**
1039
     * Get a part of the CarbonInterval object.
1040
     *
1041
     * @param string $name
1042
     *
1043
     * @throws UnknownGetterException
1044
     *
1045
     * @return int|float|string
1046
     */
1047 1
    public function get($name)
1048
    {
1049 1
        if (substr($name, 0, 5) === 'total') {
1050 1
            return $this->total(substr($name, 5));
1051
        }
1052

1053 1
        switch (Carbon::singularUnit(rtrim($name, 'z'))) {
1054 1
            case 'year':
1055 1
                return $this->y;
1056

1057 1
            case 'month':
1058 1
                return $this->m;
1059

1060 1
            case 'day':
1061 1
                return $this->d;
1062

1063 1
            case 'hour':
1064 1
                return $this->h;
1065

1066 1
            case 'minute':
1067 1
                return $this->i;
1068

1069 1
            case 'second':
1070 1
                return $this->s;
1071

1072 1
            case 'milli':
1073 1
            case 'millisecond':
1074 1
                return (int) (round($this->f * Carbon::MICROSECONDS_PER_SECOND) / Carbon::MICROSECONDS_PER_MILLISECOND);
1075

1076 1
            case 'micro':
1077 1
            case 'microsecond':
1078 1
                return (int) round($this->f * Carbon::MICROSECONDS_PER_SECOND);
1079

1080 1
            case 'microexcludemilli':
1081 1
                return (int) round($this->f * Carbon::MICROSECONDS_PER_SECOND) % Carbon::MICROSECONDS_PER_MILLISECOND;
1082

1083 1
            case 'week':
1084 1
                return (int) ($this->d / static::getDaysPerWeek());
1085

1086 1
            case 'daysexcludeweek':
1087 1
            case 'dayzexcludeweek':
1088 1
                return $this->d % static::getDaysPerWeek();
1089

1090 1
            case 'locale':
1091 1
                return $this->getTranslatorLocale();
1092

1093
            default:
1094 1
                throw new UnknownGetterException($name);
1095
        }
1096
    }
1097

1098
    /**
1099
     * Get a part of the CarbonInterval object.
1100
     *
1101
     * @param string $name
1102
     *
1103
     * @throws UnknownGetterException
1104
     *
1105
     * @return int|float|string
1106
     */
1107 1
    public function __get($name)
1108
    {
1109 1
        return $this->get($name);
1110
    }
1111

1112
    /**
1113
     * Set a part of the CarbonInterval object.
1114
     *
1115
     * @param string|array $name
1116
     * @param int          $value
1117
     *
1118
     * @throws UnknownSetterException
1119
     *
1120
     * @return $this
1121
     */
1122 1
    public function set($name, $value = null)
1123
    {
1124 1
        $properties = \is_array($name) ? $name : [$name => $value];
1125

1126 1
        foreach ($properties as $key => $value) {
1127 1
            switch (Carbon::singularUnit(rtrim($key, 'z'))) {
1128 1
                case 'year':
1129 1
                    $this->y = $value;
1130

1131 1
                    break;
1132

1133 1
                case 'month':
1134 1
                    $this->m = $value;
1135

1136 1
                    break;
1137

1138 1
                case 'week':
1139 1
                    $this->d = $value * static::getDaysPerWeek();
1140

1141 1
                    break;
1142

1143 1
                case 'day':
1144 1
                    $this->d = $value;
1145

1146 1
                    break;
1147

1148 1
                case 'daysexcludeweek':
1149 1
                case 'dayzexcludeweek':
1150 1
                    $this->d = $this->weeks * static::getDaysPerWeek() + $value;
1151

1152 1
                    break;
1153

1154 1
                case 'hour':
1155 1
                    $this->h = $value;
1156

1157 1
                    break;
1158

1159 1
                case 'minute':
1160 1
                    $this->i = $value;
1161

1162 1
                    break;
1163

1164 1
                case 'second':
1165 1
                    $this->s = $value;
1166

1167 1
                    break;
1168

1169 1
                case 'milli':
1170 1
                case 'millisecond':
1171 1
                    $this->microseconds = $value * Carbon::MICROSECONDS_PER_MILLISECOND + $this->microseconds % Carbon::MICROSECONDS_PER_MILLISECOND;
1172

1173 1
                    break;
1174

1175 1
                case 'micro':
1176 1
                case 'microsecond':
1177 1
                    $this->f = $value / Carbon::MICROSECONDS_PER_SECOND;
1178

1179 1
                    break;
1180

1181
                default:
1182 1
                    if ($this->localStrictModeEnabled ?? Carbon::isStrictModeEnabled()) {
1183 1
                        throw new UnknownSetterException($key);
1184
                    }
1185

1186 1
                    $this->$key = $value;
1187
            }
1188
        }
1189

1190 1
        return $this;
1191
    }
1192

1193
    /**
1194
     * Set a part of the CarbonInterval object.
1195
     *
1196
     * @param string $name
1197
     * @param int    $value
1198
     *
1199
     * @throws UnknownSetterException
1200
     */
1201 1
    public function __set($name, $value)
1202
    {
1203 1
        $this->set($name, $value);
1204
    }
1205

1206
    /**
1207
     * Allow setting of weeks and days to be cumulative.
1208
     *
1209
     * @param int $weeks Number of weeks to set
1210
     * @param int $days  Number of days to set
1211
     *
1212
     * @return static
1213
     */
1214 1
    public function weeksAndDays($weeks, $days)
1215
    {
1216 1
        $this->dayz = ($weeks * static::getDaysPerWeek()) + $days;
1217

1218 1
        return $this;
1219
    }
1220

1221
    /**
1222
     * Returns true if the interval is empty for each unit.
1223
     *
1224
     * @return bool
1225
     */
1226 1
    public function isEmpty()
1227
    {
1228 1
        return $this->years === 0 &&
1229 1
            $this->months === 0 &&
1230 1
            $this->dayz === 0 &&
1231 1
            !$this->days &&
1232 1
            $this->hours === 0 &&
1233 1
            $this->minutes === 0 &&
1234 1
            $this->seconds === 0 &&
1235 1
            $this->microseconds === 0;
1236
    }
1237

1238
    /**
1239
     * Register a custom macro.
1240
     *
1241
     * @example
1242
     * ```
1243
     * CarbonInterval::macro('twice', function () {
1244
     *   return $this->times(2);
1245
     * });
1246
     * echo CarbonInterval::hours(2)->twice();
1247
     * ```
1248
     *
1249
     * @param string          $name
1250
     * @param object|callable $macro
1251
     *
1252
     * @return void
1253
     */
1254 1
    public static function macro($name, $macro)
1255
    {
1256 1
        static::$macros[$name] = $macro;
1257
    }
1258

1259
    /**
1260
     * Register macros from a mixin object.
1261
     *
1262
     * @example
1263
     * ```
1264
     * CarbonInterval::mixin(new class {
1265
     *   public function daysToHours() {
1266
     *     return function () {
1267
     *       $this->hours += $this->days;
1268
     *       $this->days = 0;
1269
     *
1270
     *       return $this;
1271
     *     };
1272
     *   }
1273
     *   public function hoursToDays() {
1274
     *     return function () {
1275
     *       $this->days += $this->hours;
1276
     *       $this->hours = 0;
1277
     *
1278
     *       return $this;
1279
     *     };
1280
     *   }
1281
     * });
1282
     * echo CarbonInterval::hours(5)->hoursToDays() . "\n";
1283
     * echo CarbonInterval::days(5)->daysToHours() . "\n";
1284
     * ```
1285
     *
1286
     * @param object|string $mixin
1287
     *
1288
     * @throws ReflectionException
1289
     *
1290
     * @return void
1291
     */
1292 1
    public static function mixin($mixin)
1293
    {
1294 1
        static::baseMixin($mixin);
1295
    }
1296

1297
    /**
1298
     * Check if macro is registered.
1299
     *
1300
     * @param string $name
1301
     *
1302
     * @return bool
1303
     */
1304 1
    public static function hasMacro($name)
1305
    {
1306 1
        return isset(static::$macros[$name]);
1307
    }
1308

1309
    /**
1310
     * Call given macro.
1311
     *
1312
     * @param string $name
1313
     * @param array  $parameters
1314
     *
1315
     * @return mixed
1316
     */
1317 1
    protected function callMacro($name, $parameters)
1318
    {
1319 1
        $macro = static::$macros[$name];
1320

1321 1
        if ($macro instanceof Closure) {
1322 1
            $boundMacro = @$macro->bindTo($this, static::class) ?: @$macro->bindTo(null, static::class);
1323

1324 1
            return \call_user_func_array($boundMacro ?: $macro, $parameters);
1325
        }
1326

1327 1
        return \call_user_func_array($macro, $parameters);
1328
    }
1329

1330
    /**
1331
     * Allow fluent calls on the setters... CarbonInterval::years(3)->months(5)->day().
1332
     *
1333
     * Note: This is done using the magic method to allow static and instance methods to
1334
     *       have the same names.
1335
     *
1336
     * @param string $method     magic method name called
1337
     * @param array  $parameters parameters list
1338
     *
1339
     * @throws BadFluentSetterException|Throwable
1340
     *
1341
     * @return static|int|float|string
1342
     */
1343 1
    public function __call($method, $parameters)
1344
    {
1345 1
        if (static::hasMacro($method)) {
1346
            return static::bindMacroContext($this, function () use (&$method, &$parameters) {
1347 1
                return $this->callMacro($method, $parameters);
1348 1
            });
1349
        }
1350

1351 1
        $roundedValue = $this->callRoundMethod($method, $parameters);
1352

1353 1
        if ($roundedValue !== null) {
1354 1
            return $roundedValue;
1355
        }
1356

1357
        try {
1358 1
            $this->set($method, \count($parameters) === 0 ? 1 : $parameters[0]);
1359 1
        } catch (UnknownSetterException $exception) {
1360 1
            if ($this->localStrictModeEnabled ?? Carbon::isStrictModeEnabled()) {
1361 1
                throw new BadFluentSetterException($method, 0, $exception);
1362
            }
1363
        }
1364

1365 1
        return $this;
1366
    }
1367

1368 1
    protected function getForHumansInitialVariables($syntax, $short)
1369
    {
1370 1
        if (\is_array($syntax)) {
1371 1
            return $syntax;
1372
        }
1373

1374 1
        if (\is_int($short)) {
1375
            return [
1376 1
                'parts' => $short,
1377
                'short' => false,
1378
            ];
1379
        }
1380

1381 1
        if (\is_bool($syntax)) {
1382
            return [
1383 1
                'short' => $syntax,
1384
                'syntax' => CarbonInterface::DIFF_ABSOLUTE,
1385
            ];
1386
        }
1387

1388 1
        return [];
1389
    }
1390

1391
    /**
1392
     * @param mixed $syntax
1393
     * @param mixed $short
1394
     * @param mixed $parts
1395
     * @param mixed $options
1396
     *
1397
     * @return array
1398
     */
1399 1
    protected function getForHumansParameters($syntax = null, $short = false, $parts = self::NO_LIMIT, $options = null)
1400
    {
1401 1
        $optionalSpace = ' ';
1402 1
        $default = $this->getTranslationMessage('list.0') ?? $this->getTranslationMessage('list') ?? ' ';
1403 1
        $join = $default === '' ? '' : ' ';
1404 1
        $altNumbers = false;
1405 1
        $aUnit = false;
1406 1
        $minimumUnit = 's';
1407 1
        extract($this->getForHumansInitialVariables($syntax, $short));
1408

1409 1
        if (\is_null($syntax)) {
1410 1
            $syntax = CarbonInterface::DIFF_ABSOLUTE;
1411
        }
1412

1413 1
        if ($parts === self::NO_LIMIT) {
1414 1
            $parts = INF;
1415
        }
1416

1417 1
        if (\is_null($options)) {
1418 1
            $options = static::getHumanDiffOptions();
1419
        }
1420

1421 1
        if ($join === false) {
1422 1
            $join = ' ';
1423 1
        } elseif ($join === true) {
1424
            $join = [
1425 1
                $default,
1426 1
                $this->getTranslationMessage('list.1') ?? $default,
1427
            ];
1428
        }
1429

1430 1
        if ($altNumbers) {
1431 1
            if ($altNumbers !== true) {
1432 1
                $language = new Language($this->locale);
1433 1
                $altNumbers = \in_array($language->getCode(), (array) $altNumbers);
1434
            }
1435
        }
1436

1437 1
        if (\is_array($join)) {
1438 1
            [$default, $last] = $join;
1439

1440 1
            if ($default !== ' ') {
1441 1
                $optionalSpace = '';
1442
            }
1443

1444
            $join = function ($list) use ($default, $last) {
1445 1
                if (\count($list) < 2) {
1446 1
                    return implode('', $list);
1447
                }
1448

1449 1
                $end = array_pop($list);
1450

1451 1
                return implode($default, $list).$last.$end;
1452 1
            };
1453
        }
1454

1455 1
        if (\is_string($join)) {
1456 1
            if ($join !== ' ') {
1457 1
                $optionalSpace = '';
1458
            }
1459

1460 1
            $glue = $join;
1461
            $join = function ($list) use ($glue) {
1462 1
                return implode($glue, $list);
1463 1
            };
1464
        }
1465

1466
        $interpolations = [
1467 1
            ':optional-space' => $optionalSpace,
1468
        ];
1469

1470 1
        return [$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit];
1471
    }
1472

1473 1
    protected static function getRoundingMethodFromOptions(int $options): ?string
1474
    {
1475 1
        if ($options & CarbonInterface::ROUND) {
1476 1
            return 'round';
1477
        }
1478

1479 1
        if ($options & CarbonInterface::CEIL) {
1480 1
            return 'ceil';
1481
        }
1482

1483 1
        if ($options & CarbonInterface::FLOOR) {
1484 1
            return 'floor';
1485
        }
1486

1487 1
        return null;
1488
    }
1489

1490
    /**
1491
     * Returns interval values as an array where key are the unit names and values the counts.
1492
     *
1493
     * @return int[]
1494
     */
1495 1
    public function toArray()
1496
    {
1497
        return [
1498 1
            'years' => $this->years,
1499 1
            'months' => $this->months,
1500 1
            'weeks' => $this->weeks,
1501 1
            'days' => $this->daysExcludeWeeks,
1502 1
            'hours' => $this->hours,
1503 1
            'minutes' => $this->minutes,
1504 1
            'seconds' => $this->seconds,
1505 1
            'microseconds' => $this->microseconds,
1506
        ];
1507
    }
1508

1509
    /**
1510
     * Returns interval non-zero values as an array where key are the unit names and values the counts.
1511
     *
1512
     * @return int[]
1513
     */
1514 1
    public function getNonZeroValues()
1515
    {
1516 1
        return array_filter($this->toArray(), 'intval');
1517
    }
1518

1519
    /**
1520
     * Returns interval values as an array where key are the unit names and values the counts
1521
     * from the biggest non-zero one the the smallest non-zero one.
1522
     *
1523
     * @return int[]
1524
     */
1525 1
    public function getValuesSequence()
1526
    {
1527 1
        $nonZeroValues = $this->getNonZeroValues();
1528

1529 1
        if ($nonZeroValues === []) {
1530 1
            return [];
1531
        }
1532

1533 1
        $keys = array_keys($nonZeroValues);
1534 1
        $firstKey = $keys[0];
1535 1
        $lastKey = $keys[\count($keys) - 1];
1536 1
        $values = [];
1537 1
        $record = false;
1538

1539 1
        foreach ($this->toArray() as $unit => $count) {
1540 1
            if ($unit === $firstKey) {
1541 1
                $record = true;
1542
            }
1543

1544 1
            if ($record) {
1545 1
                $values[$unit] = $count;
1546
            }
1547

1548 1
            if ($unit === $lastKey) {
1549 1
                $record = false;
1550
            }
1551
        }
1552

1553 1
        return $values;
1554
    }
1555

1556
    /**
1557
     * Get the current interval in a human readable format in the current locale.
1558
     *
1559
     * @example
1560
     * ```
1561
     * echo CarbonInterval::fromString('4d 3h 40m')->forHumans() . "\n";
1562
     * echo CarbonInterval::fromString('4d 3h 40m')->forHumans(['parts' => 2]) . "\n";
1563
     * echo CarbonInterval::fromString('4d 3h 40m')->forHumans(['parts' => 3, 'join' => true]) . "\n";
1564
     * echo CarbonInterval::fromString('4d 3h 40m')->forHumans(['short' => true]) . "\n";
1565
     * echo CarbonInterval::fromString('1d 24h')->forHumans(['join' => ' or ']) . "\n";
1566
     * echo CarbonInterval::fromString('1d 24h')->forHumans(['minimumUnit' => 'hour']) . "\n";
1567
     * ```
1568
     *
1569
     * @param int|array $syntax  if array passed, parameters will be extracted from it, the array may contains:
1570
     *                           - 'syntax' entry (see below)
1571
     *                           - 'short' entry (see below)
1572
     *                           - 'parts' entry (see below)
1573
     *                           - 'options' entry (see below)
1574
     *                           - 'aUnit' entry, prefer "an hour" over "1 hour" if true
1575
     *                           - 'join' entry determines how to join multiple parts of the string
1576
     *                           `  - if $join is a string, it's used as a joiner glue
1577
     *                           `  - if $join is a callable/closure, it get the list of string and should return a string
1578
     *                           `  - if $join is an array, the first item will be the default glue, and the second item
1579
     *                           `    will be used instead of the glue for the last item
1580
     *                           `  - if $join is true, it will be guessed from the locale ('list' translation file entry)
1581
     *                           `  - if $join is missing, a space will be used as glue
1582
     *                           - 'minimumUnit' entry determines the smallest unit of time to display can be long or
1583
     *                           `  short form of the units, e.g. 'hour' or 'h' (default value: s)
1584
     *                           if int passed, it add modifiers:
1585
     *                           Possible values:
1586
     *                           - CarbonInterface::DIFF_ABSOLUTE          no modifiers
1587
     *                           - CarbonInterface::DIFF_RELATIVE_TO_NOW   add ago/from now modifier
1588
     *                           - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
1589
     *                           Default value: CarbonInterface::DIFF_ABSOLUTE
1590
     * @param bool      $short   displays short format of time units
1591
     * @param int       $parts   maximum number of parts to display (default value: -1: no limits)
1592
     * @param int       $options human diff options
1593
     *
1594
     * @throws Exception
1595
     *
1596
     * @return string
1597
     */
1598 1
    public function forHumans($syntax = null, $short = false, $parts = self::NO_LIMIT, $options = null)
1599
    {
1600 1
        [$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit] = $this->getForHumansParameters($syntax, $short, $parts, $options);
1601

1602 1
        $interval = [];
1603

1604 1
        $syntax = (int) ($syntax === null ? CarbonInterface::DIFF_ABSOLUTE : $syntax);
1605 1
        $absolute = $syntax === CarbonInterface::DIFF_ABSOLUTE;
1606 1
        $relativeToNow = $syntax === CarbonInterface::DIFF_RELATIVE_TO_NOW;
1607 1
        $count = 1;
1608 1
        $unit = $short ? 's' : 'second';
1609 1
        $isFuture = $this->invert === 1;
1610 1
        $transId = $relativeToNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
1611

1612
        /** @var Translator $translator */
1613 1
        $translator = $this->getLocalTranslator();
1614

1615
        $handleDeclensions = function ($unit, $count) use ($interpolations, $transId, $translator, $altNumbers, $absolute) {
1616 1
            if (!$absolute) {
1617
                // Some languages have special pluralization for past and future tense.
1618 1
                $key = $unit.'_'.$transId;
1619 1
                $result = $this->translate($key, $interpolations, $count, $translator, $altNumbers);
1620

1621 1
                if ($result !== $key) {
1622 1
                    return $result;
1623
                }
1624
            }
1625

1626 1
            $result = $this->translate($unit, $interpolations, $count, $translator, $altNumbers);
1627

1628 1
            if ($result !== $unit) {
1629 1
                return $result;
1630
            }
1631

1632 1
            return null;
1633 1
        };
1634

1635 1
        $intervalValues = $this;
1636 1
        $method = static::getRoundingMethodFromOptions($options);
1637

1638 1
        if ($method) {
1639 1
            $previousCount = INF;
1640

1641
            while (
1642 1
                \count($intervalValues->getNonZeroValues()) > $parts &&
1643 1
                ($count = \count($keys = array_keys($intervalValues->getValuesSequence()))) > 1
1644
            ) {
1645 1
                $intervalValues = $this->copy()->roundUnit(
1646 1
                    $keys[min($count, $previousCount - 1) - 2],
1647 1
                    1,
1648
                    $method
1649
                );
1650 1
                $previousCount = $count;
1651
            }
1652
        }
1653

1654
        $diffIntervalArray = [
1655 1
            ['value' => $intervalValues->years,             'unit' => 'year',        'unitShort' => 'y'],
1656 1
            ['value' => $intervalValues->months,            'unit' => 'month',       'unitShort' => 'm'],
1657 1
            ['value' => $intervalValues->weeks,             'unit' => 'week',        'unitShort' => 'w'],
1658 1
            ['value' => $intervalValues->daysExcludeWeeks,  'unit' => 'day',         'unitShort' => 'd'],
1659 1
            ['value' => $intervalValues->hours,             'unit' => 'hour',        'unitShort' => 'h'],
1660 1
            ['value' => $intervalValues->minutes,           'unit' => 'minute',      'unitShort' => 'min'],
1661 1
            ['value' => $intervalValues->seconds,           'unit' => 'second',      'unitShort' => 's'],
1662 1
            ['value' => $intervalValues->milliseconds,      'unit' => 'millisecond', 'unitShort' => 'ms'],
1663 1
            ['value' => $intervalValues->microExcludeMilli, 'unit' => 'microsecond', 'unitShort' => 'µs'],
1664
        ];
1665

1666
        $transChoice = function ($short, $unitData) use ($absolute, $handleDeclensions, $translator, $aUnit, $altNumbers, $interpolations) {
1667 1
            $count = $unitData['value'];
1668

1669 1
            if ($short) {
1670 1
                $result = $handleDeclensions($unitData['unitShort'], $count);
1671

1672 1
                if ($result !== null) {
1673 1
                    return $result;
1674
                }
1675 1
            } elseif ($aUnit) {
1676 1
                $result = $handleDeclensions('a_'.$unitData['unit'], $count);
1677

1678 1
                if ($result !== null) {
1679 1
                    return $result;
1680
                }
1681
            }
1682

1683 1
            if (!$absolute) {
1684 1
                return $handleDeclensions($unitData['unit'], $count);
1685
            }
1686

1687 1
            return $this->translate($unitData['unit'], $interpolations, $count, $translator, $altNumbers);
1688 1
        };
1689

1690 1
        $fallbackUnit = ['second', 's'];
1691 1
        foreach ($diffIntervalArray as $diffIntervalData) {
1692 1
            if ($diffIntervalData['value'] > 0) {
1693 1
                $unit = $short ? $diffIntervalData['unitShort'] : $diffIntervalData['unit'];
1694 1
                $count = $diffIntervalData['value'];
1695 1
                $interval[] = $transChoice($short, $diffIntervalData);
1696 1
            } elseif ($options & CarbonInterface::SEQUENTIAL_PARTS_ONLY && \count($interval) > 0) {
1697 1
                break;
1698
            }
1699

1700
            // break the loop after we get the required number of parts in array
1701 1
            if (\count($interval) >= $parts) {
1702 1
                break;
1703
            }
1704

1705
            // break the loop after we have reached the minimum unit
1706 1
            if (\in_array($minimumUnit, [$diffIntervalData['unit'], $diffIntervalData['unitShort']])) {
1707 1
                $fallbackUnit = [$diffIntervalData['unit'], $diffIntervalData['unitShort']];
1708

1709 1
                break;
1710
            }
1711
        }
1712

1713 1
        if (\count($interval) === 0) {
1714 1
            if ($relativeToNow && $options & CarbonInterface::JUST_NOW) {
1715 1
                $key = 'diff_now';
1716 1
                $translation = $this->translate($key, $interpolations, null, $translator);
1717

1718 1
                if ($translation !== $key) {
1719 1
                    return $translation;
1720
                }
1721
            }
1722

1723 1
            $count = $options & CarbonInterface::NO_ZERO_DIFF ? 1 : 0;
1724 1
            $unit = $fallbackUnit[$short ? 1 : 0];
1725 1
            $interval[] = $this->translate($unit, $interpolations, $count, $translator, $altNumbers);
1726
        }
1727

1728
        // join the interval parts by a space
1729 1
        $time = $join($interval);
1730

1731 1
        unset($diffIntervalArray, $interval);
1732

1733 1
        if ($absolute) {
1734 1
            return $time;
1735
        }
1736

1737 1
        $isFuture = $this->invert === 1;
1738

1739 1
        $transId = $relativeToNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
1740

1741 1
        if ($parts === 1) {
1742 1
            if ($relativeToNow && $unit === 'day') {
1743 1
                $specialTranslations = static::SPECIAL_TRANSLATIONS[$count] ?? null;
1744

1745 1
                if ($specialTranslations && $options & $specialTranslations['option']) {
1746 1
                    $key = $specialTranslations[$isFuture ? 'future' : 'past'];
1747 1
                    $translation = $this->translate($key, $interpolations, null, $translator);
1748

1749 1
                    if ($translation !== $key) {
1750 1
                        return $translation;
1751
                    }
1752
                }
1753
            }
1754

1755 1
            $aTime = $aUnit ? $handleDeclensions('a_'.$unit, $count) : null;
1756

1757 1
            $time = $aTime ?: $handleDeclensions($unit, $count) ?: $time;
1758
        }
1759

1760 1
        $time = [':time' => $time];
1761

1762 1
        return $this->translate($transId, array_merge($time, $interpolations, $time), null, $translator);
1763
    }
1764

1765
    /**
1766
     * Format the instance as a string using the forHumans() function.
1767
     *
1768
     * @throws Exception
1769
     *
1770
     * @return string
1771
     */
1772 1
    public function __toString()
1773
    {
1774 1
        $format = $this->localToStringFormat;
1775

1776 1
        if ($format) {
1777 1
            if ($format instanceof Closure) {
1778 1
                return $format($this);
1779
            }
1780

1781 1
            return $this->format($format);
1782
        }
1783

1784 1
        return $this->forHumans();
1785
    }
1786

1787
    /**
1788
     * Return native DateInterval PHP object matching the current instance.
1789
     *
1790
     * @example
1791
     * ```
1792
     * var_dump(CarbonInterval::hours(2)->toDateInterval());
1793
     * ```
1794
     *
1795
     * @return DateInterval
1796
     */
1797 1
    public function toDateInterval()
1798
    {
1799 1
        return self::castIntervalToClass($this, DateInterval::class);
1800
    }
1801

1802
    /**
1803
     * Convert the interval to a CarbonPeriod.
1804
     *
1805
     * @param array ...$params Start date, [end date or recurrences] and optional settings.
1806
     *
1807
     * @return CarbonPeriod
1808
     */
1809 1
    public function toPeriod(...$params)
1810
    {
1811 1
        return CarbonPeriod::create($this, ...$params);
1812
    }
1813

1814
    /**
1815
     * Decompose the current interval into
1816
     *
1817
     * @param mixed|int|DateInterval|string|Closure|null $interval interval or number of the given $unit
1818
     * @param string|null                                $unit     if specified, $interval must be an integer
1819
     *
1820
     * @return CarbonPeriod
1821
     */
1822 1
    public function stepBy($interval, $unit = null)
1823
    {
1824 1
        $start = $this->startDate ?: Carbon::make($this->startDate ?: 'now');
1825 1
        $end = $this->endDate ?: $start->copy()->add($this);
1826

1827 1
        return CarbonPeriod::create(static::make($interval, $unit), $start, $end);
1828
    }
1829

1830
    /**
1831
     * Invert the interval.
1832
     *
1833
     * @param bool|int $inverted if a parameter is passed, the passed value casted as 1 or 0 is used
1834
     *                           as the new value of the ->invert property.
1835
     *
1836
     * @return $this
1837
     */
1838 1
    public function invert($inverted = null)
1839
    {
1840 1
        $this->invert = (\func_num_args() === 0 ? !$this->invert : $inverted) ? 1 : 0;
1841

1842 1
        return $this;
1843
    }
1844

1845 1
    protected function solveNegativeInterval()
1846
    {
1847 1
        if (!$this->isEmpty() && $this->years <= 0 && $this->months <= 0 && $this->dayz <= 0 && $this->hours <= 0 && $this->minutes <= 0 && $this->seconds <= 0 && $this->microseconds <= 0) {
1848 1
            $this->years *= self::NEGATIVE;
1849 1
            $this->months *= self::NEGATIVE;
1850 1
            $this->dayz *= self::NEGATIVE;
1851 1
            $this->hours *= self::NEGATIVE;
1852 1
            $this->minutes *= self::NEGATIVE;
1853 1
            $this->seconds *= self::NEGATIVE;
1854 1
            $this->microseconds *= self::NEGATIVE;
1855 1
            $this->invert();
1856
        }
1857

1858 1
        return $this;
1859
    }
1860

1861
    /**
1862
     * Add the passed interval to the current instance.
1863
     *
1864
     * @param string|DateInterval $unit
1865
     * @param int                 $value
1866
     *
1867
     * @return static
1868
     */
1869 1
    public function add($unit, $value = 1)
1870
    {
1871 1
        if (is_numeric($unit)) {
1872 1
            [$value, $unit] = [$unit, $value];
1873
        }
1874

1875 1
        if (\is_string($unit) && !preg_match('/^\s*\d/', $unit)) {
1876 1
            $unit = "$value $unit";
1877 1
            $value = 1;
1878
        }
1879

1880 1
        $interval = static::make($unit);
1881

1882 1
        if (!$interval) {
1883 1
            throw new InvalidIntervalException('This type of data cannot be added/subtracted.');
1884
        }
1885

1886 1
        if ($value !== 1) {
1887 1
            $interval->times($value);
1888
        }
1889

1890 1
        $sign = ($this->invert === 1) !== ($interval->invert === 1) ? self::NEGATIVE : self::POSITIVE;
1891 1
        $this->years += $interval->y * $sign;
1892 1
        $this->months += $interval->m * $sign;
1893 1
        $this->dayz += ($interval->days === false ? $interval->d : $interval->days) * $sign;
1894 1
        $this->hours += $interval->h * $sign;
1895 1
        $this->minutes += $interval->i * $sign;
1896 1
        $this->seconds += $interval->s * $sign;
1897 1
        $this->microseconds += $interval->microseconds * $sign;
1898

1899 1
        $this->solveNegativeInterval();
1900

1901 1
        return $this;
1902
    }
1903

1904
    /**
1905
     * Subtract the passed interval to the current instance.
1906
     *
1907
     * @param string|DateInterval $unit
1908
     * @param int                 $value
1909
     *
1910
     * @return static
1911
     */
1912 1
    public function sub($unit, $value = 1)
1913
    {
1914 1
        if (is_numeric($unit)) {
1915 1
            [$value, $unit] = [$unit, $value];
1916
        }
1917

1918 1
        return $this->add($unit, -\floatval($value));
1919
    }
1920

1921
    /**
1922
     * Subtract the passed interval to the current instance.
1923
     *
1924
     * @param string|DateInterval $unit
1925
     * @param int                 $value
1926
     *
1927
     * @return static
1928
     */
1929 1
    public function subtract($unit, $value = 1)
1930
    {
1931 1
        return $this->sub($unit, $value);
1932
    }
1933

1934
    /**
1935
     * Multiply current instance given number of times. times() is naive, it multiplies each unit
1936
     * (so day can be greater than 31, hour can be greater than 23, etc.) and the result is rounded
1937
     * separately for each unit.
1938
     *
1939
     * Use times() when you want a fast and approximated calculation that does not cascade units.
1940
     *
1941
     * For a precise and cascaded calculation,
1942
     *
1943
     * @see multiply()
1944
     *
1945
     * @param float|int $factor
1946
     *
1947
     * @return $this
1948
     */
1949 1
    public function times($factor)
1950
    {
1951 1
        if ($factor < 0) {
1952 1
            $this->invert = $this->invert ? 0 : 1;
1953 1
            $factor = -$factor;
1954
        }
1955

1956 1
        $this->years = (int) round($this->years * $factor);
1957 1
        $this->months = (int) round($this->months * $factor);
1958 1
        $this->dayz = (int) round($this->dayz * $factor);
1959 1
        $this->hours = (int) round($this->hours * $factor);
1960 1
        $this->minutes = (int) round($this->minutes * $factor);
1961 1
        $this->seconds = (int) round($this->seconds * $factor);
1962 1
        $this->microseconds = (int) round($this->microseconds * $factor);
1963

1964 1
        return $this;
1965
    }
1966

1967
    /**
1968
     * Divide current instance by a given divider. shares() is naive, it divides each unit separately
1969
     * and the result is rounded for each unit. So 5 hours and 20 minutes shared by 3 becomes 2 hours
1970
     * and 7 minutes.
1971
     *
1972
     * Use shares() when you want a fast and approximated calculation that does not cascade units.
1973
     *
1974
     * For a precise and cascaded calculation,
1975
     *
1976
     * @see divide()
1977
     *
1978
     * @param float|int $divider
1979
     *
1980
     * @return $this
1981
     */
1982 1
    public function shares($divider)
1983
    {
1984 1
        return $this->times(1 / $divider);
1985
    }
1986

1987 1
    protected function copyProperties(self $interval, $ignoreSign = false)
1988
    {
1989 1
        $this->years = $interval->years;
1990 1
        $this->months = $interval->months;
1991 1
        $this->dayz = $interval->dayz;
1992 1
        $this->hours = $interval->hours;
1993 1
        $this->minutes = $interval->minutes;
1994 1
        $this->seconds = $interval->seconds;
1995 1
        $this->microseconds = $interval->microseconds;
1996

1997 1
        if (!$ignoreSign) {
1998 1
            $this->invert = $interval->invert;
1999
        }
2000

2001 1
        return $this;
2002
    }
2003

2004
    /**
2005
     * Multiply and cascade current instance by a given factor.
2006
     *
2007
     * @param float|int $factor
2008
     *
2009
     * @return $this
2010
     */
2011 1
    public function multiply($factor)
2012
    {
2013 1
        if ($factor < 0) {
2014 1
            $this->invert = $this->invert ? 0 : 1;
2015 1
            $factor = -$factor;
2016
        }
2017

2018 1
        $yearPart = (int) floor($this->years * $factor); // Split calculation to prevent imprecision
2019

2020 1
        if ($yearPart) {
2021 1
            $this->years -= $yearPart / $factor;
2022
        }
2023

2024 1
        return $this->copyProperties(
2025 1
            static::create($yearPart)
2026 1
                ->microseconds(abs($this->totalMicroseconds) * $factor)
2027 1
                ->cascade(),
2028 1
            true
2029
        );
2030
    }
2031

2032
    /**
2033
     * Divide and cascade current instance by a given divider.
2034
     *
2035
     * @param float|int $divider
2036
     *
2037
     * @return $this
2038
     */
2039 1
    public function divide($divider)
2040
    {
2041 1
        return $this->multiply(1 / $divider);
2042
    }
2043

2044
    /**
2045
     * Get the interval_spec string of a date interval.
2046
     *
2047
     * @param DateInterval $interval
2048
     *
2049
     * @return string
2050
     */
2051 1
    public static function getDateIntervalSpec(DateInterval $interval)
2052
    {
2053 1
        $date = array_filter([
2054 1
            static::PERIOD_YEARS => abs($interval->y),
2055 1
            static::PERIOD_MONTHS => abs($interval->m),
2056 1
            static::PERIOD_DAYS => abs($interval->d),
2057
        ]);
2058

2059 1
        $time = array_filter([
2060 1
            static::PERIOD_HOURS => abs($interval->h),
2061 1
            static::PERIOD_MINUTES => abs($interval->i),
2062 1
            static::PERIOD_SECONDS => abs($interval->s),
2063
        ]);
2064

2065 1
        $specString = static::PERIOD_PREFIX;
2066

2067 1
        foreach ($date as $key => $value) {
2068 1
            $specString .= $value.$key;
2069
        }
2070

2071 1
        if (\count($time) > 0) {
2072 1
            $specString .= static::PERIOD_TIME_PREFIX;
2073 1
            foreach ($time as $key => $value) {
2074 1
                $specString .= $value.$key;
2075
            }
2076
        }
2077

2078 1
        return $specString === static::PERIOD_PREFIX ? 'PT0S' : $specString;
2079
    }
2080

2081
    /**
2082
     * Get the interval_spec string.
2083
     *
2084
     * @return string
2085
     */
2086 1
    public function spec()
2087
    {
2088 1
        return static::getDateIntervalSpec($this);
2089
    }
2090

2091
    /**
2092
     * Comparing 2 date intervals.
2093
     *
2094
     * @param DateInterval $first
2095
     * @param DateInterval $second
2096
     *
2097
     * @return int
2098
     */
2099 1
    public static function compareDateIntervals(DateInterval $first, DateInterval $second)
2100
    {
2101 1
        $current = Carbon::now();
2102 1
        $passed = $current->copy()->add($second);
2103 1
        $current->add($first);
2104

2105 1
        return $current <=> $passed;
2106
    }
2107

2108
    /**
2109
     * Comparing with passed interval.
2110
     *
2111
     * @param DateInterval $interval
2112
     *
2113
     * @return int
2114
     */
2115 1
    public function compare(DateInterval $interval)
2116
    {
2117 1
        return static::compareDateIntervals($this, $interval);
2118
    }
2119

2120
    /**
2121
     * Convert overflowed values into bigger units.
2122
     *
2123
     * @return $this
2124
     */
2125 1
    public function cascade()
2126
    {
2127 1
        return $this->doCascade(false);
2128
    }
2129

2130 1
    public function hasNegativeValues(): bool
2131
    {
2132 1
        foreach ($this->toArray() as $value) {
2133 1
            if ($value < 0) {
2134 1
                return true;
2135
            }
2136
        }
2137

2138 1
        return false;
2139
    }
2140

2141 1
    public function hasPositiveValues(): bool
2142
    {
2143 1
        foreach ($this->toArray() as $value) {
2144 1
            if ($value > 0) {
2145 1
                return true;
2146
            }
2147
        }
2148

2149 1
        return false;
2150
    }
2151

2152
    /**
2153
     * Get amount of given unit equivalent to the interval.
2154
     *
2155
     * @param string $unit
2156
     *
2157
     * @throws UnknownUnitException|UnitNotConfiguredException
2158
     *
2159
     * @return float
2160
     */
2161 1
    public function total($unit): float
2162
    {
2163 1
        $realUnit = $unit = strtolower($unit);
2164

2165 1
        if (\in_array($unit, ['days', 'weeks'])) {
2166 1
            $realUnit = 'dayz';
2167 1
        } elseif (!\in_array($unit, ['microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'dayz', 'months', 'years'])) {
2168 1
            throw new UnknownUnitException($unit);
2169
        }
2170

2171 1
        if ($this->startDate && $this->endDate) {
2172 1
            return $this->startDate->diffInUnit($unit, $this->endDate);
2173
        }
2174

2175 1
        $result = 0;
2176 1
        $cumulativeFactor = 0;
2177 1
        $unitFound = false;
2178 1
        $factors = static::getFlipCascadeFactors();
2179 1
        $daysPerWeek = static::getDaysPerWeek();
2180

2181
        $values = [
2182 1
            'years' => $this->years,
2183 1
            'months' => $this->months,
2184 1
            'weeks' => (int) ($this->d / $daysPerWeek),
2185 1
            'dayz' => (int) ($this->d % $daysPerWeek),
2186 1
            'hours' => $this->hours,
2187 1
            'minutes' => $this->minutes,
2188 1
            'seconds' => $this->seconds,
2189 1
            'milliseconds' => (int) ($this->microseconds / Carbon::MICROSECONDS_PER_MILLISECOND),
2190 1
            'microseconds' => (int) ($this->microseconds % Carbon::MICROSECONDS_PER_MILLISECOND),
2191
        ];
2192

2193 1
        if (isset($factors['dayz']) && $factors['dayz'][0] !== 'weeks') {
2194 1
            $values['dayz'] += $values['weeks'] * $daysPerWeek;
2195 1
            $values['weeks'] = 0;
2196
        }
2197

2198 1
        foreach ($factors as $source => [$target, $factor]) {
2199 1
            if ($source === $realUnit) {
2200 1
                $unitFound = true;
2201 1
                $value = $values[$source];
2202 1
                $result += $value;
2203 1
                $cumulativeFactor = 1;
2204
            }
2205

2206 1
            if ($factor === false) {
2207 1
                if ($unitFound) {
2208 1
                    break;
2209
                }
2210

2211 1
                $result = 0;
2212 1
                $cumulativeFactor = 0;
2213

2214 1
                continue;
2215
            }
2216

2217 1
            if ($target === $realUnit) {
2218 1
                $unitFound = true;
2219
            }
2220

2221 1
            if ($cumulativeFactor) {
2222 1
                $cumulativeFactor *= $factor;
2223 1
                $result += $values[$target] * $cumulativeFactor;
2224

2225 1
                continue;
2226
            }
2227

2228 1
            $value = $values[$source];
2229

2230 1
            $result = ($result + $value) / $factor;
2231
        }
2232

2233 1
        if (isset($target) && !$cumulativeFactor) {
2234 1
            $result += $values[$target];
2235
        }
2236

2237 1
        if (!$unitFound) {
2238 1
            throw new UnitNotConfiguredException($unit);
2239
        }
2240

2241 1
        if ($this->invert) {
2242 1
            $result *= self::NEGATIVE;
2243
        }
2244

2245 1
        if ($unit === 'weeks') {
2246 1
            return $result / $daysPerWeek;
2247
        }
2248

2249 1
        return $result;
2250
    }
2251

2252
    /**
2253
     * Determines if the instance is equal to another
2254
     *
2255
     * @param CarbonInterval|DateInterval|mixed $interval
2256
     *
2257
     * @see equalTo()
2258
     *
2259
     * @return bool
2260
     */
2261 1
    public function eq($interval): bool
2262
    {
2263 1
        return $this->equalTo($interval);
2264
    }
2265

2266
    /**
2267
     * Determines if the instance is equal to another
2268
     *
2269
     * @param CarbonInterval|DateInterval|mixed $interval
2270
     *
2271
     * @return bool
2272
     */
2273 1
    public function equalTo($interval): bool
2274
    {
2275 1
        $interval = $this->resolveInterval($interval);
2276

2277 1
        if ($interval === null) {
2278 1
            return false;
2279
        }
2280

2281 1
        $step = $this->getStep();
2282

2283 1
        if ($step) {
2284 1
            return $step === $interval->getStep();
2285
        }
2286

2287 1
        if ($this->isEmpty()) {
2288 1
            return $interval->isEmpty();
2289
        }
2290

2291 1
        $cascadedInterval = $this->copy()->cascade();
2292 1
        $cascadedComparedInterval = $interval->copy()->cascade();
2293

2294 1
        return $cascadedInterval->invert === $cascadedComparedInterval->invert &&
2295 1
            $cascadedInterval->getNonZeroValues() === $cascadedComparedInterval->getNonZeroValues();
2296
    }
2297

2298
    /**
2299
     * Determines if the instance is not equal to another
2300
     *
2301
     * @param CarbonInterval|DateInterval|mixed $interval
2302
     *
2303
     * @see notEqualTo()
2304
     *
2305
     * @return bool
2306
     */
2307 1
    public function ne($interval): bool
2308
    {
2309 1
        return $this->notEqualTo($interval);
2310
    }
2311

2312
    /**
2313
     * Determines if the instance is not equal to another
2314
     *
2315
     * @param CarbonInterval|DateInterval|mixed $interval
2316
     *
2317
     * @return bool
2318
     */
2319 1
    public function notEqualTo($interval): bool
2320
    {
2321 1
        return !$this->eq($interval);
2322
    }
2323

2324
    /**
2325
     * Determines if the instance is greater (longer) than another
2326
     *
2327
     * @param CarbonInterval|DateInterval|mixed $interval
2328
     *
2329
     * @see greaterThan()
2330
     *
2331
     * @return bool
2332
     */
2333 1
    public function gt($interval): bool
2334
    {
2335 1
        return $this->greaterThan($interval);
2336
    }
2337

2338
    /**
2339
     * Determines if the instance is greater (longer) than another
2340
     *
2341
     * @param CarbonInterval|DateInterval|mixed $interval
2342
     *
2343
     * @return bool
2344
     */
2345 1
    public function greaterThan($interval): bool
2346
    {
2347 1
        $interval = $this->resolveInterval($interval);
2348

2349 1
        return $interval === null || $this->totalMicroseconds > $interval->totalMicroseconds;
2350
    }
2351

2352
    /**
2353
     * Determines if the instance is greater (longer) than or equal to another
2354
     *
2355
     * @param CarbonInterval|DateInterval|mixed $interval
2356
     *
2357
     * @see greaterThanOrEqualTo()
2358
     *
2359
     * @return bool
2360
     */
2361 1
    public function gte($interval): bool
2362
    {
2363 1
        return $this->greaterThanOrEqualTo($interval);
2364
    }
2365

2366
    /**
2367
     * Determines if the instance is greater (longer) than or equal to another
2368
     *
2369
     * @param CarbonInterval|DateInterval|mixed $interval
2370
     *
2371
     * @return bool
2372
     */
2373 1
    public function greaterThanOrEqualTo($interval): bool
2374
    {
2375 1
        return $this->greaterThan($interval) || $this->equalTo($interval);
2376
    }
2377

2378
    /**
2379
     * Determines if the instance is less (shorter) than another
2380
     *
2381
     * @param CarbonInterval|DateInterval|mixed $interval
2382
     *
2383
     * @see lessThan()
2384
     *
2385
     * @return bool
2386
     */
2387 1
    public function lt($interval): bool
2388
    {
2389 1
        return $this->lessThan($interval);
2390
    }
2391

2392
    /**
2393
     * Determines if the instance is less (shorter) than another
2394
     *
2395
     * @param CarbonInterval|DateInterval|mixed $interval
2396
     *
2397
     * @return bool
2398
     */
2399 1
    public function lessThan($interval): bool
2400
    {
2401 1
        $interval = $this->resolveInterval($interval);
2402

2403 1
        return $interval !== null && $this->totalMicroseconds < $interval->totalMicroseconds;
2404
    }
2405

2406
    /**
2407
     * Determines if the instance is less (shorter) than or equal to another
2408
     *
2409
     * @param CarbonInterval|DateInterval|mixed $interval
2410
     *
2411
     * @see lessThanOrEqualTo()
2412
     *
2413
     * @return bool
2414
     */
2415 1
    public function lte($interval): bool
2416
    {
2417 1
        return $this->lessThanOrEqualTo($interval);
2418
    }
2419

2420
    /**
2421
     * Determines if the instance is less (shorter) than or equal to another
2422
     *
2423
     * @param CarbonInterval|DateInterval|mixed $interval
2424
     *
2425
     * @return bool
2426
     */
2427 1
    public function lessThanOrEqualTo($interval): bool
2428
    {
2429 1
        return $this->lessThan($interval) || $this->equalTo($interval);
2430
    }
2431

2432
    /**
2433
     * Determines if the instance is between two others.
2434
     *
2435
     * The third argument allow you to specify if bounds are included or not (true by default)
2436
     * but for when you including/excluding bounds may produce different results in your application,
2437
     * we recommend to use the explicit methods ->betweenIncluded() or ->betweenExcluded() instead.
2438
     *
2439
     * @example
2440
     * ```
2441
     * CarbonInterval::hours(48)->between(CarbonInterval::day(), CarbonInterval::days(3)); // true
2442
     * CarbonInterval::hours(48)->between(CarbonInterval::day(), CarbonInterval::hours(36)); // false
2443
     * CarbonInterval::hours(48)->between(CarbonInterval::day(), CarbonInterval::days(2)); // true
2444
     * CarbonInterval::hours(48)->between(CarbonInterval::day(), CarbonInterval::days(2), false); // false
2445
     * ```
2446
     *
2447
     * @param CarbonInterval|DateInterval|mixed $interval1
2448
     * @param CarbonInterval|DateInterval|mixed $interval2
2449
     * @param bool                              $equal     Indicates if an equal to comparison should be done
2450
     *
2451
     * @return bool
2452
     */
2453 1
    public function between($interval1, $interval2, $equal = true): bool
2454
    {
2455 1
        return $equal
2456 1
            ? $this->greaterThanOrEqualTo($interval1) && $this->lessThanOrEqualTo($interval2)
2457 1
            : $this->greaterThan($interval1) && $this->lessThan($interval2);
2458
    }
2459

2460
    /**
2461
     * Determines if the instance is between two others, bounds excluded.
2462
     *
2463
     * @example
2464
     * ```
2465
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::days(3)); // true
2466
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::hours(36)); // false
2467
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::days(2)); // true
2468
     * ```
2469
     *
2470
     * @param CarbonInterval|DateInterval|mixed $interval1
2471
     * @param CarbonInterval|DateInterval|mixed $interval2
2472
     *
2473
     * @return bool
2474
     */
2475 1
    public function betweenIncluded($interval1, $interval2): bool
2476
    {
2477 1
        return $this->between($interval1, $interval2, true);
2478
    }
2479

2480
    /**
2481
     * Determines if the instance is between two others, bounds excluded.
2482
     *
2483
     * @example
2484
     * ```
2485
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::days(3)); // true
2486
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::hours(36)); // false
2487
     * CarbonInterval::hours(48)->betweenExcluded(CarbonInterval::day(), CarbonInterval::days(2)); // false
2488
     * ```
2489
     *
2490
     * @param CarbonInterval|DateInterval|mixed $interval1
2491
     * @param CarbonInterval|DateInterval|mixed $interval2
2492
     *
2493
     * @return bool
2494
     */
2495 1
    public function betweenExcluded($interval1, $interval2): bool
2496
    {
2497 1
        return $this->between($interval1, $interval2, false);
2498
    }
2499

2500
    /**
2501
     * Determines if the instance is between two others
2502
     *
2503
     * @example
2504
     * ```
2505
     * CarbonInterval::hours(48)->isBetween(CarbonInterval::day(), CarbonInterval::days(3)); // true
2506
     * CarbonInterval::hours(48)->isBetween(CarbonInterval::day(), CarbonInterval::hours(36)); // false
2507
     * CarbonInterval::hours(48)->isBetween(CarbonInterval::day(), CarbonInterval::days(2)); // true
2508
     * CarbonInterval::hours(48)->isBetween(CarbonInterval::day(), CarbonInterval::days(2), false); // false
2509
     * ```
2510
     *
2511
     * @param CarbonInterval|DateInterval|mixed $interval1
2512
     * @param CarbonInterval|DateInterval|mixed $interval2
2513
     * @param bool                              $equal     Indicates if an equal to comparison should be done
2514
     *
2515
     * @return bool
2516
     */
2517 1
    public function isBetween($interval1, $interval2, $equal = true): bool
2518
    {
2519 1
        return $this->between($interval1, $interval2, $equal);
2520
    }
2521

2522
    /**
2523
     * Round the current instance at the given unit with given precision if specified and the given function.
2524
     *
2525
     * @param string                             $unit
2526
     * @param float|int|string|DateInterval|null $precision
2527
     * @param string                             $function
2528
     *
2529
     * @throws Exception
2530
     *
2531
     * @return $this
2532
     */
2533 1
    public function roundUnit($unit, $precision = 1, $function = 'round')
2534
    {
2535 1
        $base = CarbonImmutable::parse('2000-01-01 00:00:00', 'UTC')
2536 1
            ->roundUnit($unit, $precision, $function);
2537 1
        $next = $base->add($this);
2538 1
        $inverted = $next < $base;
2539

2540 1
        if ($inverted) {
2541 1
            $next = $base->sub($this);
2542
        }
2543

2544 1
        $this->copyProperties(
2545
            $next
2546 1
                ->roundUnit($unit, $precision, $function)
2547 1
                ->diff($base)
2548
        );
2549

2550 1
        return $this->invert($inverted);
2551
    }
2552

2553
    /**
2554
     * Truncate the current instance at the given unit with given precision if specified.
2555
     *
2556
     * @param string                             $unit
2557
     * @param float|int|string|DateInterval|null $precision
2558
     *
2559
     * @throws Exception
2560
     *
2561
     * @return $this
2562
     */
2563 1
    public function floorUnit($unit, $precision = 1)
2564
    {
2565 1
        return $this->roundUnit($unit, $precision, 'floor');
2566
    }
2567

2568
    /**
2569
     * Ceil the current instance at the given unit with given precision if specified.
2570
     *
2571
     * @param string                             $unit
2572
     * @param float|int|string|DateInterval|null $precision
2573
     *
2574
     * @throws Exception
2575
     *
2576
     * @return $this
2577
     */
2578 1
    public function ceilUnit($unit, $precision = 1)
2579
    {
2580 1
        return $this->roundUnit($unit, $precision, 'ceil');
2581
    }
2582

2583
    /**
2584
     * Round the current instance second with given precision if specified.
2585
     *
2586
     * @param float|int|string|DateInterval|null $precision
2587
     * @param string                             $function
2588
     *
2589
     * @throws Exception
2590
     *
2591
     * @return $this
2592
     */
2593 1
    public function round($precision = 1, $function = 'round')
2594
    {
2595 1
        return $this->roundWith($precision, $function);
2596
    }
2597

2598
    /**
2599
     * Round the current instance second with given precision if specified.
2600
     *
2601
     * @param float|int|string|DateInterval|null $precision
2602
     *
2603
     * @throws Exception
2604
     *
2605
     * @return $this
2606
     */
2607 1
    public function floor($precision = 1)
2608
    {
2609 1
        return $this->round($precision, 'floor');
2610
    }
2611

2612
    /**
2613
     * Ceil the current instance second with given precision if specified.
2614
     *
2615
     * @param float|int|string|DateInterval|null $precision
2616
     *
2617
     * @throws Exception
2618
     *
2619
     * @return $this
2620
     */
2621 1
    public function ceil($precision = 1)
2622
    {
2623 1
        return $this->round($precision, 'ceil');
2624
    }
2625

2626 1
    private static function withOriginal($interval, $original)
2627
    {
2628 1
        if ($interval instanceof self) {
2629 1
            $interval->originalInput = $original;
2630
        }
2631

2632 1
        return $interval;
2633
    }
2634

2635 1
    private static function standardizeUnit($unit)
2636
    {
2637 1
        $unit = rtrim($unit, 'sz').'s';
2638

2639 1
        return $unit === 'days' ? 'dayz' : $unit;
2640
    }
2641

2642 1
    private static function getFlipCascadeFactors()
2643
    {
2644 1
        if (!self::$flipCascadeFactors) {
2645 1
            self::$flipCascadeFactors = [];
2646

2647 1
            foreach (static::getCascadeFactors() as $to => [$factor, $from]) {
2648 1
                self::$flipCascadeFactors[self::standardizeUnit($from)] = [self::standardizeUnit($to), $factor];
2649
            }
2650
        }
2651

2652 1
        return self::$flipCascadeFactors;
2653
    }
2654

2655
    /**
2656
     * @codeCoverageIgnore
2657
     */
2658
    private function fixNegativeMicroseconds()
2659
    {
2660
        if ($this->s !== 0 || $this->i !== 0 || $this->h !== 0 || $this->d !== 0 || $this->m !== 0 || $this->y !== 0) {
2661
            $this->f = (round($this->f * 1000000) + 1000000) / 1000000;
2662
            $this->s--;
2663

2664
            if ($this->s < 0) {
2665
                $this->s += 60;
2666
                $this->i--;
2667

2668
                if ($this->i < 0) {
2669
                    $this->i += 60;
2670
                    $this->h--;
2671

2672
                    if ($this->h < 0) {
2673
                        $this->h += 24;
2674
                        $this->d--;
2675

2676
                        if ($this->d < 0) {
2677
                            $this->d += 30;
2678
                            $this->m--;
2679

2680
                            if ($this->m < 0) {
2681
                                $this->m += 12;
2682
                                $this->y--;
2683
                            }
2684
                        }
2685
                    }
2686
                }
2687
            }
2688

2689
            return;
2690
        }
2691

2692
        $this->f *= self::NEGATIVE;
2693
        $this->invert();
2694
    }
2695

2696
    /**
2697
     * Work-around for https://bugs.php.net/bug.php?id=77145
2698
     *
2699
     * @SuppressWarnings(UnusedPrivateMethod)
2700
     *
2701
     * @codeCoverageIgnore
2702
     */
2703
    private function fixDiffInterval()
2704
    {
2705
        if ($this->f > 0 && $this->y === -1 && $this->m === 11 && $this->d >= 27 && $this->h === 23 && $this->i === 59 && $this->s === 59) {
2706
            $this->y = 0;
2707
            $this->m = 0;
2708
            $this->d = 0;
2709
            $this->h = 0;
2710
            $this->i = 0;
2711
            $this->s = 0;
2712
            $this->f = (1000000 - round($this->f * 1000000)) / 1000000;
2713
            $this->invert();
2714
        } elseif ($this->f < 0) {
2715
            $this->fixNegativeMicroseconds();
2716
        }
2717
    }
2718

2719 1
    private static function castIntervalToClass(DateInterval $interval, string $className)
2720
    {
2721 1
        $mainClass = DateInterval::class;
2722

2723 1
        if (!is_a($className, $mainClass, true)) {
2724 1
            throw new InvalidCastException("$className is not a sub-class of $mainClass.");
2725
        }
2726

2727 1
        $microseconds = $interval->f;
2728 1
        $instance = new $className(static::getDateIntervalSpec($interval));
2729

2730 1
        if ($instance instanceof self) {
2731 1
            $instance->originalInput = $interval;
2732
        }
2733

2734 1
        if ($microseconds) {
2735 1
            $instance->f = $microseconds;
2736
        }
2737

2738 1
        if ($interval instanceof self && is_a($className, self::class, true)) {
2739 1
            $instance->setStep($interval->getStep());
2740
        }
2741

2742 1
        static::copyNegativeUnits($interval, $instance);
2743

2744 1
        return self::withOriginal($instance, $interval);
2745
    }
2746

2747 1
    private static function copyNegativeUnits(DateInterval $from, DateInterval $to)
2748
    {
2749 1
        $to->invert = $from->invert;
2750

2751 1
        foreach (['y', 'm', 'd', 'h', 'i', 's'] as $unit) {
2752 1
            if ($from->$unit < 0) {
2753 1
                $to->$unit *= self::NEGATIVE;
2754
            }
2755
        }
2756
    }
2757

2758 1
    private function invertCascade(array $values)
2759
    {
2760
        return $this->set(array_map(function ($value) {
2761 1
            return -$value;
2762 1
        }, $values))->doCascade(true)->invert();
2763
    }
2764

2765 1
    private function doCascade(bool $deep)
2766
    {
2767 1
        $originalData = $this->toArray();
2768 1
        $originalData['milliseconds'] = (int) ($originalData['microseconds'] / static::getMicrosecondsPerMillisecond());
2769 1
        $originalData['microseconds'] = $originalData['microseconds'] % static::getMicrosecondsPerMillisecond();
2770 1
        $originalData['daysExcludeWeeks'] = $originalData['days'];
2771 1
        unset($originalData['days']);
2772 1
        $newData = $originalData;
2773

2774 1
        foreach (static::getFlipCascadeFactors() as $source => [$target, $factor]) {
2775 1
            foreach (['source', 'target'] as $key) {
2776 1
                if ($$key === 'dayz') {
2777 1
                    $$key = 'daysExcludeWeeks';
2778
                }
2779
            }
2780

2781 1
            $value = $newData[$source];
2782 1
            $modulo = ($factor + ($value % $factor)) % $factor;
2783 1
            $newData[$source] = $modulo;
2784 1
            $newData[$target] += ($value - $modulo) / $factor;
2785
        }
2786

2787 1
        $positive = null;
2788

2789 1
        if (!$deep) {
2790 1
            foreach ($newData as $value) {
2791 1
                if ($value) {
2792 1
                    if ($positive === null) {
2793 1
                        $positive = ($value > 0);
2794

2795 1
                        continue;
2796
                    }
2797

2798 1
                    if (($value > 0) !== $positive) {
2799 1
                        return $this->invertCascade($originalData)
2800 1
                            ->solveNegativeInterval();
2801
                    }
2802
                }
2803
            }
2804
        }
2805

2806 1
        return $this->set($newData)
2807 1
            ->solveNegativeInterval();
2808
    }
2809
}

Read our documentation on viewing source code .

Loading