I have a table in database which stores items
that can be rented for a period of few days.
Now whenever someone rents an item
, they specify a start_date
and an end_date
(basically a date range
for which they want to rent that item
).
I want to know what is the efficient algorithm (in terms of time complexity) to check that input date range
doesn't overlap with existing date ranges
in the database.
Illustration:
|<------existing 1------->|
|<------existing 2------->|
|<------ input date range ---->| (which clearly, overlaps)
Note:
This question is not a duplicate of this question. That one checks whether two date ranges
overlap. My question is about an input date range
overlapping with multiple existing date ranges
Another Note:
In case you are confused by this question's tags, I am open to both kind of answers, language-agnostic
as well as language-specific
.
For those who want to give language-specific
answer, here are more details:
I have a django 1.9
project running on python 3.4
with PostgreSQL
as database. My models are:
class Item(models.Model):
name = models.CharField(max_length=100)
class BookingPeriod(models.Model):
item = models.ForeignKey(Item)
start_date = models.DateField()
end_date = models.DateField()