Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
9 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
9 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
9 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Units.php
234 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\CarbonConverterInterface; |
| 5 | use MailPoetVendor\Carbon\CarbonInterface; |
| 6 | use MailPoetVendor\Carbon\CarbonInterval; |
| 7 | use MailPoetVendor\Carbon\Exceptions\UnitException; |
| 8 | use Closure; |
| 9 | use DateInterval; |
| 10 | use MailPoetVendor\DateMalformedStringException; |
| 11 | use ReturnTypeWillChange; |
| 12 | trait Units |
| 13 | { |
| 14 | public function addRealUnit($unit, $value = 1) |
| 15 | { |
| 16 | switch ($unit) { |
| 17 | // @call addRealUnit |
| 18 | case 'micro': |
| 19 | // @call addRealUnit |
| 20 | case 'microsecond': |
| 21 | $diff = $this->microsecond + $value; |
| 22 | $time = $this->getTimestamp(); |
| 23 | $seconds = (int) \floor($diff / static::MICROSECONDS_PER_SECOND); |
| 24 | $time += $seconds; |
| 25 | $diff -= $seconds * static::MICROSECONDS_PER_SECOND; |
| 26 | $microtime = \str_pad((string) $diff, 6, '0', \STR_PAD_LEFT); |
| 27 | $tz = $this->tz; |
| 28 | return $this->tz('UTC')->modify("@{$time}.{$microtime}")->tz($tz); |
| 29 | // @call addRealUnit |
| 30 | case 'milli': |
| 31 | // @call addRealUnit |
| 32 | case 'millisecond': |
| 33 | return $this->addRealUnit('microsecond', $value * static::MICROSECONDS_PER_MILLISECOND); |
| 34 | // @call addRealUnit |
| 35 | case 'second': |
| 36 | break; |
| 37 | // @call addRealUnit |
| 38 | case 'minute': |
| 39 | $value *= static::SECONDS_PER_MINUTE; |
| 40 | break; |
| 41 | // @call addRealUnit |
| 42 | case 'hour': |
| 43 | $value *= static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 44 | break; |
| 45 | // @call addRealUnit |
| 46 | case 'day': |
| 47 | $value *= static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 48 | break; |
| 49 | // @call addRealUnit |
| 50 | case 'week': |
| 51 | $value *= static::DAYS_PER_WEEK * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 52 | break; |
| 53 | // @call addRealUnit |
| 54 | case 'month': |
| 55 | $value *= 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 56 | break; |
| 57 | // @call addRealUnit |
| 58 | case 'quarter': |
| 59 | $value *= static::MONTHS_PER_QUARTER * 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 60 | break; |
| 61 | // @call addRealUnit |
| 62 | case 'year': |
| 63 | $value *= 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 64 | break; |
| 65 | // @call addRealUnit |
| 66 | case 'decade': |
| 67 | $value *= static::YEARS_PER_DECADE * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 68 | break; |
| 69 | // @call addRealUnit |
| 70 | case 'century': |
| 71 | $value *= static::YEARS_PER_CENTURY * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 72 | break; |
| 73 | // @call addRealUnit |
| 74 | case 'millennium': |
| 75 | $value *= static::YEARS_PER_MILLENNIUM * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
| 76 | break; |
| 77 | default: |
| 78 | if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) { |
| 79 | throw new UnitException("Invalid unit for real timestamp add/sub: '{$unit}'"); |
| 80 | } |
| 81 | return $this; |
| 82 | } |
| 83 | return $this->setTimestamp((int) ($this->getTimestamp() + $value)); |
| 84 | } |
| 85 | public function subRealUnit($unit, $value = 1) |
| 86 | { |
| 87 | return $this->addRealUnit($unit, -$value); |
| 88 | } |
| 89 | public static function isModifiableUnit($unit) |
| 90 | { |
| 91 | static $modifiableUnits = [ |
| 92 | // @call addUnit |
| 93 | 'millennium', |
| 94 | // @call addUnit |
| 95 | 'century', |
| 96 | // @call addUnit |
| 97 | 'decade', |
| 98 | // @call addUnit |
| 99 | 'quarter', |
| 100 | // @call addUnit |
| 101 | 'week', |
| 102 | // @call addUnit |
| 103 | 'weekday', |
| 104 | ]; |
| 105 | return \in_array($unit, $modifiableUnits, \true) || \in_array($unit, static::$units, \true); |
| 106 | } |
| 107 | public function rawAdd(DateInterval $interval) |
| 108 | { |
| 109 | return parent::add($interval); |
| 110 | } |
| 111 | #[\ReturnTypeWillChange] |
| 112 | public function add($unit, $value = 1, $overflow = null) |
| 113 | { |
| 114 | if (\is_string($unit) && \func_num_args() === 1) { |
| 115 | $unit = CarbonInterval::make($unit, [], \true); |
| 116 | } |
| 117 | if ($unit instanceof CarbonConverterInterface) { |
| 118 | return $this->resolveCarbon($unit->convertDate($this, \false)); |
| 119 | } |
| 120 | if ($unit instanceof Closure) { |
| 121 | return $this->resolveCarbon($unit($this, \false)); |
| 122 | } |
| 123 | if ($unit instanceof DateInterval) { |
| 124 | return parent::add($unit); |
| 125 | } |
| 126 | if (\is_numeric($unit)) { |
| 127 | [$value, $unit] = [$unit, $value]; |
| 128 | } |
| 129 | return $this->addUnit($unit, $value, $overflow); |
| 130 | } |
| 131 | public function addUnit($unit, $value = 1, $overflow = null) |
| 132 | { |
| 133 | $originalArgs = \func_get_args(); |
| 134 | $date = $this; |
| 135 | if (!\is_numeric($value) || !(float) $value) { |
| 136 | return $date->isMutable() ? $date : $date->avoidMutation(); |
| 137 | } |
| 138 | $unit = self::singularUnit($unit); |
| 139 | $metaUnits = ['millennium' => [static::YEARS_PER_MILLENNIUM, 'year'], 'century' => [static::YEARS_PER_CENTURY, 'year'], 'decade' => [static::YEARS_PER_DECADE, 'year'], 'quarter' => [static::MONTHS_PER_QUARTER, 'month']]; |
| 140 | if (isset($metaUnits[$unit])) { |
| 141 | [$factor, $unit] = $metaUnits[$unit]; |
| 142 | $value *= $factor; |
| 143 | } |
| 144 | if ($unit === 'weekday') { |
| 145 | $weekendDays = static::getWeekendDays(); |
| 146 | if ($weekendDays !== [static::SATURDAY, static::SUNDAY]) { |
| 147 | $absoluteValue = \abs($value); |
| 148 | $sign = $value / \max(1, $absoluteValue); |
| 149 | $weekDaysCount = 7 - \min(6, \count(\array_unique($weekendDays))); |
| 150 | $weeks = \floor($absoluteValue / $weekDaysCount); |
| 151 | for ($diff = $absoluteValue % $weekDaysCount; $diff; $diff--) { |
| 152 | $date = $date->addDays($sign); |
| 153 | while (\in_array($date->dayOfWeek, $weekendDays, \true)) { |
| 154 | $date = $date->addDays($sign); |
| 155 | } |
| 156 | } |
| 157 | $value = $weeks * $sign; |
| 158 | $unit = 'week'; |
| 159 | } |
| 160 | $timeString = $date->toTimeString(); |
| 161 | } elseif ($canOverflow = \in_array($unit, ['month', 'year']) && ($overflow === \false || $overflow === null && ($ucUnit = \ucfirst($unit) . 's') && !($this->{'local' . $ucUnit . 'Overflow'} ?? static::{'shouldOverflow' . $ucUnit}()))) { |
| 162 | $day = $date->day; |
| 163 | } |
| 164 | $value = (int) $value; |
| 165 | if ($unit === 'milli' || $unit === 'millisecond') { |
| 166 | $unit = 'microsecond'; |
| 167 | $value *= static::MICROSECONDS_PER_MILLISECOND; |
| 168 | } |
| 169 | // Work-around for bug https://bugs.php.net/bug.php?id=75642 |
| 170 | if ($unit === 'micro' || $unit === 'microsecond') { |
| 171 | $microseconds = $this->micro + $value; |
| 172 | $second = (int) \floor($microseconds / static::MICROSECONDS_PER_SECOND); |
| 173 | $microseconds %= static::MICROSECONDS_PER_SECOND; |
| 174 | if ($microseconds < 0) { |
| 175 | $microseconds += static::MICROSECONDS_PER_SECOND; |
| 176 | } |
| 177 | $date = $date->microseconds($microseconds); |
| 178 | $unit = 'second'; |
| 179 | $value = $second; |
| 180 | } |
| 181 | try { |
| 182 | $date = $date->modify("{$value} {$unit}"); |
| 183 | if (isset($timeString)) { |
| 184 | $date = $date->setTimeFromTimeString($timeString); |
| 185 | } elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date->day) { |
| 186 | $date = $date->modify('last day of previous month'); |
| 187 | } |
| 188 | } catch (DateMalformedStringException $ignoredException) { |
| 189 | // @codeCoverageIgnore |
| 190 | $date = null; |
| 191 | // @codeCoverageIgnore |
| 192 | } |
| 193 | if (!$date) { |
| 194 | throw new UnitException('Unable to add unit ' . \var_export($originalArgs, \true)); |
| 195 | } |
| 196 | return $date; |
| 197 | } |
| 198 | public function subUnit($unit, $value = 1, $overflow = null) |
| 199 | { |
| 200 | return $this->addUnit($unit, -$value, $overflow); |
| 201 | } |
| 202 | public function rawSub(DateInterval $interval) |
| 203 | { |
| 204 | return parent::sub($interval); |
| 205 | } |
| 206 | #[\ReturnTypeWillChange] |
| 207 | public function sub($unit, $value = 1, $overflow = null) |
| 208 | { |
| 209 | if (\is_string($unit) && \func_num_args() === 1) { |
| 210 | $unit = CarbonInterval::make($unit, [], \true); |
| 211 | } |
| 212 | if ($unit instanceof CarbonConverterInterface) { |
| 213 | return $this->resolveCarbon($unit->convertDate($this, \true)); |
| 214 | } |
| 215 | if ($unit instanceof Closure) { |
| 216 | return $this->resolveCarbon($unit($this, \true)); |
| 217 | } |
| 218 | if ($unit instanceof DateInterval) { |
| 219 | return parent::sub($unit); |
| 220 | } |
| 221 | if (\is_numeric($unit)) { |
| 222 | [$value, $unit] = [$unit, $value]; |
| 223 | } |
| 224 | return $this->addUnit($unit, -(float) $value, $overflow); |
| 225 | } |
| 226 | public function subtract($unit, $value = 1, $overflow = null) |
| 227 | { |
| 228 | if (\is_string($unit) && \func_num_args() === 1) { |
| 229 | $unit = CarbonInterval::make($unit, [], \true); |
| 230 | } |
| 231 | return $this->sub($unit, $value, $overflow); |
| 232 | } |
| 233 | } |
| 234 |