This is just a quick hack for anyone else bit by this: tzinfo is never
set when reading DateTimeFields out of the database, at least with
Postgresql.

Now, I have no idea why anyone would want to set TIME_ZONE to anything
but UTC (and the default of an arbitrary US timezone is rather
bizarre), but even with UTC, you need a tzinfo set on your datetimes,
in order to localize them to user time zones with astimezone().  Doing
this manually is a pain--you need to replace(tzinfo=UTC) before every
datetime access.

This hack patches psycopg2's _cursor to attach a timezone_factory,
giving the expected results.  Drop it in and call it from your app on
initialization, and DateTimeFields will be set appropriately.  I
strongly recommend setting TIME_ZONE="UTC" and leaving it there, too,
but this should work for other timezones, too.

The usual caveats for monkey patching apply, of course.  I'd expect
everyone who uses Postgresql and localizes times for users to hit this
problem, so I'm a little surprised at having to do this.  I don't have
time to put together a real patch for this right now, but hopefully
this gets fixed eventually.

I'm not sure if DateTimeField needs tzinfo support, too.  As long as
TIME_ZONE and the tzinfo of times passed to queries are the same it
should work fine, which is all I need.

-- 
Glenn Maynard

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Attachment: fix_datetime_timezone.py
Description: Binary data

Reply via email to