Skip to content

Commit 4b73640

Browse files
committed
TimeRangeOfDay overlapping
1 parent 191128a commit 4b73640

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Time/TimeRangeOfDay.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public function includesOrEquals(self $other): bool
8585
return $this->start()->isBeforeOrEquals($other->start()) && $this->end()->isAfterOrEquals($other->end());
8686
}
8787

88+
/**
89+
* @param self $other
90+
* @return TimeRangeOfDay
91+
*/
92+
public function overlapping(self $other): self
93+
{
94+
$start = $this->start()->isAfter($other->start()) ? $this->start() : $other->start();
95+
$end = $this->end()->isBefore($other->end()) ? $this->end() : $other->end();
96+
97+
return new self($start, $end);
98+
}
99+
88100
/**
89101
* @return TimeRangeOfDay
90102
*/

tests/Time/TimeRangeOfDayTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,47 @@ public function testIncludesOrEquals()
9595
$this->assertFalse($timeRangeOfDay1020->includesOrEquals($timeRangeOfDay0030));
9696
$this->assertTrue($timeRangeOfDay0030->includesOrEquals($timeRangeOfDay0030));
9797
}
98+
99+
public function testOverlapping()
100+
{
101+
$time0 = new TimeOfDay(0);
102+
$time1 = new TimeOfDay(1);
103+
$time2 = new TimeOfDay(2);
104+
$time3 = new TimeOfDay(3);
105+
$this->assertSame(
106+
0,
107+
(new TimeRangeOfDay($time0, $time1))
108+
->overlapping(new TimeRangeOfDay($time1, $time2))
109+
->length()->hasSeconds());
110+
$this->assertSame(
111+
0,
112+
(new TimeRangeOfDay($time1, $time2))
113+
->overlapping(new TimeRangeOfDay($time0, $time1))
114+
->length()->hasSeconds());
115+
$this->assertSame(
116+
60 * 60 * 3,
117+
(new TimeRangeOfDay($time0, $time3))
118+
->overlapping(new TimeRangeOfDay($time0, $time3))
119+
->length()->hasSeconds());
120+
$this->assertSame(
121+
60 * 60,
122+
(new TimeRangeOfDay($time0, $time2))
123+
->overlapping(new TimeRangeOfDay($time1, $time2))
124+
->length()->hasSeconds());
125+
$this->assertSame(
126+
60 * 60,
127+
(new TimeRangeOfDay($time1, $time2))
128+
->overlapping(new TimeRangeOfDay($time0, $time2))
129+
->length()->hasSeconds());
130+
$this->assertSame(
131+
60 * 60,
132+
(new TimeRangeOfDay($time0, $time3))
133+
->overlapping(new TimeRangeOfDay($time1, $time2))
134+
->length()->hasSeconds());
135+
$this->assertSame(
136+
60 * 60,
137+
(new TimeRangeOfDay($time1, $time2))
138+
->overlapping(new TimeRangeOfDay($time0, $time3))
139+
->length()->hasSeconds());
140+
}
98141
}

0 commit comments

Comments
 (0)