From ee219e640492cea871a5bb23e4b1d8f4a221d237 Mon Sep 17 00:00:00 2001 From: Marc Ypes Date: Fri, 12 Feb 2016 11:49:16 +0100 Subject: [PATCH 1/2] Business hours respecting seconds --- src/Business.php | 8 ++++---- src/Time.php | 21 +++++++++++++++++---- tests/BusinessTest.php | 21 +++++++++++++++++++++ tests/TimeIntervalTest.php | 4 ++-- tests/TimeTest.php | 6 +++--- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/Business.php b/src/Business.php index 3630667..ead89cf 100644 --- a/src/Business.php +++ b/src/Business.php @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/Time.php b/src/Time.php index 1da2faa..6ad47ea 100644 --- a/src/Time.php +++ b/src/Time.php @@ -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 $seconds Optional seconds with leading zero */ - public function __construct($hours, $minutes) + public function __construct($hours, $minutes, $seconds = '00') { $this->hours = $hours; $this->minutes = $minutes; + $this->seconds = $seconds; } /** @@ -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')); } /** @@ -109,6 +112,16 @@ public function getMinutes() return (int) $this->minutes; } + /** + * Gets the seconds. + * + * @return int + */ + public function getSeconds() + { + return (int) $this->seconds; + } + /** * Returns an integer representation of the time. * @@ -116,7 +129,7 @@ public function getMinutes() */ public function toInteger() { - return (int) $this->hours.$this->minutes; + return (int) $this->hours.$this->minutes.$this->seconds; } /** @@ -126,6 +139,6 @@ public function toInteger() */ public function toString() { - return sprintf('%s:%s', $this->hours, $this->minutes); + return sprintf('%s:%s:%s', $this->hours, $this->minutes, $this->seconds); } } diff --git a/tests/BusinessTest.php b/tests/BusinessTest.php index b4fa772..4cdddd5 100644 --- a/tests/BusinessTest.php +++ b/tests/BusinessTest.php @@ -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() @@ -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([ diff --git a/tests/TimeIntervalTest.php b/tests/TimeIntervalTest.php index d75bfc5..801cf8b 100644 --- a/tests/TimeIntervalTest.php +++ b/tests/TimeIntervalTest.php @@ -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() { @@ -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() { diff --git a/tests/TimeTest.php b/tests/TimeTest.php index 7045f75..f936d91 100644 --- a/tests/TimeTest.php +++ b/tests/TimeTest.php @@ -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() @@ -66,6 +66,6 @@ public function testIsBeforeOrEqual() public function testToString() { $time = new Time('20', '30'); - $this->assertEquals('20:30', $time->toString()); + $this->assertEquals('20:30:00', $time->toString()); } } From 321c36808eceb1db28bb8deadc9cb80e8c6befc7 Mon Sep 17 00:00:00 2001 From: Marc Ypes Date: Fri, 12 Feb 2016 13:02:14 +0100 Subject: [PATCH 2/2] Dont force leading zeros and allow for integer|string type arguments in Time class --- src/Time.php | 24 ++++++++++++------------ tests/TimeTest.php | 4 ++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Time.php b/src/Time.php index 6ad47ea..fb1c552 100644 --- a/src/Time.php +++ b/src/Time.php @@ -25,15 +25,15 @@ final class Time /** * Creates a new time. * - * @param string $hours - * @param string $minutes - * @param string $seconds Optional seconds with leading zero + * @param string|int $hours + * @param string|int $minutes + * @param string|int $seconds Optional seconds */ - public function __construct($hours, $minutes, $seconds = '00') + public function __construct($hours, $minutes, $seconds = 0) { - $this->hours = $hours; - $this->minutes = $minutes; - $this->seconds = $seconds; + $this->hours = (int) $hours; + $this->minutes = (int) $minutes; + $this->seconds = (int) $seconds; } /** @@ -99,7 +99,7 @@ public function isAfterOrEqual(Time $other) */ public function getHours() { - return (int) $this->hours; + return $this->hours; } /** @@ -109,7 +109,7 @@ public function getHours() */ public function getMinutes() { - return (int) $this->minutes; + return $this->minutes; } /** @@ -119,7 +119,7 @@ public function getMinutes() */ public function getSeconds() { - return (int) $this->seconds; + return $this->seconds; } /** @@ -129,7 +129,7 @@ public function getSeconds() */ public function toInteger() { - return (int) $this->hours.$this->minutes.$this->seconds; + return (int) sprintf('%d%02d%02d', $this->hours, $this->minutes, $this->seconds); } /** @@ -139,6 +139,6 @@ public function toInteger() */ public function toString() { - return sprintf('%s:%s:%s', $this->hours, $this->minutes, $this->seconds); + return sprintf('%02d:%02d:%02d', $this->hours, $this->minutes, $this->seconds); } } diff --git a/tests/TimeTest.php b/tests/TimeTest.php index f936d91..e702854 100644 --- a/tests/TimeTest.php +++ b/tests/TimeTest.php @@ -67,5 +67,9 @@ public function testToString() { $time = new Time('20', '30'); $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()); } }