#29580: Postgresql: db Now() doesnt respect TIME_ZONE with USE_TZ
-------------------------------------+-------------------------------------
               Reporter:  lobziik    |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.0
  layer (models, ORM)                |       Keywords:  postgres,
               Severity:  Normal     |  postgresql, time zones, USE_TZ
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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}}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29580>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.142309dd317b5665c02661f2ed0d7937%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to