2

I am trying to write a function in python whereby I can input a start time and end time and it will return the total hours.

Currently I have been able to write a function where I input for example ('07:30:00', '12:00:00') and it returns 4.5

I want to be able to import a list though. For example,

('07:30:00, 08:30:00', '12:00:00, 12:00:00') and have it return 4.5 , 3.5 etc....

How do I alter my code so I can do this?

Thanks

I have been messing around for hours but am very new to python so do not know how to progress from here

def compute_opening_duration(opening_time, closing_time):
    while True:
        try:
            FORMAT = '%H:%M:%S'
            tdelta = datetime.strptime(closing_time, FORMAT) - datetime.strptime(opening_time, FORMAT)
            tdelta_s = tdelta.total_seconds()
            tdelta_m = tdelta_s/60
            tdelta_h = tdelta_m/60
            print(tdelta_h)
            break
        except ValueError:
            print('-1')
            break
  • Aditional parametars means aditional checks. You are passing multiple times in a single parametars, so you must divide them. `opening_time.split(',')` will do the job. It will break string in n pieces based on the separator. Access them by index, it will be just a simple list. – Aleksandar Apr 10 '19 at 06:01

2 Answers2

1

Pass array as a parameter to function. Check if opening time array have the same length as closing time array lenght. Declare result array, in line where you compute tdelta you must than append to result array.

def compute_opening_duration(opening_time_arr, closing_time_arr):
    if len(opening_time_arr) != len(closing_time_arr):
        return
    resultTime = []
    for idx, closing_timein enumerate(closing_time_arr) :
            try:
                FORMAT = '%H:%M:%S'
                tdelta = datetime.strptime(closing_time, FORMAT) - 
    datetime.strptime(opening_time_arr[idx], FORMAT)
                resultTime.append(tdelta)
                tdelta_s = tdelta.total_seconds()
                tdelta_m = tdelta_s/60
                tdelta_h = tdelta_m/60
                #print(tdelta_h)
            except ValueError:
                pass   
            #print('-1')
    return resultTime
ElConrado
  • 1,477
  • 4
  • 20
  • 46
  • How can I take the result and store it as a np.array WITHOUT printing the function in the background. I am trying to define array = np.array(compute_blah_blah) but it keeps printing. – Oliver Wright Apr 10 '19 at 06:38
  • Are you asking for print function? Or other? – ElConrado Apr 10 '19 at 06:46
  • Id like to print it later once I have converted it to an array. I tried Return instead of print but that returns errors – Oliver Wright Apr 10 '19 at 06:55
  • There is a mistake in if statement. Moreover I change print function to pass. What type of error do you have and in which line of code? – ElConrado Apr 10 '19 at 06:56
  • No errors, the code works perfectly. However, I want to convert it to an array. – Oliver Wright Apr 10 '19 at 07:01
  • x = np.array(compute_opening_duration(open_arr, close_arr) . When I do this, it still prints all the values when I don't want it to. I have tried return instead of print in the function so it doesn't print whne I try convert but it does not work. – Oliver Wright Apr 10 '19 at 07:02
  • First, try compute on one line y = compute_opening_duration(open_arr, close_arr) than assign to x as np.array(y). Second, could you try this: https://stackoverflow.com/a/15868531/4510954 – ElConrado Apr 10 '19 at 07:05
  • No luck, if I try convert to an array all I get is 'None' – Oliver Wright Apr 10 '19 at 07:10
1

If I got the question correctly

def compute_opening_duration(time_list):
    # convert to datetime: 
    FORMAT = '%H:%M:%S'
    time_list = [datetime.strptime(time, FORMAT) for time in time_list]

    # compute and return deltas
    return [(close_time-open_time).total_seconds()/3600 
            for open_time, close_time in zip(time_list[:-1], time_list[1:])


Yuri Feldman
  • 2,354
  • 1
  • 20
  • 23
  • Clever approach. Looks great to me. One question, can you clarify how the `zip` function works here? Thanks! – Swadhikar Apr 10 '19 at 07:05
  • thanks! zip produces a generator which outputs pairs of consecutive times from time_list, by pairing elements at indexes 0,...,n-2 with indexes (1,...,n-1) respectively (the two list comprehensions in zip inputs) – Yuri Feldman Apr 10 '19 at 07:55