0

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.

4
  • Isn't Monday to Tuesday also 240 minutes not 480? 4 hours difference x 60 minutes? Commented Feb 22, 2018 at 18:06
  • Your input data is ambiguous. There is no way for Python (or anyone!) to know the difference in time between a Tuesday and a Friday. Which Tuesday? Which Friday? You'll have to implement that logic yourself, instead of relying on strptime. Commented Feb 22, 2018 at 18:06
  • Your code gives traceback like this. Traceback (most recent call last): File "/home/sworks/try.py", line 28, in <module> t1 = datetime.datetime.strptime(end_time[i],'%a').strftime() TypeError: strptime() argument 1 must be str, not list Commented Feb 22, 2018 at 18:07
  • Are these days in the same week? Python date time works on full dates and times, docs.python.org/3/library/datetime.html. Commented Feb 22, 2018 at 18:10

3 Answers 3

1

This would help probably

from datetime import datetime formattime = datetime.strptime 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'] ] 

This appending here, we can call it a small hacky preprocessing.

shifts.append(shifts[0]) 

Method get_delta returns timedelta of two datetime params.

def get_delta(day1, day2): day, start, end, n_day, n_start, n_end = day1 + day2 return formattime(n_day + n_start, '%a%H:%M') - formattime(day + end, '%a%H:%M') 

Enumerating list provides you with index and the value which are unpacked as i, day1. And further there is a condition for end case. Day2 is taken by next index.

for i, day1 in enumerate(shifts): if i+1 == len(shifts): break day2 = shifts[i+1] delta = get_delta(day1, day2).seconds / 60 interval.append(delta) print(interval) 

Result (in Minutes):

 [240, 135, 90, 330, 180, 360, 120] 
Sign up to request clarification or add additional context in comments.

2 Comments

could you explain what happen in get_delta function?
get_delta by its name returns delta of two datetimes. The first line appends two lists and unpacks into 6 variables. And further second line returns delta of two datetimes which we get from formattime.
1

You can convert the start times and the end times to epoch (which is basically a number), and then simply calculate the difference.

This difference will be in seconds (or ms, depending upon implementation), which you can further convert to seconds/minutes/hours as per your requirement.

Epoch is basically used for storing the date, but since they are not very important to you (as in Monday 18th and Monday 25th is same), just take a random date and assign epoch to start and end times. Since it's more or less a standard thing, there will be a lot of libraries to help you out.

So

start_time.append([(somedate day)+ each[0],(somedate + day) + each[1]]) end_time.append([(somdate + day) +each[0],(somedate + day)each[2]]) 

...

t1 = int(datetime.datetime.strptime('Jan 1 1970' + end_time[i],yourFormat)) t2 = int(datetime.datetime.strptime('Jan 2 1970' + start_time[i+1], yourFormat)) 

This difference will give you expected result.

Comments

1

Choose some random dates that fall on Sunday-Saturday

import datetime sun = datetime.datetime(2018,2,18) mon = datetime.datetime(2018,2,19) tue = datetime.datetime(2018,2,20) wed = datetime.datetime(2018,2,21) thu = datetime.datetime(2018,2,22) fri = datetime.datetime(2018,2,23) sat = datetime.datetime(2018,2,24) day_of_week = {'sun':sun,'mon':mon,'tue':tue, 'wed':wed, 'thu':thu,'fri':fri, 'sat':sat} 

Use a timedelta object to get the next monday

next_week = datetime.timedelta(days=7) 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']] 

Walk through the shifts and make datetime.datetime objects of the start and end times be combining the dates previously created with the shift times

dt_shifts = [] for day, start, end in shifts: hh, mm = map(int, start.split(':')) start = datetime.datetime.combine(day_of_week[day.lower()], datetime.time(hh, mm)) hh, mm = map(int, end.split(':')) end = datetime.datetime.combine(day_of_week[day.lower()], datetime.time(hh, mm)) dt_shifts.append((day, start, end)) 

Get the start and end times for adjacent shifts, subtract and print

for (day0,start0,end0), (day1,start1,end1) in zip(dt_shifts, dt_shifts[1:]): dt = start1 - end0 print(f'{day0} - {day1} | {dt.seconds / 60}') 

Add a week to Monday and use it for the last interval

day0,start0,end0 = day1,start1,end1 day1, start1, end1 = dt_shifts[0][0],dt_shifts[0][1] + next_week, dt_shifts[0][2] + next_week print(f'{day0} - {day1} | {dt.seconds / 60}') 

>>> Mon - Tue | 240.0 Tue - Wed | 135.0 Wed - Thu | 90.0 Thu - Fri | 330.0 Fri - Sat | 180.0 Sat - Sun | 360.0 Sun - Mon | 360.0 >>> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.