Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Rounding.php

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