Adding 2 1/2 Hours To A Time Object In Python
I need to be able to convert time on an time object I recieve from a sql database into python. Here is the current python code I am using without any conversions. I need to add 2 a
Solution 1:
Have you looked at the datetime module? http://docs.python.org/library/datetime.html
Convert your SQL time into a datetime, and make a timedelta object of 2.5 hours. Then add the two.
from datetime importdatetimedt= datetime.strptime( date, '%Y-%m-%d %H:%M' )
dt_plus_25 = dt + datetime.timedelta( 0, 2*60*60 + 30*60 )
Solution 2:
Solution 3:
Try this (switch sqlite3 with whatever db interface you are using):
f= lastReport[0][0]
newtime = sqlite3.Time(f.hour + 2, f.minute + 30, f.second, f.microsecond)
I thought that adding datetime.timedelta(0, 2.5*60*60) like Igancio suggests would be the best solution, but I get:
'unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'.
Solution 4:
I usually use the 3rd party module, dateutil, for this sort of thing. It makes this sort of time-shifting much easier to do and understand:
Post a Comment for "Adding 2 1/2 Hours To A Time Object In Python"