Assume we have an empty list called interval and a list of lists named shifts that consist of: day_of_week, shift_start, and shift_end:
interval = [] shifts = [['Mon', '01:00', '22:00'], ['Tue', '02:00', '23:00'], ['Wed', '01:15', '23:30'], ['Thu', '01:00', '21:00'], ['Fri', '02:30', '22:00'], ['Sat', '01:00', '21:00'], ['Sun', '03:00', '23:00'] ] The interval list will consist of the time differences (in minutes) between the shifts. As an example, the first element of interval will be the time difference between 'Mon', '22:00' and 'Tue', '02:00' which is 240 and the last element will be time difference between 'Sun', '23:00' and 'Mon','01:00' which is 120
This is my code:
interval = [] start_time = [] end_time = [] for each in shifts: start_time.append([each[0],each[1]]) end_time.append([each[0],each[2]]) start_val = ','.join([each[0], each[1]]) start_time.append(start_val) end_val = ','.join([each[0], each[2]]) end_time.append(end_val) for i in range(len(shifts)): t1 = datetime.datetime.strptime(end_time[i],'%a').strftime() if(i < len(shifts)-1): t2 = datetime.datetime.strptime(start_time[i+1], '%a,%H:%M') else: t2 = datetime.datetime.strptime(start_time[0], '%a,%H:%M') delta = int((t1-t2).total_seconds()/60) interval.append(delta) The problem is, the program ignore the day_of_week, so it assume the t1 and t2 were on the same day. So in my program, the first element of interval is 1320 which is 22 hours.
Need help to solve the problems. If my whole code is wrong or inconvenient please provide the best way to solve it.