hadley/lubridate — Issue #108: Detecting overlapping intervals
Author: cornelius1729
Created: 2012-01-26 14:39:04+00:00
State: open

It seems to be a moderately common problem to want to know whether two time intervals overlap. There currently isn't (AFAIK) a function in lubridate to do this. I suggest the following implementation, based on the algorithm [here] [1].

int_overlaps <- function(int1, int2)
{
(int_start(int1) <= int_end(int2)) &
(int_start(int2) <= int_end(int1))
}

int1 <- new_interval(ymd("2012-01-01"), ymd("2012-01-10"))
int2 <- new_interval(ymd("2012-01-06"), ymd("2012-01-15"))
int3 <- new_interval(ymd("2012-01-11"), ymd("2012-01-20"))
int_overlaps(c(int1, int1, int2), c(int2, int3, int3))

[1]: stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap