0

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.

Eric
  • 368
  • 1
  • 7
  • 19
  • 1
    Show us a `b` sample. – Pedro Lobito Dec 07 '18 at 17:08
  • b[2] = 2018-12-10 09:00:00 b[3] = 2018-12-10 10:00:00 – Eric Dec 07 '18 at 17:09
  • 1
    `start_date_time >= str(b[2])`, is `b[2]` a `datetime` object casted to a `str`? – Pedro Lobito Dec 07 '18 at 17:11
  • yes it's being casted to a string for comparison – Eric Dec 07 '18 at 17:12
  • 1
    Convert both the user input (`start_date_time` , `end_date_time` ) and `b` to a `datetime` object and u should be all set. – Pedro Lobito Dec 07 '18 at 17:14
  • I did already... the comparison is being done. the problem is when I chose a smaller start date/time than the one that already exists an a larger end date/time than the one that already exists. Obviously my IF Statement are both being skipped because they don't fall under the stated clause. – Eric Dec 07 '18 at 17:15
  • 2
    You're comparing [`strings`](https://docs.python.org/3.1/library/string.html), not [`datetime`](https://docs.python.org/3/library/datetime.html) objects. – Pedro Lobito Dec 07 '18 at 17:16
  • Yes, I get your point and I've converted them to date-time objects, but the problem I have is still the same even if I am not using strings – Eric Dec 07 '18 at 17:17
  • 1
    ***Yes, I get your point and I've converted them to date-time objects, but the problem I have is still the same even if I am not using strings*** -Try the proposed solution, you still get any errors, come back an tell us exactly which one. A coder's life is 95% debugging and 5% coffee... – Pedro Lobito Dec 07 '18 at 17:20
  • I've converted them back to time/dates and i've also updated the post, but i'm still encountering the same issue. Thanks for helping by the way ;) – Eric Dec 07 '18 at 17:21
  • NP. "***but i'm still encountering the same issue***" - Million dollar question... Which one? Please be specific. – Pedro Lobito Dec 07 '18 at 17:22
  • As described before I have a start_time and an end_time for the booking. Now imagine I want to book the room from 8:00AM - 12:00PM but there is already a booking in that room from 10:00AM - 11:00AM. It is still letting me create the booking because i have bad logic in my if statements. On the other hand If i try to create a booking let's say from 9:00AM - 10:00AM it won't let me because the IF statement works like that. The problem is when both the start time is less than the already booked start time and the end time is greater than the already booked end time. Got it? – Eric Dec 07 '18 at 17:26

1 Answers1

0

Extract time from datetime and then compare

start_time =start_date_time.strftime('%H:%M:%S')

Similarly change start_end_time ,b[2] and b[3]

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51