Skip to content

Commit

Permalink
Merge pull request #19 from ceeram/seconds
Browse files Browse the repository at this point in the history
Business hours respecting seconds
  • Loading branch information
florianv committed Feb 12, 2016
2 parents 62a0e5d + 321c368 commit c48ff90
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Business.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function getClosestDateBefore(\DateTime $date)

if (!$this->holidays->isHoliday($tmpDate) && null !== $day = $this->getDay($dayOfWeek)) {
if (null !== $closestTime = $day->getClosestOpeningTimeBefore($time, $tmpDate)) {
$tmpDate->setTime($closestTime->getHours(), $closestTime->getMinutes());
$tmpDate->setTime($closestTime->getHours(), $closestTime->getMinutes(), $closestTime->getSeconds());

return $tmpDate;
}
Expand All @@ -154,7 +154,7 @@ private function getClosestDateBefore(\DateTime $date)

$closestDay = $this->getClosestDayBefore((int) $tmpDate->format('N'));
$closingTime = $closestDay->getClosingTime($tmpDate);
$tmpDate->setTime($closingTime->getHours(), $closingTime->getMinutes());
$tmpDate->setTime($closingTime->getHours(), $closingTime->getMinutes(), $closingTime->getSeconds());

return $tmpDate;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ private function getClosestDateAfter(\DateTime $date)

if (!$this->holidays->isHoliday($tmpDate) && null !== $day = $this->getDay($dayOfWeek)) {
if (null !== $closestTime = $day->getClosestOpeningTimeAfter($time, $tmpDate)) {
$tmpDate->setTime($closestTime->getHours(), $closestTime->getMinutes());
$tmpDate->setTime($closestTime->getHours(), $closestTime->getMinutes(), $closestTime->getSeconds());

return $tmpDate;
}
Expand All @@ -210,7 +210,7 @@ private function getClosestDateAfter(\DateTime $date)

$closestDay = $this->getClosestDayBefore((int) $tmpDate->format('N'));
$closingTime = $closestDay->getOpeningTime($tmpDate);
$tmpDate->setTime($closingTime->getHours(), $closingTime->getMinutes());
$tmpDate->setTime($closingTime->getHours(), $closingTime->getMinutes(), $closingTime->getSeconds());

return $tmpDate;
}
Expand Down
33 changes: 23 additions & 10 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ final class Time
{
private $hours;
private $minutes;
private $seconds;

/**
* Creates a new time.
*
* @param string $hours
* @param string $minutes
* @param string|int $hours
* @param string|int $minutes
* @param string|int $seconds Optional seconds
*/
public function __construct($hours, $minutes)
public function __construct($hours, $minutes, $seconds = 0)
{
$this->hours = $hours;
$this->minutes = $minutes;
$this->hours = (int) $hours;
$this->minutes = (int) $minutes;
$this->seconds = (int) $seconds;
}

/**
Expand Down Expand Up @@ -62,7 +65,7 @@ public static function fromString($time)
*/
public static function fromDate(\DateTime $date)
{
return new self($date->format('H'), $date->format('i'));
return new self($date->format('H'), $date->format('i'), $date->format('s'));
}

/**
Expand Down Expand Up @@ -96,7 +99,7 @@ public function isAfterOrEqual(Time $other)
*/
public function getHours()
{
return (int) $this->hours;
return $this->hours;
}

/**
Expand All @@ -106,7 +109,17 @@ public function getHours()
*/
public function getMinutes()
{
return (int) $this->minutes;
return $this->minutes;
}

/**
* Gets the seconds.
*
* @return int
*/
public function getSeconds()
{
return $this->seconds;
}

/**
Expand All @@ -116,7 +129,7 @@ public function getMinutes()
*/
public function toInteger()
{
return (int) $this->hours.$this->minutes;
return (int) sprintf('%d%02d%02d', $this->hours, $this->minutes, $this->seconds);
}

/**
Expand All @@ -126,6 +139,6 @@ public function toInteger()
*/
public function toString()
{
return sprintf('%s:%s', $this->hours, $this->minutes);
return sprintf('%02d:%02d:%02d', $this->hours, $this->minutes, $this->seconds);
}
}
21 changes: 21 additions & 0 deletions tests/BusinessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testWithin()

$this->assertFalse($business->within(new \DateTime('2015-05-11 18:00'))); // Monday
$this->assertFalse($business->within(new \DateTime('2015-05-12 10:00'))); // Tuesday
$this->assertFalse($business->within(new \DateTime('2015-05-11 13:00:25'))); // Monday, seconds outside business hours
}

public function testWithinWithHoliday()
Expand Down Expand Up @@ -358,6 +359,26 @@ public function testTimelineWithDaysInterval()
$this->assertEquals('2015-06-05 10:00', $dates[3]->format('Y-m-d H:i'));
}

public function testTimelineWithSeconds()
{
$business = new Business([
new Day(Days::MONDAY, [['09:00', '17:00']]),
new Day(Days::TUESDAY, [['09:00', '17:00']]),
new Day(Days::WEDNESDAY, [['09:00', '17:00']]),
]);

$start = new \DateTime('2015-05-25 11:00:25'); // Monday, with seconds
$end = new \DateTime('2015-05-27 13:00:40');

$dates = $business->timeline($start, $end, new \DateInterval('P1D'));

$this->assertCount(3, $dates);

$this->assertEquals('2015-05-25 11:00:25', $dates[0]->format('Y-m-d H:i:s'));
$this->assertEquals('2015-05-26 11:00:25', $dates[1]->format('Y-m-d H:i:s'));
$this->assertEquals('2015-05-27 11:00:25', $dates[2]->format('Y-m-d H:i:s'));
}

public function testTimelineWithDaysIntervalAndHolidays()
{
$business = new Business([
Expand Down
4 changes: 2 additions & 2 deletions tests/TimeIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TimeIntervalTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The opening time "08:00" must be before the closing time "08:00".
* @expectedExceptionMessage The opening time "08:00:00" must be before the closing time "08:00:00".
*/
public function testConstructorOpeningEqualClosing()
{
Expand All @@ -27,7 +27,7 @@ public function testConstructorOpeningEqualClosing()

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The opening time "18:00" must be before the closing time "08:00".
* @expectedExceptionMessage The opening time "18:00:00" must be before the closing time "08:00:00".
*/
public function testConstructorOpeningAfterClosing()
{
Expand Down
10 changes: 7 additions & 3 deletions tests/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function testFromDate()
public function testToInteger()
{
$time = new Time('20', '00');
$this->assertEquals(2000, $time->toInteger());
$this->assertEquals(200000, $time->toInteger());

$time = new Time('09', '30');
$this->assertEquals(930, $time->toInteger());
$this->assertEquals(93000, $time->toInteger());
}

public function testIsAfterOrEqual()
Expand All @@ -66,6 +66,10 @@ public function testIsBeforeOrEqual()
public function testToString()
{
$time = new Time('20', '30');
$this->assertEquals('20:30', $time->toString());
$this->assertEquals('20:30:00', $time->toString());
$time = new Time('9', '8', '7');
$this->assertEquals('09:08:07', $time->toString());
$time = new Time(9, 8, 7);
$this->assertEquals('09:08:07', $time->toString());
}
}

0 comments on commit c48ff90

Please sign in to comment.