diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index ac7a4f4c9..b9c435151 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -616,7 +616,7 @@ interface CarbonInterface extends DateTimeInterface, JsonSerializable * @param string $method magic method name called * @param array $parameters parameters list * - * @throws \BadMethodCallException|\ReflectionException + * @throws UnknownMethodException|BadMethodCallException|ReflectionException|Throwable * * @return mixed */ @@ -628,7 +628,7 @@ public function __call($method, $parameters); * @param string $method magic method name called * @param array $parameters parameters list * - * @throws \BadMethodCallException + * @throws BadMethodCallException * * @return mixed */ @@ -647,6 +647,8 @@ public function __clone(); * * @param string|null $time * @param DateTimeZone|string|null $tz + * + * @throws InvalidFormatException */ public function __construct($time = null, $tz = null); @@ -662,7 +664,7 @@ public function __debugInfo(); * * @param string $name * - * @throws InvalidArgumentException|ReflectionException + * @throws UnknownGetterException * * @return string|int|bool|DateTimeZone|null */ @@ -683,7 +685,7 @@ public function __isset($name); * @param string $name * @param string|int|DateTimeZone $value * - * @throws InvalidArgumentException|ReflectionException + * @throws UnknownSetterException|ReflectionException * * @return void */ @@ -876,7 +878,7 @@ public function calendar($referenceTime = null, array $formats = []); * Return the Carbon instance passed through, a now instance in the same timezone * if null given or parse the input if string given. * - * @param \Carbon\Carbon|\Carbon\CarbonPeriod|\Carbon\CarbonInterval|\DateInterval|\DatePeriod|\DateTimeInterface|string|null $date + * @param Carbon|\Carbon\CarbonPeriod|\Carbon\CarbonInterval|\DateInterval|\DatePeriod|DateTimeInterface|string|null $date * * @return static */ @@ -990,7 +992,7 @@ public function copy(); * @param int|null $second * @param DateTimeZone|string|null $tz * - * @throws Exception|InvalidArgumentException + * @throws InvalidFormatException * * @return static */ @@ -1004,7 +1006,7 @@ public static function create($year = 0, $month = 1, $day = 1, $hour = 0, $minut * @param int|null $day * @param DateTimeZone|string|null $tz * - * @throws Exception|InvalidArgumentException + * @throws InvalidFormatException * * @return static */ @@ -1017,7 +1019,7 @@ public static function createFromDate($year = null, $month = null, $day = null, * @param string $time * @param DateTimeZone|string|false|null $tz * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static|false */ @@ -1032,11 +1034,11 @@ public static function createFromFormat($format, $time, $tz = null); * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) * @param TranslatorInterface $translator optional custom translator to use for macro-formats * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static|false */ - public static function createFromIsoFormat($format, $time, $tz = null, $locale = self::DEFAULT_LOCALE, $translator = null); + public static function createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null); /** * Create a Carbon instance from a specific format and a string in a given language. @@ -1046,7 +1048,7 @@ public static function createFromIsoFormat($format, $time, $tz = null, $locale = * @param string $time * @param DateTimeZone|string|false|null $tz * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static|false */ @@ -1060,7 +1062,7 @@ public static function createFromLocaleFormat($format, $locale, $time, $tz = nul * @param string $time * @param DateTimeZone|string|false|null $tz * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static|false */ @@ -1074,7 +1076,7 @@ public static function createFromLocaleIsoFormat($format, $locale, $time, $tz = * @param int|null $second * @param DateTimeZone|string|null $tz * - * @throws Exception|InvalidArgumentException + * @throws InvalidFormatException * * @return static */ @@ -1086,7 +1088,7 @@ public static function createFromTime($hour = 0, $minute = 0, $second = 0, $tz = * @param string $time * @param DateTimeZone|string|null $tz * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static */ @@ -1129,7 +1131,7 @@ public static function createFromTimestampUTC($timestamp); * @param int|null $day * @param DateTimeZone|string|null $tz * - * @throws Exception + * @throws InvalidFormatException * * @return static */ @@ -1147,7 +1149,7 @@ public static function createMidnightDate($year = null, $month = null, $day = nu * If $hour is not null then the default values for $minute and $second * will be 0. * - * If one of the set values is not valid, an \InvalidArgumentException + * If one of the set values is not valid, an InvalidDateException * will be thrown. * * @param int|null $year @@ -1158,7 +1160,7 @@ public static function createMidnightDate($year = null, $month = null, $day = nu * @param int|null $second * @param DateTimeZone|string|null $tz * - * @throws Exception + * @throws InvalidDateException * * @return static|false */ @@ -1924,7 +1926,7 @@ public function fromNow($syntax = null, $short = false, $parts = 1, $options = n * * @param string $value * - * @throws \InvalidArgumentException + * @throws InvalidFormatException * * @return static */ @@ -1945,7 +1947,7 @@ public static function genericMacro($macro, $priority = 0); * * @param string $name * - * @throws InvalidArgumentException|ReflectionException + * @throws UnknownGetterException * * @return string|int|bool|DateTimeZone|null */ @@ -2224,18 +2226,20 @@ public static function getTranslationMessageWith($translator, string $key, strin public static function getTranslator(); /** - * Get the last day of week + * Get the last day of week. + * + * @param string $locale local to consider the last day of week. * * @return int */ - public static function getWeekEndsAt(); + public static function getWeekEndsAt(string $locale = null): int; /** - * Get the first day of week + * Get the first day of week. * * @return int */ - public static function getWeekStartsAt(); + public static function getWeekStartsAt(string $locale = null): int; /** * Get weekend days @@ -3076,7 +3080,7 @@ public static function macro($name, $macro); * * @param mixed $var * - * @throws Exception + * @throws InvalidFormatException * * @return static|null */ @@ -3327,7 +3331,7 @@ public function ordinal(string $key, string $period = null): string; * @param string|null $time * @param DateTimeZone|string|null $tz * - * @throws Exception + * @throws InvalidFormatException * * @return static */ @@ -3340,7 +3344,7 @@ public static function parse($time = null, $tz = null); * @param string $locale * @param DateTimeZone|string|null $tz * - * @throws Exception + * @throws InvalidFormatException * * @return static */ @@ -3399,7 +3403,7 @@ public function range($end = null, $interval = null, $unit = null); * @param string $time * @param DateTimeZone|string|false|null $tz * - * @throws InvalidArgumentException + * @throws InvalidFormatException * * @return static|false */ @@ -3424,7 +3428,7 @@ public function rawFormat($format); * @param string|null $time * @param DateTimeZone|string|null $tz * - * @throws Exception + * @throws InvalidFormatException * * @return static */ @@ -3537,7 +3541,7 @@ public static function serializeUsing($callback); * @param string|array $name * @param string|int|DateTimeZone $value * - * @throws InvalidArgumentException|ReflectionException + * @throws ImmutableException|UnknownSetterException * * @return $this */ @@ -3559,7 +3563,7 @@ public function setDate($year, $month, $day); /** * Set the year, month, and date for this instance to that of the passed instance. * - * @param \Carbon\Carbon|\DateTimeInterface $date now if null + * @param Carbon|DateTimeInterface $date now if null * * @return static */ @@ -3583,7 +3587,7 @@ public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $m /** * Set the date and time for this instance to that of the passed instance. * - * @param \Carbon\Carbon|\DateTimeInterface $date + * @param Carbon|DateTimeInterface $date * * @return static */ @@ -3693,7 +3697,7 @@ public function setTime($hour, $minute, $second = 0, $microseconds = 0); /** * Set the hour, minute, second and microseconds for this instance to that of the passed instance. * - * @param \Carbon\Carbon|\DateTimeInterface $date now if null + * @param Carbon|DateTimeInterface $date now if null * * @return static */ @@ -3782,34 +3786,6 @@ public function setUnitNoOverflow($valueUnit, $value, $overflowUnit); */ public static function setUtf8($utf8); - /** - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * - * Set the last day of week - * - * @param int $day - * - * @return void - */ - public static function setWeekEndsAt($day); - - /** - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * - * Set the first day of week - * - * @param int $day week start day - * - * @return void - */ - public static function setWeekStartsAt($day); - /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather consider week-end is always saturday and sunday, and if you have some custom @@ -4604,7 +4580,7 @@ public static function tomorrow($tz = null); * * @return string */ - public function translate(string $key, array $parameters = [], $number = null, \Symfony\Contracts\Translation\TranslatorInterface $translator = null, bool $altNumbers = false): string; + public function translate(string $key, array $parameters = [], $number = null, \Symfony\Contracts\Translation\TranslatorInterface $translator = null, bool $altNumbers = false); /** * Returns the alternative number for a given integer if available in the current locale. @@ -4631,7 +4607,7 @@ public function translateNumber(int $number): string; * * @return string */ - public static function translateTimeString($timeString, $from = null, $to = null, $mode = 15); + public static function translateTimeString(string $timeString, $from = null, $to = null, $mode = 15): string; /** * Translate a time string from the current locale (`$date->locale()`) to an other. @@ -4653,7 +4629,7 @@ public function translateTimeStringTo($timeString, $to = null); * * @return string */ - public static function translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string; + public static function translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null); /** * Format as ->format() do (using date replacements patterns from http://php.net/manual/fr/function.date.php) diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 190e7bf0b..7ec74e44f 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -26,7 +26,7 @@ * will be 0. * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en_ISO', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. @@ -42,7 +42,7 @@ * values for $minute and $second will be their now() values. * If $hour is not null then the default values for $minute and $second * will be 0. - * If one of the set values is not valid, an \InvalidArgumentException + * If one of the set values is not valid, an InvalidDateException * will be thrown. * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. @@ -70,8 +70,8 @@ * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. * @method TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week + * @method int getWeekEndsAt(string $locale = null) Get the last day of week. + * @method int getWeekStartsAt(string $locale = null) Get the first day of week. * @method array getWeekendDays() Get weekend days * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. * @method bool hasMacro($name) Checks if macro is registered globally. @@ -159,16 +159,6 @@ * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use UTF-8 language packages on every machine. * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather consider week-end is always saturday and sunday, and if you have some custom * week-end days to handle, give to those days an other name and create a macro for them: @@ -193,7 +183,7 @@ * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). * @method Carbon today($tz = null) Create a Carbon instance for today. * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateTimeString(string $timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index 095772b4c..9c32bd2fe 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -26,7 +26,7 @@ * will be 0. * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. * @method CarbonImmutable|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en_ISO', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). * @method CarbonImmutable|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. @@ -42,7 +42,7 @@ * values for $minute and $second will be their now() values. * If $hour is not null then the default values for $minute and $second * will be 0. - * If one of the set values is not valid, an \InvalidArgumentException + * If one of the set values is not valid, an InvalidDateException * will be thrown. * @method CarbonImmutable disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. @@ -70,8 +70,8 @@ * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. * @method TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week + * @method int getWeekEndsAt(string $locale = null) Get the last day of week. + * @method int getWeekStartsAt(string $locale = null) Get the first day of week. * @method array getWeekendDays() Get weekend days * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. * @method bool hasMacro($name) Checks if macro is registered globally. @@ -159,16 +159,6 @@ * @method CarbonImmutable setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use UTF-8 language packages on every machine. * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather consider week-end is always saturday and sunday, and if you have some custom * week-end days to handle, give to those days an other name and create a macro for them: @@ -193,7 +183,7 @@ * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateTimeString(string $timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. diff --git a/src/Carbon/Lang/en.php b/src/Carbon/Lang/en.php index 7da352dbe..712bd5bd2 100644 --- a/src/Carbon/Lang/en.php +++ b/src/Carbon/Lang/en.php @@ -71,6 +71,14 @@ ) ); }, + 'formats' => [ + 'LT' => 'h:mm A', + 'LTS' => 'h:mm:ss A', + 'L' => 'MM/DD/YYYY', + 'LL' => 'MMMM D, YYYY', + 'LLL' => 'MMMM D, YYYY h:mm A', + 'LLLL' => 'dddd, MMMM D, YYYY h:mm A', + ], 'list' => [', ', ' and '], 'first_day_of_week' => 1, 'day_of_first_week_of_year' => 1, diff --git a/src/Carbon/Lang/sn.php b/src/Carbon/Lang/sn.php index 62c82b108..0d19c8d82 100644 --- a/src/Carbon/Lang/sn.php +++ b/src/Carbon/Lang/sn.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ return array_replace_recursive(require __DIR__.'/en.php', [ + 'first_day_of_week' => 0, 'meridiem' => ['a', 'p'], 'weekdays' => ['Svondo', 'Muvhuro', 'Chipiri', 'Chitatu', 'China', 'Chishanu', 'Mugovera'], 'weekdays_short' => ['Svo', 'Muv', 'Chp', 'Cht', 'Chn', 'Chs', 'Mug'], diff --git a/src/Carbon/Traits/Boundaries.php b/src/Carbon/Traits/Boundaries.php index ae88a3773..a6fb86502 100644 --- a/src/Carbon/Traits/Boundaries.php +++ b/src/Carbon/Traits/Boundaries.php @@ -273,6 +273,7 @@ public function endOfMillennium() public function startOfWeek($weekStartsAt = null) { $date = $this; + while ($date->dayOfWeek !== ($weekStartsAt ?? $this->firstWeekDay)) { $date = $date->subDay(); } diff --git a/src/Carbon/Traits/Creator.php b/src/Carbon/Traits/Creator.php index b53e0fef8..afe946483 100644 --- a/src/Carbon/Traits/Creator.php +++ b/src/Carbon/Traits/Creator.php @@ -694,12 +694,12 @@ public static function createFromIsoFormat($format, $time, $tz = null, $locale = $translator = $translator ?: Translator::get($locale); $formats = [ - 'LT' => static::getTranslationMessageWith($translator, 'formats.LT', $locale, 'h:mm A'), - 'LTS' => static::getTranslationMessageWith($translator, 'formats.LTS', $locale, 'h:mm:ss A'), - 'L' => static::getTranslationMessageWith($translator, 'formats.L', $locale, 'MM/DD/YYYY'), - 'LL' => static::getTranslationMessageWith($translator, 'formats.LL', $locale, 'MMMM D, YYYY'), - 'LLL' => static::getTranslationMessageWith($translator, 'formats.LLL', $locale, 'MMMM D, YYYY h:mm A'), - 'LLLL' => static::getTranslationMessageWith($translator, 'formats.LLLL', $locale, 'dddd, MMMM D, YYYY h:mm A'), + 'LT' => static::getTranslationMessageWith($translator, 'formats.LT', $locale), + 'LTS' => static::getTranslationMessageWith($translator, 'formats.LTS', $locale), + 'L' => static::getTranslationMessageWith($translator, 'formats.L', $locale), + 'LL' => static::getTranslationMessageWith($translator, 'formats.LL', $locale), + 'LLL' => static::getTranslationMessageWith($translator, 'formats.LLL', $locale), + 'LLLL' => static::getTranslationMessageWith($translator, 'formats.LLLL', $locale), ]; } diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index ea507c660..daf49e321 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -23,6 +23,7 @@ use Carbon\Exceptions\UnknownMethodException; use Carbon\Exceptions\UnknownSetterException; use Carbon\Exceptions\UnknownUnitException; +use Carbon\Translator; use Closure; use DateInterval; use DatePeriod; @@ -115,35 +116,35 @@ * @method bool isThursday() Checks if the instance day is thursday. * @method bool isFriday() Checks if the instance day is friday. * @method bool isSaturday() Checks if the instance day is saturday. - * @method bool isSameYear(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same year as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameYear(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same year as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentYear() Checks if the instance is in the same year as the current moment. * @method bool isNextYear() Checks if the instance is in the same year as the current moment next year. * @method bool isLastYear() Checks if the instance is in the same year as the current moment last year. - * @method bool isSameWeek(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same week as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameWeek(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same week as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentWeek() Checks if the instance is in the same week as the current moment. * @method bool isNextWeek() Checks if the instance is in the same week as the current moment next week. * @method bool isLastWeek() Checks if the instance is in the same week as the current moment last week. - * @method bool isSameDay(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same day as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameDay(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same day as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentDay() Checks if the instance is in the same day as the current moment. * @method bool isNextDay() Checks if the instance is in the same day as the current moment next day. * @method bool isLastDay() Checks if the instance is in the same day as the current moment last day. - * @method bool isSameHour(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same hour as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameHour(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same hour as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentHour() Checks if the instance is in the same hour as the current moment. * @method bool isNextHour() Checks if the instance is in the same hour as the current moment next hour. * @method bool isLastHour() Checks if the instance is in the same hour as the current moment last hour. - * @method bool isSameMinute(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same minute as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameMinute(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same minute as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentMinute() Checks if the instance is in the same minute as the current moment. * @method bool isNextMinute() Checks if the instance is in the same minute as the current moment next minute. * @method bool isLastMinute() Checks if the instance is in the same minute as the current moment last minute. - * @method bool isSameSecond(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same second as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameSecond(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same second as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentSecond() Checks if the instance is in the same second as the current moment. * @method bool isNextSecond() Checks if the instance is in the same second as the current moment next second. * @method bool isLastSecond() Checks if the instance is in the same second as the current moment last second. - * @method bool isSameMicro(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameMicro(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentMicro() Checks if the instance is in the same microsecond as the current moment. * @method bool isNextMicro() Checks if the instance is in the same microsecond as the current moment next microsecond. * @method bool isLastMicro() Checks if the instance is in the same microsecond as the current moment last microsecond. - * @method bool isSameMicrosecond(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameMicrosecond(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same microsecond as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentMicrosecond() Checks if the instance is in the same microsecond as the current moment. * @method bool isNextMicrosecond() Checks if the instance is in the same microsecond as the current moment next microsecond. * @method bool isLastMicrosecond() Checks if the instance is in the same microsecond as the current moment last microsecond. @@ -153,15 +154,15 @@ * @method bool isCurrentQuarter() Checks if the instance is in the same quarter as the current moment. * @method bool isNextQuarter() Checks if the instance is in the same quarter as the current moment next quarter. * @method bool isLastQuarter() Checks if the instance is in the same quarter as the current moment last quarter. - * @method bool isSameDecade(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same decade as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameDecade(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same decade as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentDecade() Checks if the instance is in the same decade as the current moment. * @method bool isNextDecade() Checks if the instance is in the same decade as the current moment next decade. * @method bool isLastDecade() Checks if the instance is in the same decade as the current moment last decade. - * @method bool isSameCentury(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same century as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameCentury(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same century as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentCentury() Checks if the instance is in the same century as the current moment. * @method bool isNextCentury() Checks if the instance is in the same century as the current moment next century. * @method bool isLastCentury() Checks if the instance is in the same century as the current moment last century. - * @method bool isSameMillennium(Carbon|DateTimeInterface|string|null $date = null) Checks if the given date is in the same millennium as the instance. If null passed, compare to now (with the same timezone). + * @method bool isSameMillennium(\Carbon\Carbon|\DateTimeInterface|string|null $date = null) Checks if the given date is in the same millennium as the instance. If null passed, compare to now (with the same timezone). * @method bool isCurrentMillennium() Checks if the instance is in the same millennium as the current moment. * @method bool isNextMillennium() Checks if the instance is in the same millennium as the current moment next millennium. * @method bool isLastMillennium() Checks if the instance is in the same millennium as the current moment last millennium. @@ -512,14 +513,14 @@ * @method CarbonInterface floorMicroseconds(float $precision = 1) Truncate the current instance microsecond with given precision. * @method CarbonInterface ceilMicrosecond(float $precision = 1) Ceil the current instance microsecond with given precision. * @method CarbonInterface ceilMicroseconds(float $precision = 1) Ceil the current instance microsecond with given precision. - * @method string shortAbsoluteDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string longAbsoluteDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string shortRelativeDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string longRelativeDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string shortRelativeToNowDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string longRelativeToNowDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string shortRelativeToOtherDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) - * @method string longRelativeToOtherDiffForHumans(DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string shortAbsoluteDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string longAbsoluteDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'Absolute' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string shortRelativeDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string longRelativeDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'Relative' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string shortRelativeToNowDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string longRelativeToNowDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'RelativeToNow' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string shortRelativeToOtherDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (short format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) + * @method string longRelativeToOtherDiffForHumans(\DateTimeInterface $other = null, int $parts = 1) Get the difference (long format, 'RelativeToOther' mode) in a human readable format in the current locale. ($other and $parts parameters can be swapped.) * * */ @@ -691,52 +692,6 @@ public function nowWithSameTz() return static::now($this->getTimezone()); } - /** - * Throws an exception if the given object is not a DateTime and does not implement DateTimeInterface. - * - * @param mixed $date - * @param string|array $other - * - * @throws InvalidTypeException - */ - protected static function expectDateTime($date, $other = []) - { - $message = 'Expected '; - foreach ((array) $other as $expect) { - $message .= "$expect, "; - } - - if (!$date instanceof DateTime && !$date instanceof DateTimeInterface) { - throw new InvalidTypeException( - $message.'DateTime or DateTimeInterface, '. - (is_object($date) ? get_class($date) : gettype($date)).' given' - ); - } - } - - /** - * Return the Carbon instance passed through, a now instance in the same timezone - * if null given or parse the input if string given. - * - * @param Carbon|DateTimeInterface|string|null $date - * - * @return static - */ - protected function resolveCarbon($date = null) - { - if (!$date) { - return $this->nowWithSameTz(); - } - - if (is_string($date)) { - return static::parse($date, $this->getTimezone()); - } - - static::expectDateTime($date, ['null', 'string']); - - return $date instanceof self ? $date : static::instance($date); - } - /** * Return the Carbon instance passed through, a now instance in the same timezone * if null given or parse the input if string given. @@ -917,11 +872,11 @@ public function get($name) // @property-read int 0 through 6 case $name === 'firstWeekDay': - return $this->localTranslator ? ($this->getTranslationMessage('first_day_of_week') ?? 0) : static::getWeekStartsAt(); + return (int) $this->getTranslationMessage('first_day_of_week'); // @property-read int 0 through 6 case $name === 'lastWeekDay': - return $this->localTranslator ? (($this->getTranslationMessage('first_day_of_week') ?? 0) + static::DAYS_PER_WEEK - 1) % static::DAYS_PER_WEEK : static::getWeekEndsAt(); + return (int) static::weekRotate($this->getTranslationMessage('first_day_of_week'), - 1); // @property int 1 through 366 case $name === 'dayOfYear': @@ -1188,26 +1143,6 @@ public function set($name, $value = null) return $this; } - protected function getTranslatedFormByRegExp($baseKey, $keySuffix, $context, $subKey, $defaultValue) - { - $key = $baseKey.$keySuffix; - $standaloneKey = "${key}_standalone"; - $baseTranslation = $this->getTranslationMessage($key); - - if ($baseTranslation instanceof Closure) { - return $baseTranslation($this, $context, $subKey) ?: $defaultValue; - } - - if ( - $this->getTranslationMessage("$standaloneKey.$subKey") && - (!$context || ($regExp = $this->getTranslationMessage("${baseKey}_regexp")) && !preg_match($regExp, $context)) - ) { - $key = $standaloneKey; - } - - return $this->getTranslationMessage("$key.$subKey", null, $defaultValue); - } - /** * Get the translation of the current week day name (with context for languages with multiple forms). * @@ -1295,7 +1230,7 @@ public function dayOfYear($value = null) */ public function weekday($value = null) { - $dayOfWeek = ($this->dayOfWeek + 7 - intval($this->getTranslationMessage('first_day_of_week') ?? 0)) % 7; + $dayOfWeek = static::weekRotate($this->dayOfWeek, -$this->getTranslationMessage('first_day_of_week')); return is_null($value) ? $dayOfWeek : $this->addDays($value - $dayOfWeek); } @@ -1606,57 +1541,28 @@ public static function getDays() /////////////////////////////////////////////////////////////////// /** - * Get the first day of week + * Get the first day of week. * * @return int */ - public static function getWeekStartsAt() + public static function getWeekStartsAt(?string $locale = null): int { - return static::$weekStartsAt; + return static::getTranslationMessageWith( + $locale ? Translator::get($locale) : static::getTranslator(), + 'first_day_of_week' + ); } /** - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. + * Get the last day of week. * - * Set the first day of week - * - * @param int $day week start day - * - * @return void - */ - public static function setWeekStartsAt($day) - { - static::$weekStartsAt = max(0, (7 + $day) % 7); - } - - /** - * Get the last day of week + * @param string $locale local to consider the last day of week. * * @return int */ - public static function getWeekEndsAt() + public static function getWeekEndsAt(?string $locale = null): int { - return static::$weekEndsAt; - } - - /** - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * - * Set the last day of week - * - * @param int $day - * - * @return void - */ - public static function setWeekEndsAt($day) - { - static::$weekEndsAt = max(0, (7 + $day) % 7); + return static::weekRotate(static::getWeekStartsAt($locale), -1); } /** @@ -1769,12 +1675,12 @@ public function formatLocalized($format) public function getIsoFormats($locale = null) { return [ - 'LT' => $this->getTranslationMessage('formats.LT', $locale, 'h:mm A'), - 'LTS' => $this->getTranslationMessage('formats.LTS', $locale, 'h:mm:ss A'), - 'L' => $this->getTranslationMessage('formats.L', $locale, 'MM/DD/YYYY'), - 'LL' => $this->getTranslationMessage('formats.LL', $locale, 'MMMM D, YYYY'), - 'LLL' => $this->getTranslationMessage('formats.LLL', $locale, 'MMMM D, YYYY h:mm A'), - 'LLLL' => $this->getTranslationMessage('formats.LLLL', $locale, 'dddd, MMMM D, YYYY h:mm A'), + 'LT' => $this->getTranslationMessage('formats.LT', $locale), + 'LTS' => $this->getTranslationMessage('formats.LTS', $locale), + 'L' => $this->getTranslationMessage('formats.L', $locale), + 'LL' => $this->getTranslationMessage('formats.LL', $locale), + 'LLL' => $this->getTranslationMessage('formats.LLL', $locale), + 'LLLL' => $this->getTranslationMessage('formats.LLLL', $locale), ]; } @@ -2270,19 +2176,6 @@ public function getOffsetString($separator = ':') return "$symbol$hour$separator$minute"; } - protected static function executeStaticCallable($macro, ...$parameters) - { - return static::bindMacroContext(null, function () use (&$macro, &$parameters) { - if ($macro instanceof Closure) { - $boundMacro = @Closure::bind($macro, null, static::class); - - return call_user_func_array($boundMacro ?: $macro, $parameters); - } - - return call_user_func_array($macro, $parameters); - }); - } - /** * Dynamically handle calls to the class. * @@ -2388,33 +2281,6 @@ public static function pluralUnit(string $unit): string return "${unit}s"; } - protected function executeCallable($macro, ...$parameters) - { - if ($macro instanceof Closure) { - $boundMacro = @$macro->bindTo($this, static::class) ?: @$macro->bindTo(null, static::class); - - return call_user_func_array($boundMacro ?: $macro, $parameters); - } - - return call_user_func_array($macro, $parameters); - } - - protected function executeCallableWithContext($macro, ...$parameters) - { - return static::bindMacroContext($this, function () use (&$macro, &$parameters) { - return $this->executeCallable($macro, ...$parameters); - }); - } - - protected static function getGenericMacros() - { - foreach (static::$globalGenericMacros as $list) { - foreach ($list as $macro) { - yield $macro; - } - } - } - /** * Dynamically handle calls to the class. * @@ -2591,4 +2457,115 @@ public function __call($method, $parameters) return $this->executeCallable($macro, ...$parameters); }); } + + /** + * Throws an exception if the given object is not a DateTime and does not implement DateTimeInterface. + * + * @param mixed $date + * @param string|array $other + * + * @throws InvalidTypeException + */ + protected static function expectDateTime($date, $other = []) + { + $message = 'Expected '; + foreach ((array) $other as $expect) { + $message .= "$expect, "; + } + + if (!$date instanceof DateTime && !$date instanceof DateTimeInterface) { + throw new InvalidTypeException( + $message.'DateTime or DateTimeInterface, '. + (is_object($date) ? get_class($date) : gettype($date)).' given' + ); + } + } + + /** + * Return the Carbon instance passed through, a now instance in the same timezone + * if null given or parse the input if string given. + * + * @param Carbon|DateTimeInterface|string|null $date + * + * @return static + */ + protected function resolveCarbon($date = null) + { + if (!$date) { + return $this->nowWithSameTz(); + } + + if (is_string($date)) { + return static::parse($date, $this->getTimezone()); + } + + static::expectDateTime($date, ['null', 'string']); + + return $date instanceof self ? $date : static::instance($date); + } + + protected static function weekRotate(int $day, int $rotation): int + { + return (static::DAYS_PER_WEEK + $rotation % static::DAYS_PER_WEEK + $day) % static::DAYS_PER_WEEK; + } + + protected function executeCallable($macro, ...$parameters) + { + if ($macro instanceof Closure) { + $boundMacro = @$macro->bindTo($this, static::class) ?: @$macro->bindTo(null, static::class); + + return call_user_func_array($boundMacro ?: $macro, $parameters); + } + + return call_user_func_array($macro, $parameters); + } + + protected function executeCallableWithContext($macro, ...$parameters) + { + return static::bindMacroContext($this, function () use (&$macro, &$parameters) { + return $this->executeCallable($macro, ...$parameters); + }); + } + + protected static function getGenericMacros() + { + foreach (static::$globalGenericMacros as $list) { + foreach ($list as $macro) { + yield $macro; + } + } + } + + protected static function executeStaticCallable($macro, ...$parameters) + { + return static::bindMacroContext(null, function () use (&$macro, &$parameters) { + if ($macro instanceof Closure) { + $boundMacro = @Closure::bind($macro, null, static::class); + + return call_user_func_array($boundMacro ?: $macro, $parameters); + } + + return call_user_func_array($macro, $parameters); + }); + } + + protected function getTranslatedFormByRegExp($baseKey, $keySuffix, $context, $subKey, $defaultValue) + { + $key = $baseKey.$keySuffix; + $standaloneKey = "${key}_standalone"; + $baseTranslation = $this->getTranslationMessage($key); + + if ($baseTranslation instanceof Closure) { + return $baseTranslation($this, $context, $subKey) ?: $defaultValue; + } + + if ( + $this->getTranslationMessage("$standaloneKey.$subKey") && + (!$context || ($regExp = $this->getTranslationMessage("${baseKey}_regexp")) && !preg_match($regExp, $context)) + ) { + $key = $standaloneKey; + } + + return $this->getTranslationMessage("$key.$subKey", null, $defaultValue); + } } diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 28a2f3571..86a72aa91 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -765,10 +765,12 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa } $intSyntax = &$syntax; + if (is_array($syntax)) { $syntax['syntax'] = $syntax['syntax'] ?? null; $intSyntax = &$syntax['syntax']; } + $intSyntax = (int) ($intSyntax === null ? static::DIFF_RELATIVE_AUTO : $intSyntax); $intSyntax = $intSyntax === static::DIFF_RELATIVE_AUTO && $other === null ? static::DIFF_RELATIVE_TO_NOW : $intSyntax; @@ -1070,12 +1072,12 @@ public function calendar($referenceTime = null, array $formats = []) /** @var CarbonInterface $other */ $other = $this->resolveCarbon($referenceTime)->copy()->setTimezone($this->getTimezone())->startOfDay(); $diff = $other->diffInDays($current, false); - $format = $diff < -6 ? 'sameElse' : ( + $format = $diff <= -static::DAYS_PER_WEEK ? 'sameElse' : ( $diff < -1 ? 'lastWeek' : ( $diff < 0 ? 'lastDay' : ( $diff < 1 ? 'sameDay' : ( $diff < 2 ? 'nextDay' : ( - $diff < 7 ? 'nextWeek' : 'sameElse' + $diff < static::DAYS_PER_WEEK ? 'nextWeek' : 'sameElse' ) ) ) diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index c5e78e116..9f2346553 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -172,9 +172,9 @@ public function setLocalTranslator(TranslatorInterface $translator) * @param string|null $locale current locale used if null * @param string|null $default default value if translation returns the key * - * @return string + * @return string|Closure|null */ - public static function getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) + public static function getTranslationMessageWith($translator, string $key, ?string $locale = null, ?string $default = null) { if (!($translator instanceof TranslatorBagInterface && $translator instanceof TranslatorInterface)) { throw new InvalidTypeException( @@ -323,7 +323,7 @@ public function translateNumber(int $number): string * * @return string */ - public static function translateTimeString($timeString, $from = null, $to = null, $mode = CarbonInterface::TRANSLATE_ALL) + public static function translateTimeString(string $timeString, $from = null, $to = null, $mode = CarbonInterface::TRANSLATE_ALL): string { $from = $from ?: static::getLocale(); $to = $to ?: CarbonInterface::DEFAULT_LOCALE; @@ -370,10 +370,10 @@ public static function translateTimeString($timeString, $from = null, $to = null } $$translationKey = array_merge( - $mode & CarbonInterface::TRANSLATE_MONTHS ? static::getTranslationArray($months, 12, $timeString) : [], - $mode & CarbonInterface::TRANSLATE_MONTHS ? static::getTranslationArray($messages['months_short'], 12, $timeString) : [], - $mode & CarbonInterface::TRANSLATE_DAYS ? static::getTranslationArray($weekdays, 7, $timeString) : [], - $mode & CarbonInterface::TRANSLATE_DAYS ? static::getTranslationArray($messages['weekdays_short'], 7, $timeString) : [], + $mode & CarbonInterface::TRANSLATE_MONTHS ? static::getTranslationArray($months, static::MONTHS_PER_YEAR, $timeString) : [], + $mode & CarbonInterface::TRANSLATE_MONTHS ? static::getTranslationArray($messages['months_short'], static::MONTHS_PER_YEAR, $timeString) : [], + $mode & CarbonInterface::TRANSLATE_DAYS ? static::getTranslationArray($weekdays, static::DAYS_PER_WEEK, $timeString) : [], + $mode & CarbonInterface::TRANSLATE_DAYS ? static::getTranslationArray($messages['weekdays_short'], static::DAYS_PER_WEEK, $timeString) : [], $mode & CarbonInterface::TRANSLATE_UNITS ? array_map(function ($unit) use ($messages, $key, $cleanWord) { $parts = explode('|', $messages[$unit]); diff --git a/src/Carbon/Traits/Options.php b/src/Carbon/Traits/Options.php index 1a3743402..ec8ffbb0d 100644 --- a/src/Carbon/Traits/Options.php +++ b/src/Carbon/Traits/Options.php @@ -33,20 +33,6 @@ trait Options */ public static $PHPIntSize = PHP_INT_SIZE; - /** - * First day of week. - * - * @var int - */ - protected static $weekStartsAt = CarbonInterface::MONDAY; - - /** - * Last day of week. - * - * @var int - */ - protected static $weekEndsAt = CarbonInterface::SUNDAY; - /** * Days of weekend. * diff --git a/src/Carbon/Traits/Units.php b/src/Carbon/Traits/Units.php index 79718269a..33b58a307 100644 --- a/src/Carbon/Traits/Units.php +++ b/src/Carbon/Traits/Units.php @@ -231,7 +231,7 @@ public function addUnit($unit, $value = 1, $overflow = null) if ($weekendDays !== [static::SATURDAY, static::SUNDAY]) { $absoluteValue = abs($value); $sign = $value / max(1, $absoluteValue); - $weekDaysCount = 7 - min(6, count(array_unique($weekendDays))); + $weekDaysCount = static::DAYS_PER_WEEK - min(static::DAYS_PER_WEEK - 1, count(array_unique($weekendDays))); $weeks = floor($absoluteValue / $weekDaysCount); for ($diff = $absoluteValue % $weekDaysCount; $diff; $diff--) { diff --git a/src/Carbon/Traits/Week.php b/src/Carbon/Traits/Week.php index 546590ad1..614402a60 100644 --- a/src/Carbon/Traits/Week.php +++ b/src/Carbon/Traits/Week.php @@ -162,7 +162,7 @@ public function weeksInYear($dayOfWeek = null, $dayOfYear = null) $endDay += $this->daysInYear; } - return (int) round(($endDay - $startDay) / 7); + return (int) round(($endDay - $startDay) / static::DAYS_PER_WEEK); } /** @@ -188,10 +188,12 @@ public function week($week = null, $dayOfWeek = null, $dayOfYear = null) $start = $date->copy()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); $end = $date->copy()->startOfWeek($dayOfWeek); + if ($start > $end) { - $start = $start->subWeeks(26)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); + $start = $start->subWeeks(static::WEEKS_PER_YEAR / 2)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); } - $week = (int) ($start->diffInDays($end) / 7 + 1); + + $week = (int) ($start->diffInDays($end) / static::DAYS_PER_WEEK + 1); return $week > $end->weeksInYear($dayOfWeek, $dayOfYear) ? 1 : $week; } diff --git a/tests/Carbon/DayOfWeekModifiersTest.php b/tests/Carbon/DayOfWeekModifiersTest.php index 16a3c40d4..507107fa2 100644 --- a/tests/Carbon/DayOfWeekModifiersTest.php +++ b/tests/Carbon/DayOfWeekModifiersTest.php @@ -31,24 +31,19 @@ public function testSetWeekendDays() $this->assertFalse(Carbon::createFromDate(2018, 2, 16)->isWeekend()); } - public function testGetWeekEndsAt() - { - Carbon::setWeekEndsAt(Carbon::SATURDAY); - $this->assertSame(Carbon::SATURDAY, Carbon::getWeekEndsAt()); - Carbon::setWeekEndsAt(Carbon::SUNDAY); - } - - public function testGetWeekStartsAt() - { - Carbon::setWeekStartsAt(Carbon::TUESDAY); - $this->assertSame(Carbon::TUESDAY, Carbon::getWeekStartsAt()); - Carbon::setWeekStartsAt(Carbon::MONDAY); - } - public function testStartOfWeek() { $d = Carbon::create(1980, 8, 7, 12, 11, 9)->startOfWeek(); $this->assertCarbon($d, 1980, 8, 4, 0, 0, 0); + + Carbon::setLocale('en_UM'); + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en'); + $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('es_US'); + $this->assertSame('domingo', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en_GB'); + $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName); } public function testStartOfWeekFromWeekStart() diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index a588d3762..4eed122d3 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -24,12 +24,6 @@ class LocalizationTest extends AbstractTestCase { - protected function tearDown(): void - { - parent::tearDown(); - Carbon::setLocale('en'); - } - public function testGetTranslator() { /** @var Translator $t */ diff --git a/tests/Carbon/RoundTest.php b/tests/Carbon/RoundTest.php index 3ebf702f9..818581acc 100644 --- a/tests/Carbon/RoundTest.php +++ b/tests/Carbon/RoundTest.php @@ -144,16 +144,6 @@ public function testRoundWeek() $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 19, 0, 0, 0, 0); $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0); $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0); - - Carbon::setWeekStartsAt(Carbon::SUNDAY); - Carbon::setWeekEndsAt(Carbon::SATURDAY); - - $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 18, 0, 0, 0, 0); - $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 25, 0, 0, 0, 0); - $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 18, 0, 0, 0, 0); - - Carbon::setWeekStartsAt(Carbon::MONDAY); - Carbon::setWeekEndsAt(Carbon::SUNDAY); } public function testRoundInvalidArgument() diff --git a/tests/Carbon/SetStartEndOfWeekTest.php b/tests/Carbon/SetStartEndOfWeekTest.php deleted file mode 100644 index 12fe96739..000000000 --- a/tests/Carbon/SetStartEndOfWeekTest.php +++ /dev/null @@ -1,62 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Tests\Carbon; - -use Carbon\Carbon; -use Tests\AbstractTestCase; - -class SetStartEndOfWeekTest extends AbstractTestCase -{ - public function testSetStartOfWeekLessThanMin() - { - Carbon::setWeekStartsAt(Carbon::SUNDAY - 1); - - $this->assertSame(Carbon::SATURDAY, Carbon::getWeekStartsAt()); - } - - public function testSetStartOfWeekMoreThanMax() - { - Carbon::setWeekStartsAt(Carbon::SATURDAY + 1); - - $this->assertSame(Carbon::SUNDAY, Carbon::getWeekStartsAt()); - } - - public function testSetStartOfWeekValid() - { - for ($i = Carbon::SUNDAY; $i < Carbon::SATURDAY; $i++) { - Carbon::setWeekStartsAt($i); - $this->assertSame($i, Carbon::getWeekStartsAt()); - } - } - - public function testSetEndOfWeekLessThanMin() - { - Carbon::setWeekEndsAt(Carbon::SUNDAY - 1); - - $this->assertSame(Carbon::SATURDAY, Carbon::getWeekEndsAt()); - } - - public function testSetEndOfWeekMoreThanMax() - { - Carbon::setWeekEndsAt(Carbon::SATURDAY + 1); - - $this->assertSame(Carbon::SUNDAY, Carbon::getWeekEndsAt()); - } - - public function testSetEndOfWeekValid() - { - for ($i = Carbon::SUNDAY; $i < Carbon::SATURDAY; $i++) { - Carbon::setWeekEndsAt($i); - $this->assertSame($i, Carbon::getWeekEndsAt()); - } - } -} diff --git a/tests/CarbonImmutable/DayOfWeekModifiersTest.php b/tests/CarbonImmutable/DayOfWeekModifiersTest.php index ce29aab8b..c7bb950bb 100644 --- a/tests/CarbonImmutable/DayOfWeekModifiersTest.php +++ b/tests/CarbonImmutable/DayOfWeekModifiersTest.php @@ -31,20 +31,6 @@ public function testSetWeekendDays() $this->assertFalse(Carbon::createFromDate(2018, 2, 16)->isWeekend()); } - public function testGetWeekEndsAt() - { - Carbon::setWeekEndsAt(Carbon::SATURDAY); - $this->assertSame(Carbon::SATURDAY, Carbon::getWeekEndsAt()); - Carbon::setWeekEndsAt(Carbon::SUNDAY); - } - - public function testGetWeekStartsAt() - { - Carbon::setWeekStartsAt(Carbon::TUESDAY); - $this->assertSame(Carbon::TUESDAY, Carbon::getWeekStartsAt()); - Carbon::setWeekStartsAt(Carbon::MONDAY); - } - public function testStartOfWeek() { $d = Carbon::create(1980, 8, 7, 12, 11, 9)->startOfWeek(); diff --git a/tests/CarbonImmutable/RoundTest.php b/tests/CarbonImmutable/RoundTest.php index 33bf9b49e..296d25462 100644 --- a/tests/CarbonImmutable/RoundTest.php +++ b/tests/CarbonImmutable/RoundTest.php @@ -111,16 +111,6 @@ public function testRoundWeek() $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 19, 0, 0, 0, 0); $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0); $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0); - - Carbon::setWeekStartsAt(Carbon::SUNDAY); - Carbon::setWeekEndsAt(Carbon::SATURDAY); - - $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 18, 0, 0, 0, 0); - $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 25, 0, 0, 0, 0); - $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 18, 0, 0, 0, 0); - - Carbon::setWeekStartsAt(Carbon::MONDAY); - Carbon::setWeekEndsAt(Carbon::SUNDAY); } public function testRoundInvalidArgument() diff --git a/tests/Laravel/App.php b/tests/Laravel/App.php index a59af5b6b..55a29c897 100644 --- a/tests/Laravel/App.php +++ b/tests/Laravel/App.php @@ -11,7 +11,7 @@ class App implements ArrayAccess /** * @var string */ - protected $locale; + protected $locale = 'en'; /** * @var string @@ -54,7 +54,7 @@ public static function getLocaleChangeEventName() return version_compare((string) static::version(), '5.5') >= 0 ? 'Illuminate\Foundation\Events\LocaleUpdated' : 'locale.changed'; } - public function setLocale($locale) + public function setLocale(string $locale) { $this->locale = $locale; $this->translator->setLocale($locale);