简体   繁体   English

检查日期是否为 UTC 格式

[英]Checking if date is in UTC format

Im using the pytz module to translate a date in America/Los_Angeles timezone to utc by the code below:我使用 pytz 模块通过以下代码将 America/Los_Angeles 时区中的日期转换为 utc:

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

Now,i want to test if utc value is actually in UTC format or not.现在,我想测试 utc 值是否实际上是 UTC 格式。 How to do that with pytz or datetime?如何用 pytz 或 datetime 做到这一点?

Please Help Thank You请帮忙谢谢

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

The accepted answer will not work for anything else as pytz objects.接受的答案不适用于其他任何pytz对象。 As pytz is actually pretty bad at doing conversions[1] (eg properly doing daylight savings etc) it is probably better to do a cross-implementation check.由于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 [1] https://pendulum.eustace.io/blog/a-faster-alternative-to-pyz.html

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.这也适用于原始时间 object,因为lunch_time.format('%Z')将返回一个空字符串。 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).此方法也适用于 pytz 或任何其他模块,因为您只是将时区检查为字符串而不是 object(接受的答案不适用于上述时区模块案例,仅适用于 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.注意:这也将消除时区是 GMT 时区而不是 UTC 时区的可能性。 The other answer now.utcoffset().total_seconds() == 0 will be True for GMT which may not be what you want.另一个答案now.utcoffset().total_seconds() == 0对于格林威治标准时间将为真,这可能不是您想要的。

The %Z specifier is documented here: %Z 说明符记录在此处:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

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.如果您有给定的日期时间 object,您希望首先使用 strftime() 方法将其转换为字符串,然后传入 %Z 说明符,该说明符给出返回“(空)、UTC、GMT 之一的时区名称”文档。

so an example is:所以一个例子是:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM