问题
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