diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
index af8fad3..5511464 100644
a
|
b
|
def display_for_field(value, field):
|
295 | 295 | elif value is None: |
296 | 296 | return EMPTY_CHANGELIST_VALUE |
297 | 297 | elif isinstance(field, models.DateTimeField): |
298 | | return formats.localize(timezone.aslocaltime(value)) |
| 298 | return formats.localize(timezone.localtime(value)) |
299 | 299 | elif isinstance(field, models.DateField) or isinstance(field, models.TimeField): |
300 | 300 | return formats.localize(value) |
301 | 301 | elif isinstance(field, models.DecimalField): |
diff --git a/django/template/base.py b/django/template/base.py
index 572ec61..40a577d 100644
a
|
b
|
from django.utils.safestring import (SafeData, EscapeData, mark_safe,
|
18 | 18 | from django.utils.formats import localize |
19 | 19 | from django.utils.html import escape |
20 | 20 | from django.utils.module_loading import module_has_submodule |
21 | | from django.utils.timezone import aslocaltime |
| 21 | from django.utils.timezone import localtime |
22 | 22 | |
23 | 23 | |
24 | 24 | TOKEN_TEXT = 0 |
… |
… |
class FilterExpression(object):
|
595 | 595 | else: |
596 | 596 | arg_vals.append(arg.resolve(context)) |
597 | 597 | if getattr(func, 'expects_localtime', False): |
598 | | obj = aslocaltime(obj, context.use_tz) |
| 598 | obj = localtime(obj, context.use_tz) |
599 | 599 | if getattr(func, 'needs_autoescape', False): |
600 | 600 | new_obj = func(obj, autoescape=context.autoescape, *arg_vals) |
601 | 601 | else: |
… |
… |
def _render_value_in_context(value, context):
|
856 | 856 | means escaping, if required, and conversion to a unicode object. If value |
857 | 857 | is a string, it is expected to have already been translated. |
858 | 858 | """ |
859 | | value = aslocaltime(value, use_tz=context.use_tz) |
| 859 | value = localtime(value, use_tz=context.use_tz) |
860 | 860 | value = localize(value, use_l10n=context.use_l10n) |
861 | 861 | value = force_unicode(value) |
862 | 862 | if ((context.autoescape and not isinstance(value, SafeData)) or |
diff --git a/django/template/debug.py b/django/template/debug.py
index d4b973b..74aa82b 100644
a
|
b
|
from django.utils.encoding import force_unicode
|
3 | 3 | from django.utils.html import escape |
4 | 4 | from django.utils.safestring import SafeData, EscapeData |
5 | 5 | from django.utils.formats import localize |
6 | | from django.utils.timezone import aslocaltime |
| 6 | from django.utils.timezone import localtime |
7 | 7 | |
8 | 8 | |
9 | 9 | class DebugLexer(Lexer): |
… |
… |
class DebugVariableNode(VariableNode):
|
82 | 82 | def render(self, context): |
83 | 83 | try: |
84 | 84 | output = self.filter_expression.resolve(context) |
85 | | output = aslocaltime(output, use_tz=context.use_tz) |
| 85 | output = localtime(output, use_tz=context.use_tz) |
86 | 86 | output = localize(output, use_l10n=context.use_l10n) |
87 | 87 | output = force_unicode(output) |
88 | 88 | except UnicodeDecodeError: |
diff --git a/django/templatetags/tz.py b/django/templatetags/tz.py
index 14b613e..48c511f 100644
a
|
b
|
from django.utils import timezone
|
13 | 13 | |
14 | 14 | register = Library() |
15 | 15 | |
| 16 | |
16 | 17 | # HACK: datetime is an old-style class, create a new-style equivalent |
17 | 18 | # so we can define additional attributes. |
18 | 19 | class datetimeobject(datetime, object): |
… |
… |
class datetimeobject(datetime, object):
|
22 | 23 | # Template filters |
23 | 24 | |
24 | 25 | @register.filter |
25 | | def aslocaltime(value): |
| 26 | def localtime(value): |
26 | 27 | """ |
27 | 28 | Converts a datetime to local time in the active time zone. |
28 | 29 | |
29 | 30 | This only makes sense within a {% localtime off %} block. |
30 | 31 | """ |
31 | | return astimezone(value, timezone.get_current_timezone()) |
| 32 | return do_timezone(value, timezone.get_current_timezone()) |
| 33 | |
32 | 34 | |
33 | 35 | @register.filter |
34 | | def asutc(value): |
| 36 | def utc(value): |
35 | 37 | """ |
36 | 38 | Converts a datetime to UTC. |
37 | 39 | """ |
38 | | return astimezone(value, timezone.utc) |
| 40 | return do_timezone(value, timezone.utc) |
39 | 41 | |
40 | | @register.filter |
41 | | def astimezone(value, arg): |
| 42 | |
| 43 | @register.filter('timezone') |
| 44 | def do_timezone(value, arg): |
42 | 45 | """ |
43 | 46 | Converts a datetime to local time in a given time zone. |
44 | 47 | |
… |
… |
class LocalTimeNode(Node):
|
103 | 106 | context.use_tz = old_setting |
104 | 107 | return output |
105 | 108 | |
| 109 | |
106 | 110 | class TimezoneNode(Node): |
107 | 111 | """ |
108 | 112 | Template node class used by ``timezone_tag``. |
… |
… |
class TimezoneNode(Node):
|
116 | 120 | output = self.nodelist.render(context) |
117 | 121 | return output |
118 | 122 | |
| 123 | |
119 | 124 | class GetCurrentTimezoneNode(Node): |
120 | 125 | """ |
121 | 126 | Template node class used by ``get_current_timezone_tag``. |
… |
… |
class GetCurrentTimezoneNode(Node):
|
127 | 132 | context[self.variable] = timezone.get_current_timezone_name() |
128 | 133 | return '' |
129 | 134 | |
| 135 | |
130 | 136 | @register.tag('localtime') |
131 | 137 | def localtime_tag(parser, token): |
132 | 138 | """ |
… |
… |
def localtime_tag(parser, token):
|
142 | 148 | if len(bits) == 1: |
143 | 149 | use_tz = True |
144 | 150 | elif len(bits) > 2 or bits[1] not in ('on', 'off'): |
145 | | raise TemplateSyntaxError("%r argument should be 'on' or 'off'" % bits[0]) |
| 151 | raise TemplateSyntaxError("%r argument should be 'on' or 'off'" % |
| 152 | bits[0]) |
146 | 153 | else: |
147 | 154 | use_tz = bits[1] == 'on' |
148 | 155 | nodelist = parser.parse(('endlocaltime',)) |
149 | 156 | parser.delete_first_token() |
150 | 157 | return LocalTimeNode(nodelist, use_tz) |
151 | 158 | |
| 159 | |
152 | 160 | @register.tag('timezone') |
153 | 161 | def timezone_tag(parser, token): |
154 | 162 | """ |
… |
… |
def timezone_tag(parser, token):
|
167 | 175 | """ |
168 | 176 | bits = token.split_contents() |
169 | 177 | if len(bits) != 2: |
170 | | raise TemplateSyntaxError("'%s' takes one argument (timezone)" % bits[0]) |
| 178 | raise TemplateSyntaxError("'%s' takes one argument (timezone)" % |
| 179 | bits[0]) |
171 | 180 | tz = parser.compile_filter(bits[1]) |
172 | 181 | nodelist = parser.parse(('endtimezone',)) |
173 | 182 | parser.delete_first_token() |
174 | 183 | return TimezoneNode(nodelist, tz) |
175 | 184 | |
| 185 | |
176 | 186 | @register.tag("get_current_timezone") |
177 | 187 | def get_current_timezone_tag(parser, token): |
178 | 188 | """ |
… |
… |
def get_current_timezone_tag(parser, token):
|
187 | 197 | """ |
188 | 198 | args = token.contents.split() |
189 | 199 | if len(args) != 3 or args[1] != 'as': |
190 | | raise TemplateSyntaxError("'get_current_timezone' requires 'as variable' (got %r)" % args) |
| 200 | raise TemplateSyntaxError("'get_current_timezone' requires " |
| 201 | "'as variable' (got %r)" % args) |
191 | 202 | return GetCurrentTimezoneNode(args[2]) |
diff --git a/django/utils/timezone.py b/django/utils/timezone.py
index 22860eb..9e78134 100644
a
|
b
|
from django.conf import settings
|
17 | 17 | __all__ = [ |
18 | 18 | 'utc', 'get_default_timezone', 'get_current_timezone', |
19 | 19 | 'activate', 'deactivate', 'override', |
20 | | 'aslocaltime', 'isnaive', |
| 20 | 'localtime', 'isnaive', |
21 | 21 | ] |
22 | 22 | |
23 | 23 | |
… |
… |
class override(object):
|
198 | 198 | |
199 | 199 | # Utilities |
200 | 200 | |
201 | | def aslocaltime(value, use_tz=None): |
| 201 | def localtime(value, use_tz=None): |
202 | 202 | """ |
203 | 203 | Checks if value is a datetime and converts it to local time if necessary. |
204 | 204 | |
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
index 0d91f16..a35d99a 100644
a
|
b
|
Otherwise, Django will use naive datetimes in local time.
|
2108 | 2108 | See also :setting:`TIME_ZONE`, :setting:`USE_I18N` and :setting:`USE_L10N`. |
2109 | 2109 | |
2110 | 2110 | .. note:: |
| 2111 | |
2111 | 2112 | The default :file:`settings.py` file created by |
2112 | 2113 | :djadmin:`django-admin.py startproject <startproject>` includes |
2113 | 2114 | ``USE_TZ = True`` for convenience. |
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 9a9adba..2c0110f 100644
a
|
b
|
For a complete discussion on the usage of the following see the
|
660 | 660 | ``None``, the :ref:`current time zone <default-current-time-zone>` is unset |
661 | 661 | on entry with :func:`deactivate()` instead. |
662 | 662 | |
663 | | .. function:: aslocaltime(value, use_tz=None) |
| 663 | .. function:: localtime(value, use_tz=None) |
664 | 664 | |
665 | 665 | This function is used by the template engine to convert datetimes to local |
666 | 666 | time where appropriate. |
diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt
index 41e8380..18d30b9 100644
a
|
b
|
These filters accept both aware and naive datetimes. For conversion purposes,
|
330 | 330 | they assume that naive datetimes are in the default time zone. They always |
331 | 331 | return aware datetimes. |
332 | 332 | |
333 | | .. templatefilter:: aslocaltime |
| 333 | .. templatefilter:: localtime |
334 | 334 | |
335 | | aslocaltime |
336 | | ~~~~~~~~~~~ |
| 335 | localtime |
| 336 | ~~~~~~~~~ |
337 | 337 | |
338 | 338 | Forces conversion of a single value to the current time zone. |
339 | 339 | |
… |
… |
For example::
|
341 | 341 | |
342 | 342 | {% load tz %} |
343 | 343 | |
344 | | {{ value|aslocaltime }} |
| 344 | {{ value|localtime }} |
345 | 345 | |
346 | | .. templatefilter:: asutc |
| 346 | .. templatefilter:: utc |
347 | 347 | |
348 | | asutc |
349 | | ~~~~~ |
| 348 | utc |
| 349 | ~~~ |
350 | 350 | |
351 | 351 | Forces conversion of a single value to UTC. |
352 | 352 | |
… |
… |
For example::
|
354 | 354 | |
355 | 355 | {% load tz %} |
356 | 356 | |
357 | | {{ value|asutc }} |
| 357 | {{ value|utc }} |
358 | 358 | |
359 | | astimezone |
360 | | ~~~~~~~~~~ |
| 359 | .. templatefilter:: timezone |
| 360 | |
| 361 | timezone |
| 362 | ~~~~~~~~ |
361 | 363 | |
362 | 364 | Forces conversion of a single value to an arbitrary timezone. |
363 | 365 | |
… |
… |
For example::
|
368 | 370 | |
369 | 371 | {% load tz %} |
370 | 372 | |
371 | | {{ value|astimezone:"Europe/Paris" }} |
| 373 | {{ value|timezone:"Europe/Paris" }} |
372 | 374 | |
373 | 375 | .. _time-zones-migration-guide: |
374 | 376 | |
diff --git a/tests/modeltests/timezones/tests.py b/tests/modeltests/timezones/tests.py
index e33d4c6..547680c 100644
a
|
b
|
class TemplateTests(BaseDateTimeTests):
|
537 | 537 | 'naive': datetime.datetime(2011, 9, 1, 13, 20, 30), |
538 | 538 | } |
539 | 539 | templates = { |
540 | | 'notag': Template("{% load tz %}{{ dt }}|{{ dt|aslocaltime }}|{{ dt|asutc }}|{{ dt|astimezone:ICT }}"), |
541 | | 'noarg': Template("{% load tz %}{% localtime %}{{ dt }}|{{ dt|aslocaltime }}|{{ dt|asutc }}|{{ dt|astimezone:ICT }}{% endlocaltime %}"), |
542 | | 'on': Template("{% load tz %}{% localtime on %}{{ dt }}|{{ dt|aslocaltime }}|{{ dt|asutc }}|{{ dt|astimezone:ICT }}{% endlocaltime %}"), |
543 | | 'off': Template("{% load tz %}{% localtime off %}{{ dt }}|{{ dt|aslocaltime }}|{{ dt|asutc }}|{{ dt|astimezone:ICT }}{% endlocaltime %}"), |
| 540 | 'notag': Template("{% load tz %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:ICT }}"), |
| 541 | 'noarg': Template("{% load tz %}{% localtime %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"), |
| 542 | 'on': Template("{% load tz %}{% localtime on %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"), |
| 543 | 'off': Template("{% load tz %}{% localtime off %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"), |
544 | 544 | } |
545 | 545 | |
546 | 546 | # Transform a list of keys in 'datetimes' to the expected template |
… |
… |
class TemplateTests(BaseDateTimeTests):
|
600 | 600 | @skipIf(pytz is None, "this test requires pytz") |
601 | 601 | def test_localtime_filters_with_pytz(self): |
602 | 602 | """ |
603 | | Test the |aslocaltime, |asutc, and |astimezone filters with pytz. |
| 603 | Test the |localtime, |utc, and |timezone filters with pytz. |
604 | 604 | """ |
605 | 605 | # Use a pytz timezone as local time |
606 | | tpl = Template("{% load tz %}{{ dt|aslocaltime }}|{{ dt|asutc }}") |
| 606 | tpl = Template("{% load tz %}{{ dt|localtime }}|{{ dt|utc }}") |
607 | 607 | ctx = Context({'dt': datetime.datetime(2011, 9, 1, 12, 20, 30)}) |
608 | 608 | |
609 | 609 | timezone._localtime = None |
… |
… |
class TemplateTests(BaseDateTimeTests):
|
612 | 612 | timezone._localtime = None |
613 | 613 | |
614 | 614 | # Use a pytz timezone as argument |
615 | | tpl = Template("{% load tz %}{{ dt|astimezone:tz }}") |
| 615 | tpl = Template("{% load tz %}{{ dt|timezone:tz }}") |
616 | 616 | ctx = Context({'dt': datetime.datetime(2011, 9, 1, 13, 20, 30), |
617 | 617 | 'tz': pytz.timezone('Europe/Paris')}) |
618 | 618 | self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") |
619 | 619 | |
620 | 620 | # Use a pytz timezone name as argument |
621 | | tpl = Template("{% load tz %}{{ dt|astimezone:'Europe/Paris' }}") |
| 621 | tpl = Template("{% load tz %}{{ dt|timezone:'Europe/Paris' }}") |
622 | 622 | ctx = Context({'dt': datetime.datetime(2011, 9, 1, 13, 20, 30), |
623 | 623 | 'tz': pytz.timezone('Europe/Paris')}) |
624 | 624 | self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") |
… |
… |
class TemplateTests(BaseDateTimeTests):
|
629 | 629 | |
630 | 630 | def test_localtime_filters_do_not_raise_exceptions(self): |
631 | 631 | """ |
632 | | Test the |aslocaltime, |asutc, and |astimezone filters on bad inputs. |
| 632 | Test the |localtime, |utc, and |timezone filters on bad inputs. |
633 | 633 | """ |
634 | | tpl = Template("{% load tz %}{{ dt }}|{{ dt|aslocaltime }}|{{ dt|asutc }}|{{ dt|astimezone:tz }}") |
| 634 | tpl = Template("{% load tz %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:tz }}") |
635 | 635 | with self.settings(USE_TZ=True): |
636 | 636 | # bad datetime value |
637 | 637 | ctx = Context({'dt': None, 'tz': ICT}) |
… |
… |
class TemplateTests(BaseDateTimeTests):
|
639 | 639 | ctx = Context({'dt': 'not a date', 'tz': ICT}) |
640 | 640 | self.assertEqual(tpl.render(ctx), "not a date|||") |
641 | 641 | # bad timezone value |
642 | | tpl = Template("{% load tz %}{{ dt|astimezone:tz }}") |
| 642 | tpl = Template("{% load tz %}{{ dt|timezone:tz }}") |
643 | 643 | ctx = Context({'dt': datetime.datetime(2011, 9, 1, 13, 20, 30), 'tz': None}) |
644 | 644 | self.assertEqual(tpl.render(ctx), "") |
645 | 645 | ctx = Context({'dt': datetime.datetime(2011, 9, 1, 13, 20, 30), 'tz': 'not a tz'}) |