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

Add generic methods isStartOfUnit and isEndOfUnit #3053

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for isStartOfDay and isEndOfDay
  • Loading branch information
kylekatarnls committed Jul 21, 2024
commit f14f4a5ac3aafce42a99aab80c56aa7ace70886e
28 changes: 28 additions & 0 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,25 @@ public function isStartOfDay(
Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false,
Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
): bool {
if ($checkMicroseconds === true) {
@trigger_error(
"Since 3.8.0, it's deprecated to use \$checkMicroseconds.\n".
"It will be removed in 4.0.0.\n".
"Instead, you should use either isStartOfDay(interval: Unit::Microsecond) or isStartOfDay(interval: Unit::Second)\n".
'And you can now use any custom interval as precision, such as isStartOfDay(interval: "15 minutes")',
\E_USER_DEPRECATED,
);
}

if ($interval === null && !\is_bool($checkMicroseconds)) {
$interval = $checkMicroseconds;
}

if ($interval !== null) {
if ($interval instanceof Unit) {
$interval = '1 '.$interval->value;
}

$date = $this->rawFormat('Y-m-d');
$time = $this->rawFormat('H:i:s.u');
$maximum = $this->avoidMutation()->startOfDay()->add($interval);
Expand Down Expand Up @@ -816,11 +830,25 @@ public function isEndOfDay(
Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false,
Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
): bool {
if ($checkMicroseconds === true) {
@trigger_error(
"Since 3.8.0, it's deprecated to use \$checkMicroseconds.\n".
"It will be removed in 4.0.0.\n".
"Instead, you should use either isStartOfDay(interval: Unit::Microsecond) or isStartOfDay(interval: Unit::Second)\n".
'And you can now use any custom interval as precision, such as isStartOfDay(interval: "15 minutes")',
\E_USER_DEPRECATED,
);
}

if ($interval === null && !\is_bool($checkMicroseconds)) {
$interval = $checkMicroseconds;
}

if ($interval !== null) {
if ($interval instanceof Unit) {
$interval = '1 '.$interval->value;
}

$date = $this->rawFormat('Y-m-d');
$time = $this->rawFormat('H:i:s.u');
$minimum = $this->avoidMutation()->endOfDay()->sub($interval);
Expand Down
56 changes: 56 additions & 0 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

use BadMethodCallException;
use Carbon\Carbon;
use Carbon\CarbonInterval;
use Carbon\Month;
use Carbon\Unit;
use Carbon\WeekDay;
use DateInterval;
use DateTime;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
Expand Down Expand Up @@ -837,6 +840,34 @@ public function testIsStartOfDayWithMicroseconds()
$this->assertFalse(Carbon::parse('00:00:00.000001')->isStartOfDay(true));
}

public function testIsStartOfDayInterval()
{
$this->assertTrue(Carbon::parse('00:00:00')->isStartOfDay('15 minutes'));
$this->assertTrue(Carbon::parse('00:15:00')->isStartOfDay('15 minutes'));
$this->assertFalse(Carbon::parse('00:15:00.000001')->isStartOfDay('15 minutes'));
$this->assertTrue(Carbon::parse('01:00:00')->isStartOfDay(Unit::Hour));
$this->assertFalse(Carbon::parse('01:00:00.000001')->isStartOfDay(Unit::Hour));
$this->assertTrue(Carbon::parse('00:01:00')->isStartOfDay(new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('00:01:00.000001')->isStartOfDay(new DateInterval('PT1M')));

$this->assertTrue(Carbon::parse('00:00:00')->isStartOfDay(interval: '15 minutes'));
$this->assertTrue(Carbon::parse('00:15:00')->isStartOfDay(interval: '15 minutes'));
$this->assertFalse(Carbon::parse('00:15:00.000001')->isStartOfDay(interval: '15 minutes'));
$this->assertTrue(Carbon::parse('01:00:00')->isStartOfDay(interval: Unit::Hour));
$this->assertFalse(Carbon::parse('01:00:00.000001')->isStartOfDay(interval: Unit::Hour));
$this->assertTrue(Carbon::parse('00:01:00')->isStartOfDay(interval: new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('00:01:00.000001')->isStartOfDay(interval: new DateInterval('PT1M')));

$this->assertTrue(Carbon::parse('00:02:00')->isStartOfDay(interval: CarbonInterval::minutes(2)));
$this->assertFalse(Carbon::parse('00:02:00.000001')->isStartOfDay(interval: CarbonInterval::minutes(2)));

// Always false with negative interval
$this->assertFalse(Carbon::parse('00:00:00')->isStartOfDay(interval: CarbonInterval::minutes(-2)));

// Always true with interval bigger than 1 day
$this->assertTrue(Carbon::parse('23:59:59.999999')->isStartOfDay(interval: CarbonInterval::hours(36)));
}

public function testIsEndOfDay()
{
$this->assertTrue(Carbon::parse('23:59:59')->isEndOfDay(false));
Expand All @@ -863,6 +894,31 @@ public function testIsEndOfDayWithMicroseconds()
$this->assertFalse(Carbon::parse('23:59:59.999998')->isEndOfDay(true));
}

public function testIsEndOfDayInterval()
{
$this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfDay('15 minutes'));
$this->assertTrue(Carbon::parse('23:45:00')->isEndOfDay('15 minutes'));
$this->assertFalse(Carbon::parse('23:44:59.999999')->isEndOfDay('15 minutes'));
$this->assertTrue(Carbon::parse('23:00:00')->isEndOfDay(Unit::Hour));
$this->assertFalse(Carbon::parse('22:59:59.999999')->isEndOfDay(Unit::Hour));
$this->assertTrue(Carbon::parse('23:59:00')->isEndOfDay(new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('23:58:59.999999')->isEndOfDay(new DateInterval('PT1M')));

$this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfDay(interval: '15 minutes'));
$this->assertTrue(Carbon::parse('23:45:00')->isEndOfDay(interval: '15 minutes'));
$this->assertFalse(Carbon::parse('23:44:59.999999')->isEndOfDay(interval: '15 minutes'));
$this->assertTrue(Carbon::parse('23:00:00')->isEndOfDay(interval: Unit::Hour));
$this->assertFalse(Carbon::parse('22:59:59.999999')->isEndOfDay(interval: Unit::Hour));
$this->assertTrue(Carbon::parse('23:59:00')->isEndOfDay(interval: new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('23:58:59.999999')->isEndOfDay(interval: new DateInterval('PT1M')));

// Always false with negative interval
$this->assertFalse(Carbon::parse('00:00:00')->isEndOfDay(interval: CarbonInterval::minutes(-2)));

// Always true with interval bigger than 1 day
$this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfDay(interval: CarbonInterval::hours(36)));
}

public function testIsMidnight()
{
$this->assertTrue(Carbon::parse('00:00:00')->isMidnight());
Expand Down
Loading