Google app engine 为什么';t appengine在调用put()时自动将日期时间转换为UTC

Google app engine 为什么';t appengine在调用put()时自动将日期时间转换为UTC,google-app-engine,app-engine-ndb,Google App Engine,App Engine Ndb,我想做的是:用户提交一个太平洋时间,一旦提交,我就使用.replace将时区设置为太平洋 Pacific = time.USTimeZone(-8, "Pacific", "PST", "PDT") addEvent.date = addEvent.date.replace(tzinfo=Pacific) 一旦我设置了tzinfo,我就要进行put。根据google appengine的python文档,它说: “如果datetime值具有tzinfo属性,它将转换为UTC时区进行存储。值作

我想做的是:用户提交一个太平洋时间,一旦提交,我就使用.replace将时区设置为太平洋

Pacific = time.USTimeZone(-8, "Pacific",  "PST", "PDT")
addEvent.date = addEvent.date.replace(tzinfo=Pacific)
一旦我设置了tzinfo,我就要进行put。根据google appengine的python文档,它说:

“如果datetime值具有tzinfo属性,它将转换为UTC时区进行存储。值作为UTC从数据存储返回,tzinfo为None。需要日期和时间值位于特定时区的应用程序必须在更新值时正确设置tzinfo,并在访问值时将值转换为时区。”

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

警告2012-10-06 21:10:14579 tasklets.py:399]初始生成器\u put\u tasklet(context.py:264)引发了NotImplementedError(DatetimeProperty date只能支持UTC。请派生一个新属性以支持其他时区。) 警告2012-10-06 21:10:14579 tasklets.py:399]挂起的生成器put(context.py:703)引发了NotImplementedError(DatetimeProperty date只能支持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”,仍然会出现相同的错误

这里真的需要很多帮助。。。
谢谢!

解决方案是将
tzinfo
从转换为UTC后的时间中完全删除

timestamp = timestamp.replace(tzinfo=None)

下面是我的工作示例:

if my_date.utcoffset():
    my_date = (my_date - my_date.utcoffset()).replace(tzinfo=None)
我用了这个,这是我的解决方案

    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)

现在试试你的put(),你会在云数据存储中看到2016-10-24 16:49:47。

实际上,一个例子会非常有用。下面是一个例子:do timestamp=timestamp.replace(tzinfo=None)在将属性设置为timestamp之前。这不是简单地删除时区信息吗?AFAIK ndb希望datetime为UTC格式,这只是将带有时区的本地datetime转换为不带时区的本地datetime,不是吗?(因此,它不会将2000-01-01T04:00:00.000000-04:00转换为2000-01-01T00:00:00.000000,而是将其转换为2000-01-01T04:00:00.000000)@Hans它确实删除了时区,但这正是NDB所期望的。NDB层存储时区原始日期时间,并依赖应用程序对从一个时区到下一个时区的转换进行排序。可能会将if语句更改为:if my_date.utcoffset()不是None: