gpt4 book ai didi

python - datetime.fromtimestamp() - 返回我的本地时区而不是 GMT

转载 作者:行者123 更新时间:2023-12-05 00:58:31 27 4
gpt4 key购买 nike

我在将 timestamp 转换为 GMT 时遇到问题。据我所知, timestamp 总是在 GMT 时间,所以我希望 datetime.fromtimestamp 返回 GMT 或时区感知日期时间,但它返回我的本地(布拉迪斯拉发/布拉格)日期时间。

import datetime
datetime.datetime.fromtimestamp(1566720000)
datetime.datetime(2019, 8, 25, 10, 0)

但是根据 Epoch Converter这是

格林威治标准时间:2019 年 8 月 25 日星期日上午 8:00:00

编辑:datetime.datetime.fromtimestamp(1566720000).tzinfo 不返回任何内容,因此它不知道 tz。

你知道问题出在哪里吗?

最佳答案

fromtimestamp() 返回本地日期和时间。如果需要 tz-aware,则必须指定参数 tz:

https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

如果您需要 UTC 日期时间对象,请改用 utcfromtimestamp:

datetime.utcfromtimestamp(timestamp)

关于python - datetime.fromtimestamp() - 返回我的本地时区而不是 GMT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646562/

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