datetime.datetime.utcnow()
为什么这个datetime没有任何时区信息,因为它是一个明确的UTC日期时间?
我希望它包含tzinfo。
datetime.datetime.utcnow()
为什么这个datetime没有任何时区信息,因为它是一个明确的UTC日期时间?
我希望它包含tzinfo。
当前回答
UTC日期不需要任何时区信息,因为它们是UTC,这根据定义意味着它们没有偏移量。
其他回答
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.
UTC日期不需要任何时区信息,因为它们是UTC,这根据定义意味着它们没有偏移量。
在Python 3.2+中添加时区信息
import datetime
>>> d = datetime.datetime.now(tz=datetime.timezone.utc)
>>> print(d.tzinfo)
'UTC+00:00'
from datetime import datetime
from dateutil.relativedelta import relativedelta
d = datetime.now()
date = datetime.isoformat(d).split('.')[0]
d_month = datetime.today() + relativedelta(months=1)
next_month = datetime.isoformat(d_month).split('.')[0]
标准的Python库直到Python 3.2才包含任何tzinfo类。我只能猜测原因。我个人认为没有为UTC包含tzinfo类是一个错误,因为它没有足够的争议性,可以有一个标准的实现。尽管库中没有实现,但tzinfo文档中给出了一个示例。
from datetime import timedelta, tzinfo
ZERO = timedelta(0)
# A UTC class.
class UTC(tzinfo):
"""UTC"""
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return ZERO
utc = UTC()
有了UTC tzinfo对象后,仍然不能将其用于utcnow。获取当前时间作为感知datetime对象:
from datetime import datetime
now = datetime.now(utc)
在Python 3.2中,他们终于在库中加入了一个UTC tzinfo类:
from datetime import datetime, timezone
now = datetime.now(timezone.utc)
在Python 3.9中,他们为所有其他时区创建了tzinfo类。有关详细信息,请参阅标准库中的PEP 615—对IANA时区数据库的支持。