So I am creating a booking system where a room can only be booked once during a date/time-range
Let's Say the room is already booked on the following date/time
2018-12-10 09:00:00 - 2018-12-10 10:00:00
and this is my python code to determine wether the room is booked already within a time-range when trying to book the room again.
start_date_time = form.start_date_time.data #The Start Time Input
end_date_time = form.end_date_time.data #The End Time Input
exists = 0 #A variable to add to if the time is within range already
for b in fetchBookingByRoomID(room_id): #A function to get the times already booked storing the results in "b" variable
if start_date_time >= b[2] and start_date_time <= b[3]:
exists = exists + 1
if end_date_time >= b[2] and end_date_time <= b[3]:
exists = exists + 1
if exists <= 0:
#BOOK THE ROOM
else:
#ERROR : Time Slot already being used
By doing this i'm managing to detect an overlapping booking if the start time is greater than the already booked start time and if the end time is smaller than the end time of another booking. But If I for example try to book the following:
2018-12-10 08:00:00 - 2018-12-10 12:00:00
It will let me book the room and doesn't detect that there is already a booking during that period because the start time to be booked is less than the start time already booked and the end time to be booked is larger than the end time already booked, so this means that both of the IF statements are not being met and so the code proceeds on creating the booking.
What I want to achieve is that if there is a booking already smaller than the range entered which is still with the range to be booked, it won't let me create the booking.
Can someone please help me figure out the logic? Thanks
The post was marked as a duplicate, but unfortunately the other question is not the same because he is comparing days and here I need to compare times. Also in the other question duplicates are allowed and they are not in my case.