﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23524	Allow to set TIME_ZONE option on databases that support time zones.	Mactory	Aymeric Augustin	"When using a cursor created by  django.db.connection.cursor() to execute a database function on a postgresql database, the timezone of the database connection is always set to UTC instead of using the specified TIME_ZONE.
This behavior brakes postgres' {{{ date_trunc }}} function, since it will truncate the hour to be 0 at UTC creating an offset from midnight at the local timestamp.

Example:
settings.py
{{{
TIME_ZONE = 'Europe/Berlin' # Offset +2
USE_TZ = TRUE
}}}

using date_trunc in a djago python shell:
{{{
./manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute(""select date_trunc('month', TIMESTAMP WITH TIME ZONE '2014-09-14 04:10:00+02');"")
>>> unicode(cursor.fetchone()[0])
u'2014-09-01 00:00:00+00:00'
}}}

using date_tunc directly in psql:
{{{
psql db
db=#select date_trunc('month', TIMESTAMP WITH TIME ZONE '2014-09-14 04:10:00+02');
       date_trunc       
------------------------
 2014-09-01 00:00:00+02
(1 row)
}}}

For executing custom functions for which no return value is needed, the following workaround does work:
{{{
>>> cursor.execute(""SET TIMEZONE to %s;select date_trunc('month', TIMESTAMP WITH TIME ZONE '2014-09-14 04:10:00+02');SET TIMEZONE to 'UTC';"", (settings.TIME_ZONE,))
}}}"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
