Reputation: 15435
I have two Intervals say,
// for simplicity, I present here only the hours
val interval1 = (08, 20)
val interval2 = (00, 10)
Now, when I do a overlap between the two intervals, I get
val overlap = interval.overlap(interval2) // gives me (08, 10)
Is there a method in joda-time that I can use to get the non overlapping time periods? I need to get (10, 20)
I tried using the gap method:
scala> val a = new Interval(DateTime.parse("2000-10-12T00:00:00"), DateTime.parse("2000-10-12T20:00:00"))
scala> val b = new Interval(DateTime.parse("2000-09-12T00:00:00"), DateTime.parse("2000-10-12T12:00:00"))
a.gap(b) // returns null
b.gap(a) // returns null
Upvotes: 0
Views: 475
Reputation: 15435
Looks like there is nothing in the library that offers this. So I came up with my own implementation which is like this:
private def splitIntervals(interval1: Interval, interval2: Interval) = {
Option(interval1.overlap(interval2)) match {
case Some(overlap) => {
if (overlap.equals(interval1)) Seq(interval1)
else if (overlap.equals(interval2))
Seq(overlap) :+
new Interval(interval1.getStart, overlap.getStart) :+
new Interval(overlap.getEnd, interval1.getEnd)
else if (overlap.getStart.equals(interval1.getStart))
Seq(overlap) :+
new Interval(overlap.getEnd, interval1.getEnd)
else Seq(overlap) :+
new Interval(interval1.getStart, overlap.getEnd)
}
case _ => Seq(interval1)
}
}
It covers all the possible edge cases!
Upvotes: 1