Preamble: I've got a car park with a hundred spaces. I store reservations as free-form start and end times against specified spaces. We currently let people query the car park for a specific time and can easily tell them if there's a free space. But I now have a requirement to advertise how many free spaces there are for 1 hour slots from 7-7, for a span of 5 days. Using the existing query would mean I needed to do 60 queries which is too slow to do interactively and seems just too boggy to do non-interactively.
So I'm looking for a way to map the time between bookings, and then coerce that into slot availability which I can store separately. This process might be a bit boggy in itself but it might also make querying the database for specific availability somewhat faster.
What I want to do is define a day and then "subtract" bookings for that day and be left with a list of available time ranges around those bookings, that I can then go onto coerce into hour-long slots.
I'm halfway through doing this myself by starting from a given time, ranging up to the first booking, skipping to the end, and repeating until I hit the end of the slot but it's pretty dull stuff and I'm struggling to believe this hasn't been done before. I'm using Python but I'm happy to port. Something like...
today = Day(start=datetime.time(7), end=datetime.time(19))
print(today.ranges)
# [(datetime.time(7), datetime.time(19))]
day.subtract(datetime.time(12), datetime.time(13))
print(today.ranges)
# [(datetime.time(7), datetime.time(12)), (datetime.time(13), datetime.time(19))]
I realise I'm probably drifting into a level of specificity where it might only benefit me, but this idea of ranging and splitting time is where I need help. Any ideas?