Boundaries.php
2 years ago
Cast.php
2 years ago
Comparison.php
1 year ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
1 month ago
DeprecatedProperties.php
2 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
2 years ago
Localization.php
1 month ago
Macro.php
2 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
2 years ago
Mutability.php
2 years ago
ObjectInitialisation.php
2 years ago
Options.php
1 year ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
1 month ago
Timestamp.php
1 month ago
ToStringFormat.php
2 years ago
Units.php
2 years ago
Week.php
2 years ago
Rounding.php
215 lines
| 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 IAWPSCOPED\Carbon\Traits; |
| 12 | |
| 13 | use IAWPSCOPED\Carbon\CarbonInterface; |
| 14 | use IAWPSCOPED\Carbon\Exceptions\UnknownUnitException; |
| 15 | /** |
| 16 | * Trait Rounding. |
| 17 | * |
| 18 | * Round, ceil, floor units. |
| 19 | * |
| 20 | * Depends on the following methods: |
| 21 | * |
| 22 | * @method static copy() |
| 23 | * @method static startOfWeek(int $weekStartsAt = null) |
| 24 | * @internal |
| 25 | */ |
| 26 | trait Rounding |
| 27 | { |
| 28 | use IntervalRounding; |
| 29 | /** |
| 30 | * Round the current instance at the given unit with given precision if specified and the given function. |
| 31 | * |
| 32 | * @param string $unit |
| 33 | * @param float|int $precision |
| 34 | * @param string $function |
| 35 | * |
| 36 | * @return CarbonInterface |
| 37 | */ |
| 38 | public function roundUnit($unit, $precision = 1, $function = 'round') |
| 39 | { |
| 40 | $metaUnits = [ |
| 41 | // @call roundUnit |
| 42 | 'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'], |
| 43 | // @call roundUnit |
| 44 | 'century' => [static::YEARS_PER_CENTURY, 'year'], |
| 45 | // @call roundUnit |
| 46 | 'decade' => [static::YEARS_PER_DECADE, 'year'], |
| 47 | // @call roundUnit |
| 48 | 'quarter' => [static::MONTHS_PER_QUARTER, 'month'], |
| 49 | // @call roundUnit |
| 50 | 'millisecond' => [1000, 'microsecond'], |
| 51 | ]; |
| 52 | $normalizedUnit = static::singularUnit($unit); |
| 53 | $ranges = \array_merge(static::getRangesByUnit($this->daysInMonth), [ |
| 54 | // @call roundUnit |
| 55 | 'microsecond' => [0, 999999], |
| 56 | ]); |
| 57 | $factor = 1; |
| 58 | if ($normalizedUnit === 'week') { |
| 59 | $normalizedUnit = 'day'; |
| 60 | $precision *= static::DAYS_PER_WEEK; |
| 61 | } |
| 62 | if (isset($metaUnits[$normalizedUnit])) { |
| 63 | [$factor, $normalizedUnit] = $metaUnits[$normalizedUnit]; |
| 64 | } |
| 65 | $precision *= $factor; |
| 66 | if (!isset($ranges[$normalizedUnit])) { |
| 67 | throw new UnknownUnitException($unit); |
| 68 | } |
| 69 | $found = \false; |
| 70 | $fraction = 0; |
| 71 | $arguments = null; |
| 72 | $initialValue = null; |
| 73 | $factor = $this->year < 0 ? -1 : 1; |
| 74 | $changes = []; |
| 75 | $minimumInc = null; |
| 76 | foreach ($ranges as $unit => [$minimum, $maximum]) { |
| 77 | if ($normalizedUnit === $unit) { |
| 78 | $arguments = [$this->{$unit}, $minimum]; |
| 79 | $initialValue = $this->{$unit}; |
| 80 | $fraction = $precision - \floor($precision); |
| 81 | $found = \true; |
| 82 | continue; |
| 83 | } |
| 84 | if ($found) { |
| 85 | $delta = $maximum + 1 - $minimum; |
| 86 | $factor /= $delta; |
| 87 | $fraction *= $delta; |
| 88 | $inc = ($this->{$unit} - $minimum) * $factor; |
| 89 | if ($inc !== 0.0) { |
| 90 | $minimumInc = $minimumInc ?? $arguments[0] / \pow(2, 52); |
| 91 | // If value is still the same when adding a non-zero increment/decrement, |
| 92 | // it means precision got lost in the addition |
| 93 | if (\abs($inc) < $minimumInc) { |
| 94 | $inc = $minimumInc * ($inc < 0 ? -1 : 1); |
| 95 | } |
| 96 | // If greater than $precision, assume precision loss caused an overflow |
| 97 | if ($function !== 'floor' || \abs($arguments[0] + $inc - $initialValue) >= $precision) { |
| 98 | $arguments[0] += $inc; |
| 99 | } |
| 100 | } |
| 101 | $changes[$unit] = \round($minimum + ($fraction ? $fraction * $function(($this->{$unit} - $minimum) / $fraction) : 0)); |
| 102 | // Cannot use modulo as it lose double precision |
| 103 | while ($changes[$unit] >= $delta) { |
| 104 | $changes[$unit] -= $delta; |
| 105 | } |
| 106 | $fraction -= \floor($fraction); |
| 107 | } |
| 108 | } |
| 109 | [$value, $minimum] = $arguments; |
| 110 | $normalizedValue = \floor($function(($value - $minimum) / $precision) * $precision + $minimum); |
| 111 | /** @var CarbonInterface $result */ |
| 112 | $result = $this; |
| 113 | foreach ($changes as $unit => $value) { |
| 114 | $result = $result->{$unit}($value); |
| 115 | } |
| 116 | return $result->{$normalizedUnit}($normalizedValue); |
| 117 | } |
| 118 | /** |
| 119 | * Truncate the current instance at the given unit with given precision if specified. |
| 120 | * |
| 121 | * @param string $unit |
| 122 | * @param float|int $precision |
| 123 | * |
| 124 | * @return CarbonInterface |
| 125 | */ |
| 126 | public function floorUnit($unit, $precision = 1) |
| 127 | { |
| 128 | return $this->roundUnit($unit, $precision, 'floor'); |
| 129 | } |
| 130 | /** |
| 131 | * Ceil the current instance at the given unit with given precision if specified. |
| 132 | * |
| 133 | * @param string $unit |
| 134 | * @param float|int $precision |
| 135 | * |
| 136 | * @return CarbonInterface |
| 137 | */ |
| 138 | public function ceilUnit($unit, $precision = 1) |
| 139 | { |
| 140 | return $this->roundUnit($unit, $precision, 'ceil'); |
| 141 | } |
| 142 | /** |
| 143 | * Round the current instance second with given precision if specified. |
| 144 | * |
| 145 | * @param float|int|string|\DateInterval|null $precision |
| 146 | * @param string $function |
| 147 | * |
| 148 | * @return CarbonInterface |
| 149 | */ |
| 150 | public function round($precision = 1, $function = 'round') |
| 151 | { |
| 152 | return $this->roundWith($precision, $function); |
| 153 | } |
| 154 | /** |
| 155 | * Round the current instance second with given precision if specified. |
| 156 | * |
| 157 | * @param float|int|string|\DateInterval|null $precision |
| 158 | * |
| 159 | * @return CarbonInterface |
| 160 | */ |
| 161 | public function floor($precision = 1) |
| 162 | { |
| 163 | return $this->round($precision, 'floor'); |
| 164 | } |
| 165 | /** |
| 166 | * Ceil the current instance second with given precision if specified. |
| 167 | * |
| 168 | * @param float|int|string|\DateInterval|null $precision |
| 169 | * |
| 170 | * @return CarbonInterface |
| 171 | */ |
| 172 | public function ceil($precision = 1) |
| 173 | { |
| 174 | return $this->round($precision, 'ceil'); |
| 175 | } |
| 176 | /** |
| 177 | * Round the current instance week. |
| 178 | * |
| 179 | * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week |
| 180 | * |
| 181 | * @return CarbonInterface |
| 182 | */ |
| 183 | public function roundWeek($weekStartsAt = null) |
| 184 | { |
| 185 | return $this->closest($this->avoidMutation()->floorWeek($weekStartsAt), $this->avoidMutation()->ceilWeek($weekStartsAt)); |
| 186 | } |
| 187 | /** |
| 188 | * Truncate the current instance week. |
| 189 | * |
| 190 | * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week |
| 191 | * |
| 192 | * @return CarbonInterface |
| 193 | */ |
| 194 | public function floorWeek($weekStartsAt = null) |
| 195 | { |
| 196 | return $this->startOfWeek($weekStartsAt); |
| 197 | } |
| 198 | /** |
| 199 | * Ceil the current instance week. |
| 200 | * |
| 201 | * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week |
| 202 | * |
| 203 | * @return CarbonInterface |
| 204 | */ |
| 205 | public function ceilWeek($weekStartsAt = null) |
| 206 | { |
| 207 | if ($this->isMutable()) { |
| 208 | $startOfWeek = $this->avoidMutation()->startOfWeek($weekStartsAt); |
| 209 | return $startOfWeek != $this ? $this->startOfWeek($weekStartsAt)->addWeek() : $this; |
| 210 | } |
| 211 | $startOfWeek = $this->startOfWeek($weekStartsAt); |
| 212 | return $startOfWeek != $this ? $startOfWeek->addWeek() : $this->avoidMutation(); |
| 213 | } |
| 214 | } |
| 215 |