Reputation: 18427
Say I have 2 sets of time spans. So one is from 10:00 am to 12:00 pm, the other from 11:00 am to 13:00 pm, both on the same day. How can I easily calculate to find out if they overlap?
My code is irrelevant, but just for understanding the use case - I'm developing a Django app where I want to allow building some sort of schedule, something like this:
class TimeSpan(models.Model):
start = models.DateTimeField()
end = models.DateTimeField()
class Worker(models.Model):
name = models.CharField(max_length=100)
limitations = models.ManyToManyField(TimeSpan)
Class Shift(models.Model):
workers = models.ManyToManyField(Workers)
timespan = models.ForeignKey(TimeSpan)
I need to be able to compare a TimeSpan object of shifts, with the limitations of each worker. What would be a good way to do that?
Upvotes: 2
Views: 308
Reputation: 32244
The easiest way to find if two time ranges overlap is like so
overlap = span_1_start < span_2_end and span_1_end > span_2_start
Upvotes: 1