如何使用 pytz 或 datetime 检查日期是否为 UTC 格式?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
接受的答案不适用于
pytz
对象之外的任何其他对象。由于 pytz 实际上在转换方面相当糟糕 [1](例如正确执行夏令时等),因此最好进行交叉实现检查。[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. Aspytz
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.[1] https://pendulum.eustace.io/blog/a-faster-alternative-to-pyz.html
例子:
Example:
您可以简单地这样做:
这也适用于简单的时间对象,因为
lunch_time.format('%Z')
将返回一个空字符串。此方法也适用于 pytz 或任何其他模块,因为您只是将时区检查为字符串而不是对象(接受的答案不适用于上述时区模块情况,仅适用于 pytz)。注意:这也将消除时区是 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:
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).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
与之前的答案类似,您可以这样做:
如果您有给定的日期时间对象,您想首先使用 strftime() 方法将其转换为字符串,然后传入 %Z 说明符,该说明符给出返回的时区名称根据文档,“(空)、UTC、GMT”之一。
所以一个例子是:
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:
上面的代码获取默认时区,主要是 UTC。
您可以使用上面的代码来了解日期时间时区。
如果使用 python 3.8 : time.tzinfo == pytz.utc
如果使用 python 3.9 > : time.tzinfo == datetime.timezone.utc
注意: pytz.utc != datetime.timezone.utc 所以请使用 type(date) 检查您的日期类型。
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).