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

Implement isStartOf* and isEndOf* for all units #3056

Merged
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
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parameters:
- '#should return (\S*)static\(Carbon\\CarbonInterval\)(\|null)? but returns Carbon\\CarbonInterval(\|null)?\.$#'
- '#^Call to an undefined method DateInterval::(spec|optimize)\(\)\.$#'
- '#^Property Carbon\\Carbon::\$timezone \(Carbon\\CarbonTimeZone\) does not accept string\.$#'
- '#^Method class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+::modify\(\) should return class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+ but returns \(DateTimeImmutable\|false\)\.$#'
- '#^Method class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+::modify\(\) should return class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+ but returns \(?DateTimeImmutable#'
-
message: '#^Undefined variable: \$this$#'
paths:
Expand Down
150 changes: 148 additions & 2 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,18 @@ public function endOfHour(): static;
*/
public function endOfMillennium();

/**
* Modify to end of current millisecond, microseconds such as 12345 become 123999
*
* @example
* ```
* echo Carbon::parse('2018-07-25 12:45:16.334455')
* ->endOfSecond()
* ->format('H:i:s.u');
* ```
*/
public function endOfMillisecond(): static;

/**
* Modify to end of current minute, seconds become 59
*
Expand Down Expand Up @@ -2662,6 +2674,11 @@ public function isCurrentUnit(string $unit): bool;
*/
public function isDayOfWeek($dayOfWeek): bool;

/**
* Determines if the instance is end of century (last day by default but interval can be customized).
*/
public function isEndOfCentury(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Check if the instance is end of day.
*
Expand All @@ -2684,6 +2701,46 @@ public function isDayOfWeek($dayOfWeek): bool;
*/
public function isEndOfDay(Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of decade (last day by default but interval can be customized).
*/
public function isEndOfDecade(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of hour (last microsecond by default but interval can be customized).
*/
public function isEndOfHour(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of millennium (last day by default but interval can be customized).
*/
public function isEndOfMillennium(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of millisecond (last microsecond by default but interval can be customized).
*/
public function isEndOfMillisecond(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of minute (last microsecond by default but interval can be customized).
*/
public function isEndOfMinute(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of month (last day by default but interval can be customized).
*/
public function isEndOfMonth(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of quarter (last day by default but interval can be customized).
*/
public function isEndOfQuarter(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is end of second (last microsecond by default but interval can be customized).
*/
public function isEndOfSecond(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Returns true if the date was created using CarbonImmutable::endOfTime()
*
Expand All @@ -2700,7 +2757,23 @@ public function isEndOfTime(): bool;
* Carbon::parse('2019-02-28 20:13:00')->isEndOfUnit(Unit::Hour, '15 minutes'); // false
* ```
*/
public function isEndOfUnit(Unit $unit, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;
public function isEndOfUnit(Unit $unit, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null, mixed ...$params): bool;

/**
* Determines if the instance is end of week (last day by default but interval can be customized).
*
* @example
* ```
* Carbon::parse('2024-08-31')->endOfWeek()->isEndOfWeek(); // true
* Carbon::parse('2024-08-31')->isEndOfWeek(); // false
* ```
*/
public function isEndOfWeek(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null, WeekDay|int|null $weekEndsAt = null): bool;

/**
* Determines if the instance is end of year (last day by default but interval can be customized).
*/
public function isEndOfYear(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is in the future, ie. greater (after) than now.
Expand Down Expand Up @@ -2895,6 +2968,11 @@ public function isSameQuarter(DateTimeInterface|string $date, bool $ofSameYear =
*/
public function isSameUnit(string $unit, DateTimeInterface|string $date): bool;

/**
* Determines if the instance is start of century (first day by default but interval can be customized).
*/
public function isStartOfCentury(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Check if the instance is start of day / midnight.
*
Expand All @@ -2915,6 +2993,46 @@ public function isSameUnit(string $unit, DateTimeInterface|string $date): bool;
*/
public function isStartOfDay(Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of decade (first day by default but interval can be customized).
*/
public function isStartOfDecade(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of hour (first microsecond by default but interval can be customized).
*/
public function isStartOfHour(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of millennium (first day by default but interval can be customized).
*/
public function isStartOfMillennium(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of millisecond (first microsecond by default but interval can be customized).
*/
public function isStartOfMillisecond(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of minute (first microsecond by default but interval can be customized).
*/
public function isStartOfMinute(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of month (first day by default but interval can be customized).
*/
public function isStartOfMonth(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of quarter (first day by default but interval can be customized).
*/
public function isStartOfQuarter(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Determines if the instance is start of second (first microsecond by default but interval can be customized).
*/
public function isStartOfSecond(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Returns true if the date was created using CarbonImmutable::startOfTime()
*
Expand All @@ -2931,7 +3049,23 @@ public function isStartOfTime(): bool;
* Carbon::parse('2019-02-28 20:13:00')->isStartOfUnit(Unit::Hour, '15 minutes'); // true
* ```
*/
public function isStartOfUnit(Unit $unit, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;
public function isStartOfUnit(Unit $unit, Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null, mixed ...$params): bool;

/**
* Determines if the instance is start of week (first day by default but interval can be customized).
*
* @example
* ```
* Carbon::parse('2024-08-31')->startOfWeek()->isStartOfWeek(); // true
* Carbon::parse('2024-08-31')->isStartOfWeek(); // false
* ```
*/
public function isStartOfWeek(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null, WeekDay|int|null $weekStartsAt = null): bool;

/**
* Determines if the instance is start of year (first day by default but interval can be customized).
*/
public function isStartOfYear(Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null): bool;

/**
* Returns true if the strict mode is globally in use, false else.
Expand Down Expand Up @@ -3979,6 +4113,18 @@ public function startOfHour(): static;
*/
public function startOfMillennium();

/**
* Modify to start of current millisecond, microseconds such as 12345 become 123000
*
* @example
* ```
* echo Carbon::parse('2018-07-25 12:45:16.334455')
* ->startOfSecond()
* ->format('H:i:s.u');
* ```
*/
public function startOfMillisecond(): static;

/**
* Modify to start of current minute, seconds become 0
*
Expand Down
36 changes: 35 additions & 1 deletion src/Carbon/Traits/Boundaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function startOfWeek(WeekDay|int|null $weekStartsAt = null): static
* echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek(Carbon::SATURDAY) . "\n";
* ```
*
* @param WeekDay|int|null $weekEndsAt optional start allow you to specify the day of week to use to end the week
* @param WeekDay|int|null $weekEndsAt optional end allow you to specify the day of week to use to end the week
*
* @return static
*/
Expand Down Expand Up @@ -391,6 +391,40 @@ public function endOfSecond(): static
return $this->setTime($this->hour, $this->minute, $this->second, static::MICROSECONDS_PER_SECOND - 1);
}

/**
* Modify to start of current millisecond, microseconds such as 12345 become 123000
*
* @example
* ```
* echo Carbon::parse('2018-07-25 12:45:16.334455')
* ->startOfSecond()
* ->format('H:i:s.u');
* ```
*/
public function startOfMillisecond(): static
{
$millisecond = (int) floor($this->micro / 1000);

return $this->setTime($this->hour, $this->minute, $this->second, $millisecond * 1000);
}

/**
* Modify to end of current millisecond, microseconds such as 12345 become 123999
*
* @example
* ```
* echo Carbon::parse('2018-07-25 12:45:16.334455')
* ->endOfSecond()
* ->format('H:i:s.u');
* ```
*/
public function endOfMillisecond(): static
{
$millisecond = (int) floor($this->micro / 1000);

return $this->setTime($this->hour, $this->minute, $this->second, $millisecond * 1000 + 999);
}

/**
* Modify to start of current given unit.
*
Expand Down
Loading
Loading