0

I have many time range in database and build a form to insert new time range in it.

also i write a function to check overlaping between new time range with other time range

when first range for example is 14:00 to 15:00 and second range is 16:00 to 17:00 this function works well and dont return True.

if second range is 15:00 to 16:00 although two range have not any overlap but return True !

def is_overlap(start_date, end_date, obj):
"""
Check overlaping between start_date and end_date
start_date = Start Meeting DateTime
end_date = End Meeting DateTime
obj = Model query objects
"""

for q in obj:
    latest_start = max(start_date, q.start_date)
    earliest_end = min(end_date, q.end_date)

    if (earliest_end - latest_start).days == 0:
        if not (earliest_end - latest_start).seconds == 0:
            return True
            break

    if (earliest_end - latest_start).days >= 0:
        return True
        break
Ehsan
  • 604
  • 7
  • 21
  • 1
    Here are two possibly duplicate questions https://stackoverflow.com/questions/9044084/efficient-date-range-overlap-calculation-in-python and https://stackoverflow.com/questions/14885188/checking-for-overlap-between-time-spans – Mazdak Sep 15 '17 at 19:23

1 Answers1

1
    if (earliest_end - latest_start).days >= 0:
        return True
        break

This statement is what is returning True. You should take it out.

minterm
  • 259
  • 3
  • 13