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

巧了我就是萌 提交于 2020-01-30 02:38:38

问题


I have written a function in python that accepts a timestamp and return the timestamp w.r.t 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


回答1:


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.



来源:https://stackoverflow.com/questions/25888726/python-error-unsupported-operand-types-for-int-and-datetime-timedelta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!