gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:39:27 25 4
gpt4 key购买 nike

我用 python 编写了一个函数,它接受时间戳并返回相对于当前时区的时间戳。

代码

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

错误:

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

你能帮我解决这个错误吗?

我希望这个函数返回时间戳

最佳答案

offsetdatetime.timedelta object .如果您只需要秒数,请使用 timedelta.total_seconds() 提取它们:

return utc_datetime + offset.total_seconds()

然而,您的函数签名表明它期望您向它提供一个datetime.datetime() 对象,在这种情况下您不应该更改此函数,但是调用它的代码。显然,您给它的是一个整数。

关于Python 错误 : unsupported operand type(s) for +: 'int' and 'datetime.timedelta' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com