gpt4 book ai didi

python - UTC ISO 8601 日期的时间戳

转载 作者:行者123 更新时间:2023-12-02 03:43:12 30 4
gpt4 key购买 nike

这个

import datetime
datetime.datetime.fromtimestamp(1501545600).isoformat()

应该输出2017-08-01T00:00:00Z2017-08-01T00:00:00但我得到

2017-08-01T02:00:00

相反,可能是因为本地时区。

如何使 .isoformat() 输出 UTC 时间,即 2017-08-01T00:00:00

如果可能的话,我只想使用datetime,而不使用其他第三方库,例如arrowdateutils

最佳答案

使用utcfromtimestamp()而不是fromtimestamp()。来自 docs :

Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None.

fromtimestamp() 中未指定 tzinfo:

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.

这似乎不是你想要的。

关于python - UTC ISO 8601 日期的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47780897/

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