Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1967 Add auto mode for start/end of week #2090

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,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_DAY_AUTO = 'auto';

/**
* RFC7231 DateTime format.
*
Expand Down Expand Up @@ -3789,7 +3794,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
*/
Expand All @@ -3803,7 +3809,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
*/
Expand Down
25 changes: 21 additions & 4 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1605,13 +1605,25 @@ 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
*
* @return int
*/
public static function getWeekStartsAt()
{
if (static::$weekStartsAt === static::WEEK_DAY_AUTO) {
return static::getFirstDayOfWeek();
}

return static::$weekStartsAt;
}

Expand All @@ -1623,13 +1635,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_DAY_AUTO ? $day : max(0, (7 + $day) % 7);
}

/**
Expand All @@ -1639,6 +1651,10 @@ public static function setWeekStartsAt($day)
*/
public static function getWeekEndsAt()
{
if (static::$weekStartsAt === static::WEEK_DAY_AUTO) {
return (int) (static::DAYS_PER_WEEK - 1 + static::getFirstDayOfWeek()) % static::DAYS_PER_WEEK;
}

return static::$weekEndsAt;
}

Expand All @@ -1650,13 +1666,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_DAY_AUTO ? $day : max(0, (7 + $day) % 7);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
35 changes: 35 additions & 0 deletions tests/Carbon/DayOfWeekModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
35 changes: 35 additions & 0 deletions tests/CarbonImmutable/DayOfWeekModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}