About DateTimeProperty's timezone convert

4,257 views
Skip to first unread message

Ben Chang

unread,
Jun 10, 2008, 11:13:58 PM6/10/08
to Google App Engine
I'm newbie.

I have a question about DateTimeProperty. I saw "Date-time values are
stored as and returned using the UTC time zone." in below link:

http://code.google.com/appengine/docs/datastore/typesandpropertyclasses.html#DateTimeProperty

Then I saw another sample to convert timezone by "astimezone(tz)":

http://code.google.com/appengine/docs/datastore/typesandpropertyclasses.html#datetime

The only difference to me is I use pytz to create tzinfo, below is my
source code:


......
import pytz
from pytz import timezone
......
taipei_tz = timezone('Asia/Taipei')
......
......
for greeting in greetings:
......
#greeting.date.replace(tzinfo=taipei_tz)
greeting.date.astimezone(taipei_tz)
self.response.out.write("<td>")
self.response.out.write(greeting.date.tzname())
self.response.out.write("</td>")
......


But I get these error message:

Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext
\webapp\__init__.py", line 499, in __call__
handler.get(*groups)
File "C:\GoogleAppEngineWorkspace\helloworld\05_helloworld_db.py",
line 51, in get
greeting.date.astimezone(taipei_tz)
ValueError: astimezone() cannot be applied to a naive datetime


So, the date field got from datastore is a naive datetime? then I
can't use "astimezone()" on that?

Bkad

unread,
Jul 3, 2008, 2:21:40 PM7/3/08
to Google App Engine
A datetime object is naive when it has no tzinfo object, so try

greeting.date.replace(tzinfo=pytz.utc).astimezone(taipei_tz).

On Jun 10, 8:13 pm, Ben Chang <Ben.RB.Ch...@gmail.com> wrote:
> I'm newbie.
>
> I have a question about DateTimeProperty. I saw "Date-time values are
> stored as and returned using the UTC time zone." in below link:
>
> http://code.google.com/appengine/docs/datastore/typesandpropertyclass...
>
> Then I saw another sample to convert timezone by "astimezone(tz)":
>
> http://code.google.com/appengine/docs/datastore/typesandpropertyclass...

danielpops

unread,
Jul 13, 2008, 3:51:53 AM7/13/08
to Google App Engine
Wow good call Bkad...

I was not familiar with the .replace( ) method, and ended up driving
myself nuts trying to figure out this same situation, not being able
to find a quick and easy solution. I thought i had to get all crazy
and pythonic as such:

>>> import pytz
>>> import datetime
>>> utc_string = '2008-07-11 06:30:00.000' # datetime format used in our database, UTC time zone
>>> arglist = utc_string.split( )[ 0 ].split( '-' ) # luckily, the format is perfect
>>> arglist.extend( utc_string.split( )[ 1 ].split( ':' ) ) # for the datetime.datetime constructor
>>> arglist
['2008', '07', '11', '06', '30', '00']
>>> arglist.append( 0 )
>>> arglist = [ int( i ) for i in arglist ] # datetime.datetime constructor needs ints
>>> arglist.append( pytz.utc ) # need this to make it a non-naive datetime object
>>> nonNaiveUTCtimeStamp = datetime.datetime( *arglist ) # cleverly pass the argument list
>>> print nonNaiveUTCtimeStamp
2008-07-11 06:30:00+00:00
>>> localTime = nonNaiveUTCtimeStamp.astimezone( pytz.timezone( ‘US/Pacific’ ) )
>>> print localTime
2008-07-10 23:30:00-07:00


obviously, strptime to parse the string and then a .replace is much
better.... shame on me for not figuring that out sooner :)
Reply all
Reply to author
Forward
0 new messages