<p>我去年发了<a href="http://www.letsyouandhimfight.com/2008/04/12/time-zones-in-google-app-engine" rel="nofollow noreferrer">something related to this subject on my blog</a>。基本上,当将所有时间存储在数据存储中时,它会将所有时间转换为UTC,并在读取时将UTC时区附加到这些时间。您可以随意修改它,然后将值转换为您想要的任何本地时区。在</p>
<p>这个代码示例可能已经过时了-我已经很久没有使用appengine了。但我希望它能对你有所帮助。在</p>
<pre><code>import pytz
class TzDateTimeProperty(db.DateTimeProperty):
def get_value_for_datastore(self, model_instance):
if model_instance.posted_at.tzinfo is None:
model_instance.posted_at = model_instance.posted_at.replace(tzinfo=pytz.utc)
else:
model_instance.posted_at = model_instance.posted_at.astimezone(pytz.utc)
return super(TzDateTimeProperty, self).get_value_for_datastore(model_instance)
def make_value_from_datastore(self, value):
value = super(TzDateTimeProperty, self).make_value_from_datastore(value)
if value.tzinfo is None:
value = value.replace(tzinfo=pytz.utc)
else:
value = value.astimezone(pytz.utc)
return value
</code></pre>
<p>我还推荐优秀的pytz库;它为几乎每个有用的时区提供时区对象。(我想链接到它,但是垃圾邮件预防阻止了我。只需谷歌搜索。)</p>