检查日期是否为UTC格式 [英] Checking if date is in UTC format

查看:112
本文介绍了检查日期是否为UTC格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pytz模块通过以下代码将America / Los_Angeles时区中的日期转换为utc:

  TZ = 'America / Los_Angeles'
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屋!

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