为什么appengine在调用put()时将datetime自动转换为UTC, [英] Why doesn't appengine auto-convert datetime to UTC when calling put()

查看:94
本文介绍了为什么appengine在调用put()时将datetime自动转换为UTC,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想要做的事情:用户提交太平洋时间,一旦提交,我使用.replace将时区设置为太平洋。

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

一旦设置了tzinfo,我在做一个放。根据谷歌appengine的python文档,它说:


如果datetime值具有tzinfo属性,它将被转换为UTC数据存储的时区为UTC,tzinfo为None。需要日期和时间值的应用程序在特定时区中时,必须在更新值时正确设置tzinfo,并将值转换为时区


但是,当我执行put()时,出现以下错误:


警告2012-10-06 21:10:14,579 tasklets.py:399]初始生成器_put_tasklet(context.py:264)引发NotImplementedError(DatetimeProperty date can )
警告2012-10-06 21:10:14,579 tasklets.py:399]暂停生成器put(context.py:703)引发NotImplementedError(DatetimeProperty日期只能支持UTC )


请注意我使用的是NDB



好的,所以在做完之后,我假定NDB可能不会自动将其转换为UTC。所以然后我尝试使用以下代码将其转换为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并将tzinfo名称设置为UTC。

真的可以在这里使用大量的帮助...
谢谢!

解决方案



<$> 解决方案是从转换为UTC后的时间内完全删除 tzinfo 。 p $ p> timestamp = timestamp.replace(tzinfo =无)


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)

Once i've set the tzinfo, I'm doing a put. According to the python documentation of google appengine it says:

"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."

However, when I do a put(), i get the following error:

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.)

Please note I am using NDB

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)

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!

解决方案

The solution is to remove the tzinfo completely from the time after converting to UTC.

timestamp = timestamp.replace(tzinfo=None)

这篇关于为什么appengine在调用put()时将datetime自动转换为UTC,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆