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 closest() and farthest() #51

Merged
merged 1 commit into from
Nov 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/ChronosInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,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);
}
}