I have a simple question (at least I thought so), I want to add a time value to a datetime value in Python. The values are read from an excel file.
I have the following code:
import xlrd
from datetime import time, datetime, timedelta
book = xlrd.open_workbook('C:\\Users\eline\Documents\***\***\Python\Example 1.xlsx')
sh = book.sheet_by_name("test")
arr_time = datetime(*xlrd.xldate_as_tuple(sh.cell_value(1,2), book.datemode))
print(arr_time)
a2 = sh.cell_value(1,5)
# converting float from excel to time value
print(int(a2*24*3600))
x = int(a2*24*3600)
slack_time = time(x//3600, (x%3600)//60, x%60)
print(slack_time)
new_arr_time = arr_time + slack_time
print(new_arr_time)
arr_time is here a datetime value which can vary e.g.:
2016-08-28 13:10:00
slack_time is here a time in minutes (sometimes hours) which can vary e.g.:
00:15:00
I would like to add the slack time (e.g. 15 minutes) to the arr_time. Thus for this example I would like to get the following output for new_arr_time:
2016-08-28 13:25:00
However, when running my code I get the following error: "TypeError: unsupported operand type(s) for +: ‘datetime.datetime’ and ‘datetime.time’". From this, I understand that I cannot add a time value to a datetime value, but when converting slack_time to a datetime value and then adding slack_time to arr_time I get a similar error (although subtracting works that way). I know I can use timedelta(minutes = 15) but since the values from the excel file vary and sometimes contain hours this does not work for me.
So my question is: how can I add a time value which is read from excel to a datetime value?