Why doesn't appengine auto-convert datetime to UTC when calling put()

眉间皱痕 提交于 2019-12-06 02:54:07

问题


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!


回答1:


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

timestamp = timestamp.replace(tzinfo=None)



回答2:


Here my working example:

if my_date.utcoffset():
    my_date = (my_date - my_date.utcoffset()).replace(tzinfo=None)



回答3:


I ran into this using http://hnrss.org/. This was my solution.

    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)

Now try your put() and you should see 2016-10-24 16:49:47 in the Cloud datastore.



来源:https://stackoverflow.com/questions/12763938/why-doesnt-appengine-auto-convert-datetime-to-utc-when-calling-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!