Blame view
public/vendor/nesbot/carbon/src/Carbon/Traits/Week.php
7.22 KB
86143e36f Коммит вторник |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
<?php /** * This file is part of the Carbon package. * * (c) Brian Nesbitt <brian@nesbot.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Carbon\Traits; /** * Trait Week. * * week and ISO week number, year and count in year. * * Depends on the following properties: * * @property int $daysInYear * @property int $dayOfWeek * @property int $dayOfYear * @property int $year * * Depends on the following methods: * * @method static addWeeks(int $weeks = 1) * @method static copy() * @method static dayOfYear(int $dayOfYear) * @method string getTranslationMessage(string $key, ?string $locale = null, ?string $default = null, $translator = null) * @method static next(int|string $day = null) * @method static startOfWeek(int $day = 1) * @method static subWeeks(int $weeks = 1) * @method static year(int $year = null) */ trait Week { /** * Set/get the week number of year using given first day of week and first * day of year included in the first week. Or use ISO format if no settings * given. * * @param int|null $year if null, act as a getter, if not null, set the year and return current instance. * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) * @param int|null $dayOfYear first day of year included in the week #1 * * @return int|static */ public function isoWeekYear($year = null, $dayOfWeek = null, $dayOfYear = null) { return $this->weekYear( $year, $dayOfWeek ?? 1, $dayOfYear ?? 4 ); } /** * Set/get the week number of year using given first day of week and first * day of year included in the first week. Or use US format if no settings * given (Sunday / Jan 6). * * @param int|null $year if null, act as a getter, if not null, set the year and return current instance. * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) * @param int|null $dayOfYear first day of year included in the week #1 * * @return int|static */ public function weekYear($year = null, $dayOfWeek = null, $dayOfYear = null) { $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; if ($year !== null) { $year = (int) round($year); if ($this->weekYear(null, $dayOfWeek, $dayOfYear) === $year) { return $this->avoidMutation(); } $week = $this->week(null, $dayOfWeek, $dayOfYear); $day = $this->dayOfWeek; $date = $this->year($year); switch ($date->weekYear(null, $dayOfWeek, $dayOfYear) - $year) { case 1: $date = $date->subWeeks(26); break; case -1: $date = $date->addWeeks(26); break; } $date = $date->addWeeks($week - $date->week(null, $dayOfWeek, $dayOfYear))->startOfWeek($dayOfWeek); if ($date->dayOfWeek === $day) { return $date; } return $date->next($day); } $year = $this->year; $day = $this->dayOfYear; $date = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); if ($date->year === $year && $day < $date->dayOfYear) { return $year - 1; } $date = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); if ($date->year === $year && $day >= $date->dayOfYear) { return $year + 1; } return $year; } /** * Get the number of weeks of the current week-year using given first day of week and first * day of year included in the first week. Or use ISO format if no settings * given. * * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) * @param int|null $dayOfYear first day of year included in the week #1 * * @return int */ public function isoWeeksInYear($dayOfWeek = null, $dayOfYear = null) { return $this->weeksInYear( $dayOfWeek ?? 1, $dayOfYear ?? 4 ); } /** * Get the number of weeks of the current week-year using given first day of week and first * day of year included in the first week. Or use US format if no settings * given (Sunday / Jan 6). * * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) * @param int|null $dayOfYear first day of year included in the week #1 * * @return int */ public function weeksInYear($dayOfWeek = null, $dayOfYear = null) { $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; $year = $this->year; $start = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); $startDay = $start->dayOfYear; if ($start->year !== $year) { $startDay -= $start->daysInYear; } $end = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); $endDay = $end->dayOfYear; if ($end->year !== $year) { $endDay += $this->daysInYear; } return (int) round(($endDay - $startDay) / 7); } /** * Get/set the week number using given first day of week and first * day of year included in the first week. Or use US format if no settings * given (Sunday / Jan 6). * * @param int|null $week * @param int|null $dayOfWeek * @param int|null $dayOfYear * * @return int|static */ public function week($week = null, $dayOfWeek = null, $dayOfYear = null) { $date = $this; $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; if ($week !== null) { return $date->addWeeks(round($week) - $this->week(null, $dayOfWeek, $dayOfYear)); } $start = $date->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); $end = $date->avoidMutation()->startOfWeek($dayOfWeek); if ($start > $end) { $start = $start->subWeeks(26)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); } $week = (int) ($start->diffInDays($end) / 7 + 1); return $week > $end->weeksInYear($dayOfWeek, $dayOfYear) ? 1 : $week; } /** * Get/set the week number using given first day of week and first * day of year included in the first week. Or use ISO format if no settings * given. * * @param int|null $week * @param int|null $dayOfWeek * @param int|null $dayOfYear * * @return int|static */ public function isoWeek($week = null, $dayOfWeek = null, $dayOfYear = null) { return $this->week( $week, $dayOfWeek ?? 1, $dayOfYear ?? 4 ); } } |