Skip to content

Commit f85f691

Browse files
committed
Fix off by one errors in diffFiltered
Remove the workaround as it is causing #445.
1 parent 0bb107d commit f85f691

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/Traits/DifferenceTrait.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,6 @@ public function diffFiltered(
182182
$end = $this;
183183
$inverse = true;
184184
}
185-
// Hack around DatePeriod not including end values.
186-
// When handling dates we need to convert to a DateTime
187-
// and offset by 1 second.
188-
if ($end instanceof ChronosDate) {
189-
$end = (new Chronos($end))->modify('+1 second');
190-
}
191185

192186
$period = new DatePeriod($start, $ci, $end);
193187
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {

tests/TestCase/Date/DiffTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function testDiffFilteredNegativeWithSignWithSecondObject($class)
271271
*/
272272
public function testBug188DiffWithSameDates($class)
273273
{
274-
$start = $class::create(2014, 10, 8, 15, 20, 0);
274+
$start = $class::create(2014, 10, 8);
275275
$end = clone $start;
276276

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

290290
$this->assertSame(1, $start->diffInDays($end));
291291
$this->assertSame(1, $start->diffInWeekdays($end));
292292
}
293293

294+
/**
295+
* @dataProvider dateClassProvider
296+
* @return void
297+
*/
298+
public function testDiffInDaysTwoWeeks($class)
299+
{
300+
$start = $class::create(2014, 10, 8);
301+
for ($i = 1; $i <= 14; $i++) {
302+
$end = (clone $start)->addDays($i);
303+
$this->assertSame($i, $start->diffInDays($end));
304+
}
305+
}
306+
294307
/**
295308
* @dataProvider dateClassProvider
296309
* @return void
297310
*/
298311
public function testBug188DiffWithDatesOnTheWeekend($class)
299312
{
300-
$start = $class::create(2014, 1, 1, 0, 0, 0);
313+
$start = $class::create(2014, 1, 1);
301314
$start = $start->next($class::SATURDAY);
302315
$end = (clone $start)->addDays(1);
303316

0 commit comments

Comments
 (0)