google-app-engine - 为什么 Appengine 在调用 put() 时不自动将日期时间转换为 UTC

标签 google-app-engine app-engine-ndb

这是我正在尝试做的事情:用户提交太平洋时间,提交后我使用 .replace 将时区设置为太平洋时间。

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

一旦我设置了 tzinfo,我就会做一个看跌期权。根据 google appengine 的 python 文档,它说:

"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() 时,出现以下错误:

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:

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

timestamp = timestamp.replace(tzinfo=None)

关于google-app-engine - 为什么 Appengine 在调用 put() 时不自动将日期时间转换为 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763938/

相关文章:

google-app-engine - 部署在 Google App Engine 上的应用程序的/healthz 路由返回 404

python - 在 Google App Engine 上移植基本脚本

google-app-engine - Appengine NDB : Putting 880 rows, 超出数据存储区写入操作配额。为什么?

google-app-engine - 返回零结果的非根实体组查询

java - JDO 持久字段返回 null

postgresql - Odoo 与 Google App Engine NDB |从 postgresql 移植到 NDB

Python WTForms : How to dynamically set options of a SelectField in FieldList of FormFields?

python - Google App Engine - NDB - 在多条记录上设置属性值

python - 为什么 required 和 default 在ndb中是互斥的?

google-app-engine - 无法编制索引的数据存储 BLOB