2012-10-06 2 views
6

これは私がやろうとしていることです:ユーザーは太平洋で時間を送信します。一度送信すると.replaceを使用して太平洋にタイムゾーンを設定します。putengine()を呼び出すときにdatetimeを自動変換しないのはなぜですか?

Pacific = time.USTimeZone(-8, "Pacific", "PST", "PDT") 
addEvent.date = addEvent.date.replace(tzinfo=Pacific) 

私がtzinfoを設定したら、putを実行しています。 GoogleのAppEngineののPythonドキュメントによると、それは言う:私はPUTを()を実行するとき

"If the datetime value has a tzinfo attribute, it will be converted to the UTC time zone for storage. Values come back from the datastore as UTC, with a tzinfo of None. An application that needs date and time values to be in a particular time zone must set tzinfo correctly when updating the value, and convert values to the timezone when accessing the value."

はしかし、私は次のエラーを取得する:

WARNING 2012-10-06 21:10:14,579 tasklets.py:399] initial generator _put_tasklet(context.py:264) raised NotImplementedError(DatetimeProperty date can only support UTC. Please derive a new Property to support alternative timezones.) WARNING 2012-10-06 21:10:14,579 tasklets.py:399] suspended generator put(context.py:703) raised NotImplementedError(DatetimeProperty date can only support UTC. Please derive a new Property to support alternative timezones.)

私はNDB

を使用していますのでご注意ください

これを実行した後、NDBがUTCに自動的に変換しない可能性があると想定しました。

class UTC(tzinfo): 
    def utcoffset(self, dt): 
    return timedelta(0) 
    def tzname(self, dt): 
    return str("UTC") 
    def dst(self, dt): 
    return timedelta(0) 

、今、私はまだ私はUTCに太平洋標準時を変換した後も同じエラーを取得し、「UTC」としてtzinfoの名を設定します。それでは、私は次のコードを使用してUTCに変換しようとしました。

本当にここで多額の助けを借りることができました... ありがとう!

答えて

13

解決策は、UTCに変換後の時刻から完全にtzinfoを削除することです。

timestamp = timestamp.replace(tzinfo=None) 
+0

ああそう簡単!ありがとう! – iceanfire

+3

実際には一例が本当に役に立ちました。 – Houman

+0

例を次に示します。プロパティをタイムスタンプに設定する前に、timestamp = timestamp.replace(tzinfo = None)を実行します。 – Luca

0

ここに私の作業例:

if my_date.utcoffset(): 
    my_date = (my_date - my_date.utcoffset()).replace(tzinfo=None) 
+0

if文を:my_date.utcoffset()がNoneでない場合: – MrJ

0

私はhttp://hnrss.org/を使用して、このに走りました。これが私の解決策でした。

import datetime 
    from dateutil import parser 

    your_date_string = 'Mon, 24 Oct 2016 16:49:47 +0000' 

    your_date = parser.parse('your_date_string') 
    # your_date is now a datetime.datetime object 

    your_date_minus_tz = your_date.replace(tzinfo=None) 

今(あなたのプットをしてみてください)、あなたはクラウドデータストアに2016年10月24日16時49分47秒が表示されるはずです。

関連する問題