Alex Rivera | Logout

attribute 'tzinfo' of 'datetime.datetime' objects is not writable

Asked 2010-01-18T21:41:38.947
40

How do I set the timezone of a datetime instance that just came out of the datastore?

When it first comes out it is in UTC. I want to change it to EST.

I'm trying, for example:

class Book( db.Model ):
    creationTime = db.DateTimeProperty()

When a Book is retrieved, I want to set its tzinfo immediately:

book.creationTime.tzinfo = EST

Where I use this example for my EST object

However I get:

attribute 'tzinfo' of 'datetime.datetime' objects is not writable

I've seen a number of answers that recommend pytz and python-dateutil, but I really want an answer to this question.

Edit
Report

1 Answer

8

If you're receiving a datetime that's in EST, but doesn't have its tzinfo field set, use dt.replace(tzinfo=tz) to assign a tzinfo without modifying the time. (Your database should be doing this for you.)

If you're receiving a datetime that's in UDT, and you want it in EST, then you need astimezone. http://docs.python.org/library/datetime.html#datetime.datetime.astimezone

In the vast majority of cases, your database should be storing and returning data in UDT, and you shouldn't need to use replace (except possibly to assign a UDT tzinfo).

answered 2010-01-18T21:50:33.840

Your Answer