为什么在调用 put() 时 appengine 不自动将日期时间转换为 UTC [英] Why doesn't appengine auto-convert datetime to UTC when calling put()
问题描述
这是我想要做的:用户提交太平洋时间,提交后我使用 .replace 将时区设置为太平洋.
Here's what I'm trying to do: the user submits a time in pacific, once submitted I use .replace to set the timezone to Pacific.
Pacific = time.USTimeZone(-8, "Pacific", "PST", "PDT")
addEvent.date = addEvent.date.replace(tzinfo=Pacific)
一旦我设置了 tzinfo,我就在做一个看跌期权.根据 google appengine 的 python 文档,它说:
Once i've set the tzinfo, I'm doing a put. According to the python documentation of google appengine it says:
"如果 datetime 值具有 tzinfo 属性,它将被转换为 UTC 时区进行存储.值作为 UTC 从数据存储区返回,其中 tzinfo 为 None.需要日期和时间值的应用程序在特定时区更新值时必须正确设置tzinfo,并在访问值时将值转换为时区."
"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."
但是,当我执行 put() 时,出现以下错误:
However, when I do a put(), i get the following error:
警告 2012-10-06 21:10:14,579 tasklets.py:399] 初始生成器 _put_tasklet(context.py:264) 引发 NotImplementedError(DatetimeProperty 日期只能支持 UTC.请派生一个新的属性来支持替代时区.)警告 2012-10-06 21:10:14,579 tasklets.py:399] 暂停生成器 put(context.py:703) 引发 NotImplementedError(DatetimeProperty 日期只能支持 UTC.请派生一个新的属性来支持替代时区.)>
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.然后我尝试使用以下代码将其转换为 UTC:
Ok, so after doing that I assumed that maybe NDB doesn't automatically convert it into UTC. So then I tried to convert it to UTC using the following code:
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 并将 tzinfo 名称设置为UTC",我仍然遇到相同的错误.
and now I still get the same error even after I convert the pacific time to UTC and set the tzinfo name as "UTC".
在这里真的可以用很多帮助......谢谢!
Could really use a ton of help here... thanks!
推荐答案
解决方案是将tzinfo
从时间转换为UTC后彻底删除.
The solution is to remove the tzinfo
completely from the time after converting to UTC.
timestamp = timestamp.replace(tzinfo=None)
这篇关于为什么在调用 put() 时 appengine 不自动将日期时间转换为 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!