如何使用 pytz 或 datetime 检查日期是否为 UTC 格式?

发布于 2024-11-24 06:35:48 字数 257 浏览 1 评论 0原文

我正在使用 pytz 模块使用以下代码将日期从 America/Los_Angeles 时区转换为 UTC:

TZ = 'America/Los_Angeles'
from = pytz.timezone(TZ)
utc = from.localize(original_date).astimezone(pytz.utc)

现在,我想测试 UTC 值是否实际上采用 UTC 格式。如何使用 pytz 或 datetime 做到这一点?

I'm using the pytz module to translate a date from America/Los_Angeles timezone to UTC using the code below:

TZ = 'America/Los_Angeles'
from = pytz.timezone(TZ)
utc = from.localize(original_date).astimezone(pytz.utc)

Now, I want to test if the UTC value is actually in UTC format or not. How to do that with pytz or datetime ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉城已无爱 2024-12-01 06:35:48

接受的答案不适用于 pytz 对象之外的任何其他对象。由于 pytz 实际上在转换方面相当糟糕 [1](例如正确执行夏令时等),因此最好进行交叉实现检查。

now = datetime.datetime.now(pytz.utc)
if now.tzinfo:
    now.utcoffset().total_seconds() == 0 # returns true

[1] https://pendulum.eustace.io/blog /a-faster-alternative-to-pyz.html

The accepted answer will not work for anything else other than pytz objects. As pytz is actually pretty bad at doing conversions [1] (e.g. properly doing daylight savings, etc.) it is probably better to do a cross-implementation check.

now = datetime.datetime.now(pytz.utc)
if now.tzinfo:
    now.utcoffset().total_seconds() == 0 # returns true

[1] https://pendulum.eustace.io/blog/a-faster-alternative-to-pyz.html

梦罢 2024-12-01 06:35:48
utc.tzinfo == pytz.utc # returns True if utc in UTC

例子:

now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc # returns True

now = now.astimezone(pytz.timezone('America/Los_Angeles'))
now.tzinfo == pytz.utc # returns False
utc.tzinfo == pytz.utc # returns True if utc in UTC

Example:

now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc # returns True

now = now.astimezone(pytz.timezone('America/Los_Angeles'))
now.tzinfo == pytz.utc # returns False
靑春怀旧 2024-12-01 06:35:48

您可以简单地这样做:

from datetime import datetime, timezone
lunch_time = datetime.now(timezone.utc)

if lunch_time.format('%Z') == 'UTC':
     print("Eat food")

这也适用于简单的时间对象,因为 lunch_time.format('%Z') 将返回一个空字符串。此方法也适用于 pytz 或任何其他模块,因为您只是将时区检查为字符串而不是对象(接受的答案不适用于上述时区模块情况,仅适用于 pytz)。

from datetime import datetime
import pytz
dinner_time = datetime.now(pytz.timezone('UTC'))

if dinner_time.format('%Z') == 'UTC':
     print("Hungry!")

注意:这也将消除时区是 GMT 时区而不是 UTC 时区的可能性。另一个答案 now.utcoffset().total_seconds() == 0 对于 GMT 来说将为 True,这可能不是您想要的。

%Z 说明符记录在此处:
https://docs.python.org/3/库/datetime.html#strftime-and-strptime-behavior

You can do it simply like this:

from datetime import datetime, timezone
lunch_time = datetime.now(timezone.utc)

if lunch_time.format('%Z') == 'UTC':
     print("Eat food")

This will also work with a naive time object because lunch_time.format('%Z') will return an empty string. This method will also work with pytz or any other module because you are simply checking the timezone as string not as an object (the accepted answer won't work with the above timezone module case, only pytz).

from datetime import datetime
import pytz
dinner_time = datetime.now(pytz.timezone('UTC'))

if dinner_time.format('%Z') == 'UTC':
     print("Hungry!")

Note: This will also eliminate the possibility of the timezone being GMT timezone rather than UTC timezone. The other answer now.utcoffset().total_seconds() == 0 will be True for GMT which may not be what you want.

The %Z specifier is documented here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

萤火眠眠 2024-12-01 06:35:48

与之前的答案类似,您可以这样做:

如果您有给定的日期时间对象,您想首先使用 strftime() 方法将其转换为字符串,然后传入 %Z 说明符,该说明符给出返回的时区名称根据文档,“(空)、UTC、GMT”之一。

所以一个例子是:


def check_if_utc(my_datetime):
   return my_datetime.strftime('%Z') == 'UTC'
    

Similar to a previous answer, you can do it like this:

If you have a given datetime object, you want to first convert it to a string with the strftime() method, then pass in the %Z specifier which gives the timezone name returning one of '(empty), UTC, GMT' according to the documentation.

so an example is:


def check_if_utc(my_datetime):
   return my_datetime.strftime('%Z') == 'UTC'
    
娇女薄笑 2024-12-01 06:35:48
timezone.get_default_timezone()
time = timezone.make_aware(time, default_timezone)

上面的代码获取默认时区,主要是 UTC。
您可以使用上面的代码来了解日期时间时区。

如果使用 python 3.8 : time.tzinfo == pytz.utc

如果使用 python 3.9 > : time.tzinfo == datetime.timezone.utc

注意: pytz.utc != datetime.timezone.utc 所以请使用 type(date) 检查您的日期类型。

timezone.get_default_timezone()
time = timezone.make_aware(time, default_timezone)

The above code gets you the default timezone, which is mostly UTC.
You can make the datetime timezone aware using the above code.

If using python 3.8 : time.tzinfo == pytz.utc

If using python 3.9 > : time.tzinfo == datetime.timezone.utc

Note : pytz.utc != datetime.timezone.utc so do check your the type of your date, using type(date).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文