Skip to content

Commit

Permalink
Fix off by one errors in diffFiltered
Browse files Browse the repository at this point in the history
Remove the workaround as it is causing #445.
  • Loading branch information
markstory committed Oct 29, 2023
1 parent 0bb107d commit f85f691
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 0 additions & 6 deletions src/Traits/DifferenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ public function diffFiltered(
$end = $this;
$inverse = true;
}
// Hack around DatePeriod not including end values.
// When handling dates we need to convert to a DateTime
// and offset by 1 second.
if ($end instanceof ChronosDate) {
$end = (new Chronos($end))->modify('+1 second');
}

$period = new DatePeriod($start, $ci, $end);
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {
Expand Down
19 changes: 16 additions & 3 deletions tests/TestCase/Date/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function testDiffFilteredNegativeWithSignWithSecondObject($class)
*/
public function testBug188DiffWithSameDates($class)
{
$start = $class::create(2014, 10, 8, 15, 20, 0);
$start = $class::create(2014, 10, 8);
$end = clone $start;

$this->assertSame(0, $start->diffInDays($end));
Expand All @@ -284,20 +284,33 @@ public function testBug188DiffWithSameDates($class)
*/
public function testBug188DiffWithSameDates1DayApart($class)
{
$start = $class::create(2014, 10, 8, 15, 20, 0);
$start = $class::create(2014, 10, 8);
$end = (clone $start)->addDays(1);

$this->assertSame(1, $start->diffInDays($end));
$this->assertSame(1, $start->diffInWeekdays($end));
}

/**
* @dataProvider dateClassProvider
* @return void
*/
public function testDiffInDaysTwoWeeks($class)
{
$start = $class::create(2014, 10, 8);
for ($i = 1; $i <= 14; $i++) {
$end = (clone $start)->addDays($i);
$this->assertSame($i, $start->diffInDays($end));
}
}

/**
* @dataProvider dateClassProvider
* @return void
*/
public function testBug188DiffWithDatesOnTheWeekend($class)
{
$start = $class::create(2014, 1, 1, 0, 0, 0);
$start = $class::create(2014, 1, 1);
$start = $start->next($class::SATURDAY);
$end = (clone $start)->addDays(1);

Expand Down

0 comments on commit f85f691

Please sign in to comment.