1
據Convert UTC to local time to UTC in Python and Google App Engine,正確的,現在的時間和目標時間比較(考慮不同的時區),我要在目標時間轉換爲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():
...
問題:
- 這是正確的嗎?
有一個錯誤信息:
TypeError: can't compare offset-naive and offset-aware datetimes
如何解決呢?
感謝您的聯繫。 –