[Django] #26711: Aware datetimes rendered through templates are changed to TIME_ZONE

9 views
Skip to first unread message

Django

unread,
Jun 5, 2016, 12:21:01 PM6/5/16
to django-...@googlegroups.com
#26711: Aware datetimes rendered through templates are changed to TIME_ZONE
---------------------------------+--------------------
Reporter: aptiko | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
Start a new project with {{{django-admin startproject}}}, then start a
shell with {{{./manage.py shell}}} and execute these commands:

{{{
from datetime import datetime
import sys

from django.template import Context, Template

import pytz

# Set adatetime to 2015-10-03 02:00 EEST (+0300)
athens = pytz.timezone('Europe/Athens')
adatetime = athens.localize(datetime(2015, 10, 3, 2, 0))

# Render that date in a template
template = Template(
'Take note of this date: {{ adatetime|date:"Y-m-d H:i e (O)" }}')
result = template.render(Context({'adatetime': adatetime}))
sys.stdout.write(result)
}}}

**Result:** `2015-10-02 23:00 UTC (+0000)`

**Expected:** `2015-10-03 02:00 EEST (+0300)`

These are actually the same time displayed in different time zones. Django
chooses to display the time in the `TIME_ZONE` setting. I thought it would
be more logical to leave an aware datetime untouched.

If this behavior is correct: How am I going to do what I want, which is to
render the aware datetime in its own time zone?

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

Django

unread,
Jun 5, 2016, 12:23:25 PM6/5/16
to django-...@googlegroups.com
#26711: Aware datetimes rendered through templates are changed to TIME_ZONE
---------------------------------+--------------------------------------

Reporter: aptiko | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by aptiko):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> Start a new project with {{{django-admin startproject}}}, then start a
> shell with {{{./manage.py shell}}} and execute these commands:
>
> {{{
> from datetime import datetime
> import sys
>
> from django.template import Context, Template
>
> import pytz
>
> # Set adatetime to 2015-10-03 02:00 EEST (+0300)
> athens = pytz.timezone('Europe/Athens')
> adatetime = athens.localize(datetime(2015, 10, 3, 2, 0))
>
> # Render that date in a template
> template = Template(
> 'Take note of this date: {{ adatetime|date:"Y-m-d H:i e (O)" }}')
> result = template.render(Context({'adatetime': adatetime}))
> sys.stdout.write(result)
> }}}
>
> **Result:** `2015-10-02 23:00 UTC (+0000)`
>
> **Expected:** `2015-10-03 02:00 EEST (+0300)`
>
> These are actually the same time displayed in different time zones.
> Django chooses to display the time in the `TIME_ZONE` setting. I thought
> it would be more logical to leave an aware datetime untouched.
>
> If this behavior is correct: How am I going to do what I want, which is
> to render the aware datetime in its own time zone?

New description:

Start a new project with {{{django-admin startproject}}}, then start a
shell with {{{./manage.py shell}}} and execute these commands:

{{{#!python


from datetime import datetime
import sys

from django.template import Context, Template

import pytz

# Set adatetime to 2015-10-03 02:00 EEST (+0300)
athens = pytz.timezone('Europe/Athens')
adatetime = athens.localize(datetime(2015, 10, 3, 2, 0))

# Render that date in a template
template = Template(
'Take note of this date: {{ adatetime|date:"Y-m-d H:i e (O)" }}')
result = template.render(Context({'adatetime': adatetime}))
sys.stdout.write(result)
}}}

**Result:** `2015-10-02 23:00 UTC (+0000)`

**Expected:** `2015-10-03 02:00 EEST (+0300)`

These are actually the same time displayed in different time zones. Django
chooses to display the time in the `TIME_ZONE` setting. I thought it would
be more logical to leave an aware datetime untouched.

If this behavior is correct: How am I going to do what I want, which is to
render the aware datetime in its own time zone?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26711#comment:1>

Django

unread,
Jun 5, 2016, 12:33:52 PM6/5/16
to django-...@googlegroups.com
#26711: Aware datetimes rendered through templates are changed to TIME_ZONE
---------------------------------+--------------------------------------

Reporter: aptiko | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by aptiko):

The behavior is the same in 1.8, 1.9, and master.

According to the
[[https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#date|documentation
for the template date filter]], the time zone name produced by the `e`
format character "[c]ould be in any format, or might return an empty
string, depending on the datetime." But with the current behavior it
doesn't seem to depend on the datetime, but only on the TIME_ZONE setting.

I investigated briefly and I think that it's not the `date` template
filter that does it; this apparently receives an already modified date. So
the change of time zone occurs earlier (but I'm not 100% certain on that).

--
Ticket URL: <https://code.djangoproject.com/ticket/26711#comment:2>

Django

unread,
Jun 5, 2016, 12:57:24 PM6/5/16
to django-...@googlegroups.com
#26711: Aware datetimes rendered through templates are changed to TIME_ZONE
---------------------------------+--------------------------------------
Reporter: aptiko | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.9
Severity: Normal | Resolution: invalid
Keywords: timezone | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by charettes):

* keywords: => timezone
* status: new => closed
* resolution: => invalid


Comment:

Aware datetimes are rendered using the current timezone
(`django.utils.timezone.get_current_timezone()`) just like strings marked
for translation are rendered using the current language
(`django.utils.language.get_language()`) by design.

If you want to prevent this conversion to the current timezone you should
use the
[https://docs.djangoproject.com/en/1.9/topics/i18n/timezones/#localtime
localtime] template tag to turn it off.

{{{#!python
{% load tz %}
{% localtime off %}


Take note of this date: {{ adatetime|date:"Y-m-d H:i e (O)" }}

{% endlocaltime %}
}}}

Please reopen the ticket if I missed something.

--
Ticket URL: <https://code.djangoproject.com/ticket/26711#comment:3>

Reply all
Reply to author
Forward
0 new messages