From b8ac42f1395260acb9891bb33d8f35aed770f720 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 19 May 2020 22:46:14 +0200 Subject: [PATCH 1/4] Fix #1967 Add auto mode for start/end of week --- src/Carbon/CarbonInterface.php | 5 +++ src/Carbon/Traits/Date.php | 23 +++++++++--- tests/Carbon/DayOfWeekModifiersTest.php | 35 +++++++++++++++++++ .../DayOfWeekModifiersTest.php | 35 +++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 650654db8c..2e89891ac3 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -578,6 +578,11 @@ interface CarbonInterface extends DateTimeInterface, JsonSerializable public const MICROSECONDS_PER_MILLISECOND = 1000; public const MICROSECONDS_PER_SECOND = 1000000; + /** + * Special settings to get the start of week from current locale culture. + */ + public const WEEK_START_DAY = 'auto'; + /** * RFC7231 DateTime format. * diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index d0e8b7bab0..4e6a98805d 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -1612,6 +1612,13 @@ public static function getDays() */ public static function getWeekStartsAt() { + if (static::$weekStartsAt === static::WEEK_START_DAY) { + return (int) static::getTranslationMessageWith( + static::getTranslator(), + 'first_day_of_week' + ); + } + return static::$weekStartsAt; } @@ -1623,13 +1630,13 @@ public static function getWeekStartsAt() * * Set the first day of week * - * @param int $day week start day + * @param int|string $day week start day (or 'auto' to get the first day of week from Carbon::getLocale() culture). * * @return void */ public static function setWeekStartsAt($day) { - static::$weekStartsAt = max(0, (7 + $day) % 7); + static::$weekStartsAt = $day === static::WEEK_START_DAY ? $day : max(0, (7 + $day) % 7); } /** @@ -1639,6 +1646,13 @@ public static function setWeekStartsAt($day) */ public static function getWeekEndsAt() { + if (static::$weekStartsAt === static::WEEK_START_DAY) { + return (int) (static::DAYS_PER_WEEK - 1 + static::getTranslationMessageWith( + static::getTranslator(), + 'first_day_of_week' + )) % static::DAYS_PER_WEEK; + } + return static::$weekEndsAt; } @@ -1650,13 +1664,14 @@ public static function getWeekEndsAt() * * Set the last day of week * - * @param int $day + * @param int|string $day week end day (or 'auto' to get the day before the first day of week + * from Carbon::getLocale() culture). * * @return void */ public static function setWeekEndsAt($day) { - static::$weekEndsAt = max(0, (7 + $day) % 7); + static::$weekEndsAt = $day === static::WEEK_START_DAY ? $day : max(0, (7 + $day) % 7); } /** diff --git a/tests/Carbon/DayOfWeekModifiersTest.php b/tests/Carbon/DayOfWeekModifiersTest.php index 16a3c40d45..20445b565f 100644 --- a/tests/Carbon/DayOfWeekModifiersTest.php +++ b/tests/Carbon/DayOfWeekModifiersTest.php @@ -416,4 +416,39 @@ public function testPreviousWeekendDay() $d = Carbon::create(2016, 7, 17)->previousWeekendDay(); $this->assertCarbon($d, 2016, 7, 16); } + + public function testWeekStartAndEndWithAutoMode() + { + $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName); + + Carbon::setWeekStartsAt('auto'); + + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en_UM'); + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en_US'); + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en'); + $this->assertSame('Sunday', 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); + + Carbon::setWeekEndsAt('auto'); + + Carbon::setLocale('en_UM'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en_US'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('es_US'); + $this->assertSame('sábado', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en_GB'); + $this->assertSame('Sunday', Carbon::now()->endOfWeek()->dayName); + + Carbon::setWeekStartsAt(Carbon::MONDAY); + Carbon::setWeekEndsAt(Carbon::SUNDAY); + } } diff --git a/tests/CarbonImmutable/DayOfWeekModifiersTest.php b/tests/CarbonImmutable/DayOfWeekModifiersTest.php index ce29aab8b4..77d9f17741 100644 --- a/tests/CarbonImmutable/DayOfWeekModifiersTest.php +++ b/tests/CarbonImmutable/DayOfWeekModifiersTest.php @@ -415,4 +415,39 @@ public function testPreviousWeekendDay() $d = Carbon::create(2016, 7, 17)->previousWeekendDay(); $this->assertCarbon($d, 2016, 7, 16); } + + public function testWeekStartAndEndWithAutoMode() + { + $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName); + + Carbon::setWeekStartsAt('auto'); + + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en_UM'); + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en_US'); + $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName); + Carbon::setLocale('en'); + $this->assertSame('Sunday', 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); + + Carbon::setWeekEndsAt('auto'); + + Carbon::setLocale('en_UM'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en_US'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en'); + $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('es_US'); + $this->assertSame('sábado', Carbon::now()->endOfWeek()->dayName); + Carbon::setLocale('en_GB'); + $this->assertSame('Sunday', Carbon::now()->endOfWeek()->dayName); + + Carbon::setWeekStartsAt(Carbon::MONDAY); + Carbon::setWeekEndsAt(Carbon::SUNDAY); + } } From 2e089ff77a4ddf17efd1263e6656fb6d8e3eb971 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 19 May 2020 22:50:51 +0200 Subject: [PATCH 2/4] Rename constant --- src/Carbon/CarbonInterface.php | 7 ++++--- src/Carbon/Traits/Date.php | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 2e89891ac3..bfdc52e32b 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -581,7 +581,7 @@ interface CarbonInterface extends DateTimeInterface, JsonSerializable /** * Special settings to get the start of week from current locale culture. */ - public const WEEK_START_DAY = 'auto'; + public const WEEK_DAY_AUTO = 'auto'; /** * RFC7231 DateTime format. @@ -3793,7 +3793,8 @@ public static function setUtf8($utf8); * * Set the last day of week * - * @param int $day + * @param int|string $day week end day (or 'auto' to get the day before the first day of week + * from Carbon::getLocale() culture). * * @return void */ @@ -3807,7 +3808,7 @@ public static function setWeekEndsAt($day); * * Set the first day of week * - * @param int $day week start day + * @param int|string $day week start day (or 'auto' to get the first day of week from Carbon::getLocale() culture). * * @return void */ diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index 4e6a98805d..e56cabc168 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -1612,7 +1612,7 @@ public static function getDays() */ public static function getWeekStartsAt() { - if (static::$weekStartsAt === static::WEEK_START_DAY) { + if (static::$weekStartsAt === static::WEEK_DAY_AUTO) { return (int) static::getTranslationMessageWith( static::getTranslator(), 'first_day_of_week' @@ -1636,7 +1636,7 @@ public static function getWeekStartsAt() */ public static function setWeekStartsAt($day) { - static::$weekStartsAt = $day === static::WEEK_START_DAY ? $day : max(0, (7 + $day) % 7); + static::$weekStartsAt = $day === static::WEEK_DAY_AUTO ? $day : max(0, (7 + $day) % 7); } /** @@ -1646,7 +1646,7 @@ public static function setWeekStartsAt($day) */ public static function getWeekEndsAt() { - if (static::$weekStartsAt === static::WEEK_START_DAY) { + if (static::$weekStartsAt === static::WEEK_DAY_AUTO) { return (int) (static::DAYS_PER_WEEK - 1 + static::getTranslationMessageWith( static::getTranslator(), 'first_day_of_week' @@ -1671,7 +1671,7 @@ public static function getWeekEndsAt() */ public static function setWeekEndsAt($day) { - static::$weekEndsAt = $day === static::WEEK_START_DAY ? $day : max(0, (7 + $day) % 7); + static::$weekEndsAt = $day === static::WEEK_DAY_AUTO ? $day : max(0, (7 + $day) % 7); } /** From e0e4c4d839110e7ff60c2e34cfd2b17b475ac533 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 19 May 2020 22:51:56 +0200 Subject: [PATCH 3/4] Update $weekStartsAt and $weekEndsAt type hint --- src/Carbon/Traits/Options.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Carbon/Traits/Options.php b/src/Carbon/Traits/Options.php index 1a3743402b..9ca824a551 100644 --- a/src/Carbon/Traits/Options.php +++ b/src/Carbon/Traits/Options.php @@ -36,14 +36,14 @@ trait Options /** * First day of week. * - * @var int + * @var int|string */ protected static $weekStartsAt = CarbonInterface::MONDAY; /** * Last day of week. * - * @var int + * @var int|string */ protected static $weekEndsAt = CarbonInterface::SUNDAY; From 14bbb61da79fd5e0251dc8d4e5960966f9f1d613 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 20 May 2020 00:53:37 +0200 Subject: [PATCH 4/4] Add private method getFirstDayOfWeek --- src/Carbon/Traits/Date.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index e56cabc168..0110391108 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -1605,6 +1605,14 @@ public static function getDays() /////////////////////// WEEK SPECIAL DAYS ///////////////////////// /////////////////////////////////////////////////////////////////// + private static function getFirstDayOfWeek(): int + { + return (int) static::getTranslationMessageWith( + static::getTranslator(), + 'first_day_of_week' + ); + } + /** * Get the first day of week * @@ -1613,10 +1621,7 @@ public static function getDays() public static function getWeekStartsAt() { if (static::$weekStartsAt === static::WEEK_DAY_AUTO) { - return (int) static::getTranslationMessageWith( - static::getTranslator(), - 'first_day_of_week' - ); + return static::getFirstDayOfWeek(); } return static::$weekStartsAt; @@ -1647,10 +1652,7 @@ public static function setWeekStartsAt($day) public static function getWeekEndsAt() { if (static::$weekStartsAt === static::WEEK_DAY_AUTO) { - return (int) (static::DAYS_PER_WEEK - 1 + static::getTranslationMessageWith( - static::getTranslator(), - 'first_day_of_week' - )) % static::DAYS_PER_WEEK; + return (int) (static::DAYS_PER_WEEK - 1 + static::getFirstDayOfWeek()) % static::DAYS_PER_WEEK; } return static::$weekEndsAt;