检查日期是否为UTC格式 [英] Checking if date is in UTC format
本文介绍了检查日期是否为UTC格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用pytz模块通过以下代码将America / Los_Angeles时区中的日期转换为utc:
TZ = 'America / Los_Angeles'
$现在,我想测试utc值是否实际上是UTC格式。如何使用pytz或datetime做到这一点?
from = pytz.timezone(TZ)
utc = from.localize(original_date).astimezone(pytz.utc)
请帮助
谢谢解决方案
utc.tzinfo == pytz.utc#如果UTC为UTC
$,则返回True b
$ b示例:
now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc#返回True
now = now.astimezone(pytz.timezone('America / Los_Angeles'))
now.tzinfo == pytz.utc #返回False
Im using the pytz module to translate a date in America/Los_Angeles timezone to utc by 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 utc value is actually in UTC format or not. How to do that with pytz or 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
这篇关于检查日期是否为UTC格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文