Skip to content

Commit

Permalink
Merge pull request #51 from cakephp/master-closest-farthest
Browse files Browse the repository at this point in the history
Add closest() and farthest()
  • Loading branch information
markstory committed Nov 6, 2015
2 parents 2bd8cab + 998cf34 commit d2b6f8d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ChronosInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ public function lte(ChronosInterface $dt);
*/
public function between(ChronosInterface $dt1, ChronosInterface $dt2, $equal = true);

/**
* Get the closest date from the instance.
*
* @param ChronosInterface $dt1
* @param ChronosInterface $dt2
* @return static
*/
public function closest(ChronosInterface $dt1, ChronosInterface $dt2);

/**
* Get the farthest date from the instance.
*
* @param ChronosInterface $dt1
* @param ChronosInterface $dt2
* @return static
*/
public function farthest(ChronosInterface $dt1, ChronosInterface $dt2);

/**
* Get the minimum instance between a given instance (default now) and the current instance.
*
Expand Down
24 changes: 24 additions & 0 deletions src/ComparisonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ public function between(ChronosInterface $dt1, ChronosInterface $dt2, $equal = t

return $this->gt($dt1) && $this->lt($dt2);
}

/**
* Get the closest date from the instance.
*
* @param ChronosInterface $dt1
* @param ChronosInterface $dt2
* @return static
*/
public function closest(ChronosInterface $dt1, ChronosInterface $dt2)
{
return $this->diffInSeconds($dt1) < $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}

/**
* Get the farthest date from the instance.
*
* @param ChronosInterface $dt1
* @param ChronosInterface $dt2
* @return static
*/
public function farthest(ChronosInterface $dt1, ChronosInterface $dt2)
{
return $this->diffInSeconds($dt1) > $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}

/**
* Get the minimum instance between a given instance (default now) and the current instance.
Expand Down
52 changes: 52 additions & 0 deletions tests/DateTime/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,56 @@ public function testIsBirthday($class)
$this->assertFalse($dt2->isBirthday($dt1));
$this->assertTrue($dt3->isBirthday($dt1));
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testClosest($class)
{
$instance = $class::create(2015, 5, 28, 12, 0, 0);
$dt1 = $class::create(2015, 5, 28, 11, 0, 0);
$dt2 = $class::create(2015, 5, 28, 14, 0, 0);
$closest = $instance->closest($dt1, $dt2);
$this->assertEquals($dt1, $closest);
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testClosestWithEquals($class)
{
$instance = $class::create(2015, 5, 28, 12, 0, 0);
$dt1 = $class::create(2015, 5, 28, 12, 0, 0);
$dt2 = $class::create(2015, 5, 28, 14, 0, 0);
$closest = $instance->closest($dt1, $dt2);
$this->assertEquals($dt1, $closest);
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testFarthest($class)
{
$instance = $class::create(2015, 5, 28, 12, 0, 0);
$dt1 = $class::create(2015, 5, 28, 11, 0, 0);
$dt2 = $class::create(2015, 5, 28, 14, 0, 0);
$Farthest = $instance->farthest($dt1, $dt2);
$this->assertEquals($dt2, $Farthest);
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testFarthestWithEquals($class)
{
$instance = $class::create(2015, 5, 28, 12, 0, 0);
$dt1 = $class::create(2015, 5, 28, 12, 0, 0);
$dt2 = $class::create(2015, 5, 28, 14, 0, 0);
$Farthest = $instance->farthest($dt1, $dt2);
$this->assertEquals($dt2, $Farthest);
}
}

0 comments on commit d2b6f8d

Please sign in to comment.