[英]Comparing times by considering time zones
According to Convert UTC to local time to UTC in Python and Google App Engine , to correctly compare the times of now and a target time (considering different time zones), I have to convert the target time to UTC as follows: 根据Python和Google App Engine中的将UTC转换为本地时间到UTC ,要正确比较现在时间和目标时间(考虑不同时区),我必须将目标时间转换为UTC,如下所示:
import pytz
def toUTC(date, tz):
tz = pytz.timezone('Asia/Taipei')
utc = pytz.timezone('UTC')
d_tz = tz.normalize(tz.localize(date))
d_utc = d_tz.astimezone(utc)
return d_utc
days = 10
minutes = 20
targetTime = datetime.datetime(2012,12,22,0,0,0)
targetTime = targetTime + datetime.timedelta(days=days, minutes=minutes)
targetTime = toUTC(targetTime)
if targetTime < datetime.datetime.now():
...
Questions: 问题:
There is an error message: 没有错误消息:
TypeError: can't compare offset-naive and offset-aware datetimes
How to solve it? 怎么解决呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.