﻿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
29580	Now() database function doesn't respect TIME_ZONE with USE_TZ	lobziik	nobody	"Hi guys!
Seems that PostgreSQL database wrapper doesn't set the timezone on connection initialization.

My database settings:
{{{
{
  ""NAME"": null,
  ""USER"": ""lobziik"",
  ""PASSWORD"": """",
  ""HOST"": ""127.0.0.1"",
  ""PORT"": 5432,
  ""ENGINE"": ""django.db.backends.postgresql"",
  ""ATOMIC_REQUESTS"": false,
  ""AUTOCOMMIT"": true,
  ""CONN_MAX_AGE"": 0,
  ""OPTIONS"": {
    
  },
  ""TIME_ZONE"": null,
  ""TEST"": {
    ""CHARSET"": null,
    ""COLLATION"": null,
    ""NAME"": ""test_django_test"",
    ""MIRROR"": null
  }
}
}}}

psql behaviour on this database
{{{
$ psql -d test_django_test

psql (10.4)
Type ""help"" for help.

test_django_test=# select * from statement_timestamp();
              now
-------------------------------
 2018-07-20 01:37:54.903856+02
(1 row)
}}}

system date:
{{{
$ date
Fri Jul 20 01:38:44 CEST 2018
}}}

Trying to annotate some qs with built-in Django Now function:

{{{

In [8]: User.objects.all().annotate(now=Now()).last().now
Out[8]: datetime.datetime(2018, 7, 19, 23, 42, 51, 125068, tzinfo=<UTC>)

In [15]: settings.TIME_ZONE
Out[15]: 'Europe/Prague'

In [13]: settings.USE_TZ
Out[13]: True
}}}

I found strange behaviour in {{{django.db.backends.base.base.BaseDatabaseWrapper#timezone_name}}}, this prop is used in {{{django.db.backends.postgresql.base.DatabaseWrapper#ensure_timezone}}} for checking, if we should set timezone for connection or not.
But with USE_TZ = True, it always will be UTC in postgres case.
{{{
    @cached_property
    def timezone_name(self):
        """"""
        Name of the time zone of the database connection.
        """"""
        if not settings.USE_TZ:
            return settings.TIME_ZONE
        elif self.settings_dict['TIME_ZONE'] is None:
            return 'UTC'
        else:
            return self.settings_dict['TIME_ZONE']
}}}

Also it impossible to pass TIME_ZONE manually for postgres database. See {{{django.db.backends.base.base.BaseDatabaseWrapper#check_settings}}}


"	Bug	closed	Database layer (models, ORM)	2.0	Normal	invalid	postgres, postgresql, time zones, USE_TZ		Unreviewed	0	0	0	0	0	0
