Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Error : unsupported operand type(s) for +: 'int' and 'datetime.timedelta'

I have written a function in python that accepts a timestamp and returns the timestamp with respect to the current timezone.

Code

def datetime_from_utc_to_local(utc_datetime):
    now_timestamp = time.time()
    offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
    return utc_datetime + offset

Error:

unsupported operand type(s) for +: 'int' and 'datetime.timedelta'

Can you please help me fix this error.

I want this function to return the timestamp

like image 320
abhinsit Avatar asked Sep 17 '14 10:09

abhinsit


1 Answers

offset is a datetime.timedelta object. If you need just the seconds, extract them with timedelta.total_seconds():

return utc_datetime + offset.total_seconds()

Your function signature however, suggests it was expecting you to feed it a datetime.datetime() object, in which case you shouldn't change this function, but the code that calls it. Clearly you are giving it an integer instead.

like image 182
Martijn Pieters Avatar answered Nov 14 '22 23:11

Martijn Pieters