Search code examples
pythondatetimetimescheduled-tasks

Find Time Conflicts between a list of Time Ranges and a specified Time Range


I've been working on a project and I am trying to figure out how to compare the start and end time of an object (we'' call it an event) to the time interval of already placed events? I am making a Routine planner and before adding a task to the planner I want to make sure the start and end time of my current event does not conflict with other events? I have three lists; morning afternoon and night and I account for most situations except where the first time is less than a starttime in the list and the second/end is greater than a secondtime in the list.

ex: user enters start:6:00 AM end:02:00PM(6:00 -14:00 PM military time, my program is working with military numbers)

events already in list:

morning:none

afternoon: 12:00PM - 1:00PM/13:00

6am to 2 pm obviously breaks this boundary but for some reason my program isn't flagging it? It could possibly be that based off what my first time is, dictates what list i check. So anything past 12, i check for any conflicts in the afternoon range (PM). Anything less than 12PM is am. Even so, i tried appending all the lists elements to one big list and tried comparing but that was a lost cause. (I wonder if it would be easier to sort the times?) How would I go about handling this more efficiently? I imagine there has to be another way outside of manually typing if statements to account for each case which I did but it's not catching this bit and I'm not sure how to catch it. Thank you for any help!

Compare a specific start/end time range to a list of time ranges to see if there's any time conflicts. If there are then it prints True, otherwise false.


Solution

  • Add a nonce to each range, sort all {start|end s|e, nonce <a,b,c...>, time t} sets by time, check if sorted result is of the form sat1, eat2, sbt3, ebt4, sct5, ect6, ... (a,b,c are of course not "true" nonces since they are each used twice--for start and then again for end).