如何在 Python 中获取“时区感知"的 datetime.today() 值? [英] How do I get a value of datetime.today() in Python that is "timezone aware"?

查看:21
本文介绍了如何在 Python 中获取“时区感知"的 datetime.today() 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 datetime.datetime.today() 的值中减去一个日期值,以计算某件事是多久以前的.但它抱怨:

I am trying to subtract one date value from the value of datetime.datetime.today() to calculate how long ago something was. But it complains:

TypeError: can't subtract offset-naive and offset-aware datetimes

datetime.datetime.today() 似乎不是时区感知",而我的其他日期值是.如何获得时区感知的 datetime.datetime.today() 值?

The value datetime.datetime.today() doesn't seem to be "timezone aware", while my other date value is. How do I get a value of datetime.datetime.today() that is timezone aware?

现在,它给我的是当地时间,恰好是 PST,即 UTC - 8 小时.最坏的情况,有没有办法可以手动将时区值输入到 datetime.datetime.today() 返回的 datetime 对象并将其设置为 UTC-8?

Right now, it's giving me the time in local time, which happens to be PST, i.e. UTC - 8 hours. Worst case, is there a way I can manually enter a timezone value into the datetime object returned by datetime.datetime.today() and set it to UTC-8?

当然,理想的解决方案是让它自动知道时区.

Of course, the ideal solution would be for it to automatically know the timezone.

推荐答案

在标准库中,没有创建自己的时区类的跨平台方法来创建感知时区.( Python 3.9 引入了 zoneinfo 在提供此功能的标准库中.)

In the standard library, there is no cross-platform way to create aware timezones without creating your own timezone class. ( Python 3.9 introduces zoneinfo in the standard library which does provide this functionality.)

在 Windows 上,有 win32timezone.utcnow(),但这是 pywin32 的一部分.我宁愿建议使用 pytz 库,它有一个不断更新的大多数时区数据库.

On Windows, there's win32timezone.utcnow(), but that's part of pywin32. I would rather suggest to use the pytz library, which has a constantly updated database of most timezones.

使用本地时区可能非常棘手(请参阅下面的进一步阅读"链接),因此您可能更希望在整个应用程序中使用 UTC,尤其是对于算术运算,例如计算两个时间点之间的差异.

Working with local timezones can be very tricky (see "Further reading" links below), so you may rather want to use UTC throughout your application, especially for arithmetic operations like calculating the difference between two time points.

您可以像这样获取当前日期/时间:

You can get the current date/time like so:

import pytz
from datetime import datetime
datetime.utcnow().replace(tzinfo=pytz.utc)

请注意 datetime.today()datetime.now() 返回 local 时间,而不是 UTC 时间,因此应用 .replace(tzinfo=pytz.utc) 对他们来说是不正确的.

Mind that datetime.today() and datetime.now() return the local time, not the UTC time, so applying .replace(tzinfo=pytz.utc) to them would not be correct.

另一个不错的方法是:

datetime.now(pytz.utc)

这有点短,但作用相同.

which is a bit shorter and does the same.

进一步阅读/观看为什么在许多情况下更喜欢 UTC:

Further reading/watching why to prefer UTC in many cases:

  • pytz documentation
  • What Every Developer Should Know About Time – development hints for many real-life use cases
  • The Problem with Time & Timezones - Computerphile – funny, eye-opening explanation about the complexity of working with timezones (video)

这篇关于如何在 Python 中获取“时区感知"的 datetime.today() 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆