Why, when calling the put () method, one cannot turn on automatic conversion of date and time to UTC,

Here is what I'm trying to do: the user sends the time in the Pacific, after submitting, I use .replace to set the time zone for Pacific.

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

As soon as I installed tzinfo, I bet. According to the python documentation for google appengine, it says:

"If the datetime value has the tzinfo attribute, it will be converted to the UTC time zone for storage. The values ​​are returned from the UTC data store, with tzinfo from None. An application that needs date and time values ​​in a specific time zone must correctly set tzinfo when updating values ​​and convert the values ​​to the time zone when accessing the value. "

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

WARNING 2012-10-06 21:10: 14,579 tasklets.pyhaps99] initial generator _put_tasklet (context.py:264) raised by NotImplementedError (DatetimeProperty date can only support UTC. Get a new property to support alternative time zones.) WARNING 2012 -10-06 21:10: 14,579 tasklets.pyhaps99] paused put generator (context.py:703) raised by NotImplementedError (DatetimeProperty date can only support UTC. Get a new property to support alternative time zones.)

Please note that I am using NDB

So, after that, I suggested that perhaps NDB will not automatically convert it to UTC. So 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 peacetime to UTC and set the name tzinfo to "UTC".

Could really use a ton of help here ... thanks!

+6
source share
3 answers

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

 timestamp = timestamp.replace(tzinfo=None) 
+13
source

Here is my working example:

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

I came across this using http://hnrss.org/ . That was my decision.

  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 data store.

0
source

Source: https://habr.com/ru/post/927115/


All Articles