What is the easy way to validate scheduling data in python

Well everyone. Honestly my head is about to explode on this situation. My whole day spent on this thing to solve it but i could not do that, so finally here i am on stackoverflow and asking for help from some of you experts. who can help me out in solving this problem. Well i have json object that contain the scheduling of a single day. and let me explain each part of this so you guys understand where i'm struggling so far.

{
   "timings":{
      "id":43,
      "partition":7,
      "day_name":"Tuesday",
      "full_day":false,
      "close_day":false,
      "start_time":"2022-12-20 00:00:00",
      "close_time":"2022-12-20 23:00:00"
   },
   "pricings":[
      {
         "partition":7,
         "day_name":"Tuesday",
         "peak_hours":true,
         "start_time":"2022-12-20 17:00:00",
         "close_time":"2022-12-20 23:00:00",
         "price":"1300"
      },
      {
         "partition":7,
         "day_name":"Tuesday",
         "peak_hours":true,
         "start_time":"2022-12-20 00:00:00",
         "close_time":"2022-12-20 04:00:00",
         "price":"1300"
      },
      {
         "id":54,
         "partition":7,
         "day_name":"Tuesday",
         "peak_hours":false,
         "start_time":"2022-12-20 04:00:00",
         "close_time":"2022-12-20 17:00:00",
         "price":2000
      }
   ]
}

as you can see the first key is timings. Which contains the detail of shop when it will open and when it'll close. simple and nice.

then we move on second key which is pricings. its a list of objects that contains that is it a peak hour or off_peak_hours or when they start and when they end. Now problem arise here : How can we validate all timing values such as start times and close times of each entry. they should not over lap each other. and all time should be within shop opening and closing timings.

e.g with valid data : shop opens at : 1 am shop closes at : 11 am

now we have list of hours : first peak hours start at : 1 am to 4am second off_hours starts at : 4 am to 7am third peak_hours again starts at : 7am to 11am .

e.g in valid data

first peak hours start at : 1 am to 5am second off_hours starts at : 4 am to 7am third peak_hours again starts at : 7am to 11am .

as you can see in first line peak hour start from 1 am to 5am second off hours start at : 4am to 7am

here is the problem comes that 4am to 5am in over lapping that single hour is coming into peak hour as well as off peak hour so that input is not valid.

I know this is quite complicated. But anyone who help me will be appreciated.!

Assuming objects in the pricings list are entered in chronological order (start at 00:00:00, end at 23:59:59) then we can jsut check to mak sure the diffrence between the i close_time and i+1 start_time is 0. If it is any other number then there must be overlap.

This can be accomplished using datetime

import datetime

validFlag = True
for i in range(len(obj["pricings"])-1):
    currentClose = datetime.datetime.strptime(obj["pricings"][i]["close_time"],'%Y-%m-%d %H:%M:%S')
    nextStart = datetime.datetime.strptime(obj["pricings"][i+1]["start_time"],'%Y-%m-%d %H:%M:%S')
    if currentClose-nextStart==datetime.timedelta(0):
        pass
    else:
        validFlag = False

if validFlag:
    print("Valid")
else:
    print("Not Valid")

Here we take the strings that indicate the time, change them to date times, find the difference between them. If the difference is anything but 0, we check a flag that indicates that it is not a valid entry.

This works according to your wishes given that the entries are in chronological order. In your example however they are not. So we will go over an example for this as well. The only idffrence in this second example is the use of list.sort() to sort the times

startTime = []
    closeTime = []
    for i in range(len(obj["pricings"])):
        startTime.append(datetime.datetime.strptime(obj["pricings"][i]["start_time"],'%Y-%m-%d %H:%M:%S'))
        closeTime.append(datetime.datetime.strptime(obj["pricings"][i]["close_time"],'%Y-%m-%d %H:%M:%S'))
    startTime.sort()
    closeTime.sort()
    validFlag = True
    for i in range(len(startTime)-1):
        if closeTime[i]-startTime[i+1]==datetime.timedelta(0):
            pass
        else:
            validFlag = False

    if validFlag:
        print("Valid")
    else:
        print("Not Valid")
Back to Top