Skip to content

Commit

Permalink
Merge pull request #56 from hughgrigg/54-diff-business-start-after-end
Browse files Browse the repository at this point in the history
Improve negative interval handling
  • Loading branch information
hughgrigg authored Jul 25, 2019
2 parents 53afe1a + 87630de commit 694157e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/BusinessTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,18 @@ public function diffBusiness(
DateTimeInterface $time = null,
bool $absolute = true
): Interval {
$diffInBusinessTime = $this->diffInBusinessTime($time, $absolute);
// Allow for weird behaviour of DateInterval with negatives.
if ($diffInBusinessTime < 0 && !$absolute) {
/** @var Interval $interval */
$interval = Interval::seconds(0);
$interval->seconds = $diffInBusinessTime * $this->precision()->inSeconds();

return $interval;
}

return Interval::seconds(
$this->diffInBusinessTime($time, $absolute)
* $this->precision()->inSeconds()
abs($diffInBusinessTime) * $this->precision()->inSeconds()
);
}

Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/BusinessTime/DiffBusinessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace BusinessTime\Tests\Unit\BusinessTime;

use BusinessTime\BusinessTime;
use PHPUnit\Framework\TestCase;

/**
* Test the BusinessTime::diffBusiness() method.
*/
class DiffBusinessTest extends TestCase
{
/**
* Should be able to handle start date being after end date.
*/
public function testStartAfterEndAbsolute()
{
// Given we have a business time as a start;
$start = new BusinessTime('Monday 11am');

// And an end time that is before that;
$end = new BusinessTime('Monday 10am');

// When we get the absolute difference in business time;
$diff = $start->diffBusiness($end);

// Then it should still be correct.
self::assertEquals(60, $diff->inMinutes());
}

/**
* Should be able to handle start date being after end date.
*/
public function testStartAfterEndNonAbsolute()
{
// Given we have a business time as a start;
$start = new BusinessTime('Monday 11am');

// And an end time that is before that;
$end = new BusinessTime('Monday 10am');

// When we get the non-absolute difference in business time;
$diff = $start->diffBusiness($end, false);

// Then it should still be correct (note weirdness with how DateInterval
// handles negatives and units).
self::assertEquals(-3600, $diff->seconds);
self::assertEquals(60, $diff->inMinutes());
}
}
38 changes: 38 additions & 0 deletions tests/Unit/Interval/IntervalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace BusinessTime\Tests\Unit\Interval;

use BusinessTime\Interval;
use PHPUnit\Framework\TestCase;

class IntervalTest extends TestCase
{
public function testPositiveInterval()
{
// Given we have a positive number of seconds;
$seconds = 3600;

// When we construct and interval with it;
/** @var Interval $interval */
$interval = Interval::seconds($seconds);

// Then it should be correct.
self::assertEquals(60, $interval->inMinutes());
}

public function testNegativeInterval()
{
// Given we have a negative number of seconds;
$seconds = -3600;

// When we construct and interval with it;
/** @var Interval $interval */
$interval = Interval::seconds(0);
$interval->seconds = $seconds;
$interval->invert = 1;

// Then it should be correct.
self::assertEquals(-3600, $interval->seconds);
self::assertEquals(60, $interval->inMinutes());
}
}

0 comments on commit 694157e

Please sign in to comment.