为什么 datetime.datetime.utcnow() 不包含时区信息?
datetime.datetime.utcnow()
既然这个 datetime
明确是 UTC datetime
,为什么它没有任何时区信息?
我希望这会包含 tzinfo。
datetime.datetime.utcnow()
Why does this datetime
not have any timezone info given that it is explicitly a UTC datetime
?
I would expect that this would contain tzinfo
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请注意,对于 Python 3.2 及以上版本,
datetime
模块包含datetime.timezone
。datetime.utcnow()
的文档 说:因此,
datetime.utcnow()
不会设置tzinfo
来指示它是 UTC,而是datetime.now(datetime.timezone.utc)
> 确实返回设置了tzinfo
的 UTC 时间。所以你可以这样做:
从Python 3.11开始,还存在
datetime。 UTC
相当于datetime.timezone.utc
。因此,您也可以执行datetime.datetime.now(datetime.UTC)
。Note that for Python 3.2 onwards, the
datetime
module containsdatetime.timezone
. The documentation fordatetime.utcnow()
says:So,
datetime.utcnow()
doesn't settzinfo
to indicate that it is UTC, butdatetime.now(datetime.timezone.utc)
does return UTC time withtzinfo
set.So you can do:
Since Python 3.11, there also exists
datetime.UTC
which is equivalent todatetime.timezone.utc
. So you can also dodatetime.datetime.now(datetime.UTC)
.这意味着它是天真的时区,所以你不能将它与
datetime.astimezone
一起使用你可以给它一个像这样的时区
现在你可以更改时区
要获取给定时区的当前时间,你可以直接将 tzinfo 传递给
datetime.now()
:它适用于任何时区,包括遵守夏令时 (DST) 的时区,即,它适用于在不同时间可能具有不同 utc 偏移量的时区(非固定 utc 偏移量)。不要使用 tz.localize(datetime.now()) - 当本地时间不明确时,它可能会在 DST 结束转换期间失败。
That means it is timezone naive, so you can't use it with
datetime.astimezone
you can give it a timezone like this
now you can change timezones
To get the current time in a given timezone, you could pass tzinfo to
datetime.now()
directly:It works for any timezone including those that observe daylight saving time (DST) i.e., it works for timezones that may have different utc offsets at different times (non-fixed utc offset). Don't use
tz.localize(datetime.now())
-- it may fail during end-of-DST transition when the local time is ambiguous.在 Python 3.2 之前,标准 Python 库不包含任何 tzinfo 类。我只能猜测原因。就我个人而言,我认为不包含 UTC 的 tzinfo 类是一个错误,因为该类没有争议,足以有一个标准实现。尽管库中没有实现,但在
tzinfo
文档。一旦您拥有 UTC
tzinfo
对象,您仍然无法将其与utcnow
一起使用。为了将当前时间作为感知的日期时间对象:在 Python 3.2 中,他们最终在库中放入了 UTC
tzinfo
类:在 Python 3.9 中,他们为所有其他创建了
tzinfo
类时区。请参阅PEP 615 - 标准库中对 IANA 时区数据库的支持了解所有详细信息。The standard Python libraries didn't include any tzinfo classes until Python 3.2. I can only guess at the reasons. Personally I think it was a mistake not to include a tzinfo class for UTC, because that one is uncontroversial enough to have a standard implementation. Although there was no implementation in the library, there is one given as an example in the
tzinfo
documentation.Once you have a UTC
tzinfo
object, you still can't use it withutcnow
. To get the current time as an aware datetime object:In Python 3.2 they finally put a UTC
tzinfo
class in the library:In Python 3.9 they created
tzinfo
classes for all the other time zones. See PEP 615 -- Support for the IANA Time Zone Database in the Standard Library for all the details.pytz 模块是一个选项,还有另一个 python-dateutil,虽然也是第三方包,但可能已经可用,具体取决于您的其他依赖项和操作系统。
我只是想包含此方法以供参考 - 如果您已经出于其他目的安装了
python-dateutil
,则可以使用其tzinfo
而不是使用pytz 进行复制
我倾向于同意对
utcnow
的调用应包含 UTC 时区信息。我怀疑这不包括在内,因为本机日期时间库默认为天真的日期时间以实现交叉兼容性。The
pytz
module is one option, and there is anotherpython-dateutil
, which although is also third party package, may already be available depending on your other dependencies and operating system.I just wanted to include this methodology for reference- if you've already installed
python-dateutil
for other purposes, you can use itstzinfo
instead of duplicating withpytz
I tend to agree that calls to
utcnow
should include the UTC timezone information. I suspect that this is not included because the native datetime library defaults to naive datetimes for cross compatibility.在 Python 3.2+ 中添加
时区
信息To add
timezone
information in Python 3.2+Julien Danjou 写了一篇很好的文章,解释了 原因你永远不应该处理时区。摘录:
唉,即使您可以使用
utcnow()
,您仍然不会看到时区信息,正如您发现的那样。建议:
Julien Danjou wrote a good article explaining why you should never deal with timezones. An excerpt:
Alas, even though you may use
utcnow()
, you still won't see the timezone info, as you discovered.Recommendations:
datetime.datetime.utcnow()
将 UTC 时间返回为原始日期时间对象的行为显然是有问题的,必须修复。如果您的系统本地时区不是 UTC,则可能会导致意外结果,因为日期时间库假定原始日期时间对象来表示系统本地时间。例如,datetime.datetime.utcnow().timestaamp()
给出的时间戳比我计算机上的正确值提前 4 小时。另外,从 python 3.6 开始,可以在原始日期时间实例上调用 datetime.astimezone() ,但是 datetime.datetime.utcnow().astimezone(any_timezone) 给出错误的结果除非您的系统本地时区是 UTC。The behaviour of
datetime.datetime.utcnow()
returning UTC time as naive datetime object is obviously problematic and must be fixed. It can lead to unexpected result if your system local timezone is not UTC, since datetime library presume naive datetime object to represent system local time. For example,datetime.datetime.utcnow().timestaamp()
gives timestamp of 4 hours ahead from correct value on my computer. Also, as of python 3.6,datetime.astimezone()
can be called on naive datetime instances, butdatetime.datetime.utcnow().astimezone(any_timezone)
gives wrong result unless your system local timezone is UTC.它还应该包括
now()
。相关问题。在此之前,
now()
优于today()
和utcnow()
。It should include and
now()
also. Related issue.So till that,
now()
is preferred overtoday()
andutcnow()
.UTC 日期不需要任何时区信息,因为它们是 UTC,根据定义,这意味着它们没有偏移量。
UTC dates don't need any timezone info since they're UTC, which by definition means that they have no offset.