2012-12-22 111 views
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(): 
    ... 

問題:

  1. 這是正確的嗎?
  2. 有一個錯誤信息:

    TypeError: can't compare offset-naive and offset-aware datetimes

    如何解決呢?

回答

1

它的因爲你的datetime對象之一有一個時區設置,另一個沒有。
看一看herehere

+0

感謝您的聯繫。 –