datetime.datetime.utcnow()
为什么这个datetime没有任何时区信息,因为它是一个明确的UTC日期时间?
我希望它包含tzinfo。
datetime.datetime.utcnow()
为什么这个datetime没有任何时区信息,因为它是一个明确的UTC日期时间?
我希望它包含tzinfo。
当前回答
pytz模块是一个选项,还有另一个python-dateutil,尽管它也是第三方包,但可能已经可用,这取决于您的其他依赖项和操作系统。
我只是想包括这个方法作为参考——如果你已经为其他目的安装了python-dateutil,你可以使用它的tzinfo,而不是用pytz复制
import datetime
import dateutil.tz
# Get the UTC time with datetime.now:
utcdt = datetime.datetime.now(dateutil.tz.tzutc())
# Get the UTC time with datetime.utcnow:
utcdt = datetime.datetime.utcnow()
utcdt = utcdt.replace(tzinfo=dateutil.tz.tzutc())
# For fun- get the local time
localdt = datetime.datetime.now(dateutil.tz.tzlocal())
我倾向于同意调用utcnow应该包含UTC时区信息。我怀疑这没有包括在内,因为本机datetime库默认为naive datetimes以实现交叉兼容性。
其他回答
它还应该包括and now()。相关的问题。
因此,在此之前,now()优先于today()和utcnow()。
from datetime import datetime, timezone
utc = timezone.utc
date = datetime.now(utc)
print(date) # 2022-04-06 05:40:13.025347+00:00
Julien Danjou写了一篇很好的文章,解释了为什么你永远不应该处理时区问题。摘录:
实际上,Python datetime API总是返回未意识到的datetime对象, 这是非常不幸的。的确,只要你有了这个 对象,没有办法知道时区是什么,因此这些 对象本身非常“无用”。
遗憾的是,即使您可以使用utcnow(),您仍然不会看到您所发现的时区信息。
建议:
Always use aware datetime objects, i.e. with timezone information. That makes sure you can compare them directly (aware and unaware datetime objects are not comparable) and will return them correctly to users. Leverage pytz to have timezone objects. Use ISO 8601 as the input and output string format. Use datetime.datetime.isoformat() to return timestamps as string formatted using that format, which includes the timezone information. If you need to parse strings containing ISO 8601 formatted timestamps, you can rely on iso8601, which returns timestamps with correct timezone information. This makes timestamps directly comparable.
pytz模块是一个选项,还有另一个python-dateutil,尽管它也是第三方包,但可能已经可用,这取决于您的其他依赖项和操作系统。
我只是想包括这个方法作为参考——如果你已经为其他目的安装了python-dateutil,你可以使用它的tzinfo,而不是用pytz复制
import datetime
import dateutil.tz
# Get the UTC time with datetime.now:
utcdt = datetime.datetime.now(dateutil.tz.tzutc())
# Get the UTC time with datetime.utcnow:
utcdt = datetime.datetime.utcnow()
utcdt = utcdt.replace(tzinfo=dateutil.tz.tzutc())
# For fun- get the local time
localdt = datetime.datetime.now(dateutil.tz.tzlocal())
我倾向于同意调用utcnow应该包含UTC时区信息。我怀疑这没有包括在内,因为本机datetime库默认为naive datetimes以实现交叉兼容性。
UTC日期不需要任何时区信息,因为它们是UTC,这根据定义意味着它们没有偏移量。
在Python 3.2+中添加时区信息
import datetime
>>> d = datetime.datetime.now(tz=datetime.timezone.utc)
>>> print(d.tzinfo)
'UTC+00:00'