Skip to content

Commit

Permalink
Add unit tests for start/end of unit method
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jul 21, 2024
1 parent f14f4a5 commit 6b5325b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
20 changes: 11 additions & 9 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public function isStartOfDay(
$maximumDate = $maximum->rawFormat('Y-m-d');

if ($date === $maximumDate) {
return $time <= $maximum->rawFormat('H:i:s.u');
return $time < $maximum->rawFormat('H:i:s.u');
}

return $maximumDate > $date;
Expand Down Expand Up @@ -797,13 +797,15 @@ public function isStartOfUnit(

$startOfUnit = $this->avoidMutation()->startOf($unit);
$startOfUnitDateTime = $startOfUnit->rawFormat('Y-m-d H:i:s.u');
$maximumDateTime = $startOfUnit->add($interval)->rawFormat('Y-m-d H:i:s.u');
$maximumDateTime = $startOfUnit
->add($interval instanceof Unit ? '1 '.$interval->value : $interval)
->rawFormat('Y-m-d H:i:s.u');

if ($maximumDateTime < $startOfUnitDateTime) {
return false;
}

return $this->rawFormat('Y-m-d H:i:s.u') <= $maximumDateTime;
return $this->rawFormat('Y-m-d H:i:s.u') < $maximumDateTime;
}

/**
Expand Down Expand Up @@ -845,13 +847,11 @@ public function isEndOfDay(
}

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);
$minimum = $this->avoidMutation()
->endOfDay()
->sub($interval instanceof Unit ? '1 '.$interval->value : $interval);
$minimumDate = $minimum->rawFormat('Y-m-d');

if ($date === $minimumDate) {
Expand Down Expand Up @@ -887,7 +887,9 @@ public function isEndOfUnit(

$endOfUnit = $this->avoidMutation()->endOf($unit);
$endOfUnitDateTime = $endOfUnit->rawFormat('Y-m-d H:i:s.u');
$minimumDateTime = $endOfUnit->sub($interval)->rawFormat('Y-m-d H:i:s.u');
$minimumDateTime = $endOfUnit
->sub($interval instanceof Unit ? '1 '.$interval->value : $interval)
->rawFormat('Y-m-d H:i:s.u');

if ($minimumDateTime > $endOfUnitDateTime) {
return false;
Expand Down
66 changes: 52 additions & 14 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,23 +843,23 @@ public function testIsStartOfDayWithMicroseconds()
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:14:59.999999')->isStartOfDay('15 minutes'));
$this->assertFalse(Carbon::parse('00:15:00')->isStartOfDay('15 minutes'));
$this->assertTrue(Carbon::parse('00:59:59.999999')->isStartOfDay(Unit::Hour));
$this->assertFalse(Carbon::parse('01:00:00')->isStartOfDay(Unit::Hour));
$this->assertTrue(Carbon::parse('00:00:59.999999')->isStartOfDay(new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('00:01:00')->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:14:59.999999')->isStartOfDay(interval: '15 minutes'));
$this->assertFalse(Carbon::parse('00:15:00')->isStartOfDay(interval: '15 minutes'));
$this->assertTrue(Carbon::parse('00:59:59.999999')->isStartOfDay(interval: Unit::Hour));
$this->assertFalse(Carbon::parse('01:00:00')->isStartOfDay(interval: Unit::Hour));
$this->assertTrue(Carbon::parse('00:00:59.999999')->isStartOfDay(interval: new DateInterval('PT1M')));
$this->assertFalse(Carbon::parse('00:01:00')->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)));
$this->assertTrue(Carbon::parse('00:01:59.999999')->isStartOfDay(interval: CarbonInterval::minutes(2)));
$this->assertFalse(Carbon::parse('00:02:00')->isStartOfDay(interval: CarbonInterval::minutes(2)));

// Always false with negative interval
$this->assertFalse(Carbon::parse('00:00:00')->isStartOfDay(interval: CarbonInterval::minutes(-2)));
Expand All @@ -868,6 +868,25 @@ public function testIsStartOfDayInterval()
$this->assertTrue(Carbon::parse('23:59:59.999999')->isStartOfDay(interval: CarbonInterval::hours(36)));
}

public function testIsStartOfUnit()
{
$this->assertTrue(Carbon::parse('00:00:00')->isStartOfUnit(Unit::Hour));

$this->assertFalse(Carbon::parse('00:00:00.000001')->isStartOfUnit(Unit::Hour));
$this->assertFalse(Carbon::parse('00:00:01')->isStartOfUnit(Unit::Hour));

$this->assertTrue(Carbon::parse('00:00:00')->isStartOfUnit(Unit::Hour, '5 minutes'));
$this->assertTrue(Carbon::parse('00:04:59.999999')->isStartOfUnit(Unit::Hour, '5 minutes'));

$this->assertFalse(Carbon::parse('00:05:00')->isStartOfUnit(Unit::Hour, '5 minutes'));

$this->assertTrue(Carbon::parse('Monday')->isStartOfUnit(Unit::Week));
$this->assertTrue(Carbon::parse('Monday 23:59:59.999999')->isStartOfUnit(Unit::Week));

$this->assertFalse(Carbon::parse('Tuesday')->isStartOfUnit(Unit::Week));
$this->assertFalse(Carbon::parse('Monday')->isStartOfUnit(Unit::Week, CarbonInterval::day(-1)));
}

public function testIsEndOfDay()
{
$this->assertTrue(Carbon::parse('23:59:59')->isEndOfDay(false));
Expand Down Expand Up @@ -919,6 +938,25 @@ public function testIsEndOfDayInterval()
$this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfDay(interval: CarbonInterval::hours(36)));
}

public function testIsEndOfUnit()
{
$this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfUnit(Unit::Hour));

$this->assertFalse(Carbon::parse('23:59:59.999998')->isEndOfUnit(Unit::Hour));
$this->assertFalse(Carbon::parse('23:59:59')->isEndOfUnit(Unit::Hour));

$this->assertTrue(Carbon::parse('23:55:00.000001')->isEndOfUnit(Unit::Hour, '5 minutes'));
$this->assertTrue(Carbon::parse('23:55:00')->isEndOfUnit(Unit::Hour, '5 minutes'));

$this->assertFalse(Carbon::parse('23:54:59.999999')->isEndOfUnit(Unit::Hour, '5 minutes'));

$this->assertTrue(Carbon::parse('Sunday 23:59:59')->isEndOfUnit(Unit::Week, '2 days'));
$this->assertTrue(Carbon::parse('Saturday 00:00')->isEndOfUnit(Unit::Week, '2 days'));

$this->assertFalse(Carbon::parse('Friday 23:59:59.999999')->isEndOfUnit(Unit::Week, '2 days'));
$this->assertFalse(Carbon::parse('Monday')->isEndOfUnit(Unit::Week, CarbonInterval::day(-1)));
}

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

0 comments on commit 6b5325b

Please sign in to comment.