Ticket #17343: tz.diff

File tz.diff, 2.5 KB (added by Tomek Paczkowski, 13 years ago)

Consolidated diff

  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 9620d1c..e01e979 100644
    a b  
    22
    33import sys
    44import re
    5 from datetime import datetime
     5from django.utils import timezone
    66from itertools import groupby, cycle as itertools_cycle
    77
    88from django.conf import settings
    class NowNode(Node):  
    343343        self.format_string = format_string
    344344
    345345    def render(self, context):
    346         return date(datetime.now(), self.format_string)
     346        return date(timezone.localtime(timezone.now()), self.format_string)
    347347
    348348class SpacelessNode(Node):
    349349    def __init__(self, nodelist):
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index e274358..e49d06d 100644
    a b class Templates(unittest.TestCase):  
    504504        self.assertEqual(failures, [], "Tests failed:\n%s\n%s" %
    505505            ('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
    506506
     507    @override_settings(USE_TZ=True, TIME_ZONE='America/Chicago')
     508    def test_now_uses_time_zones(self):
     509        from django.utils import timezone
     510        with timezone.override('Europe/Warsaw'):
     511            template_time = Template('{% now "H:i:s" %}').render(Context())
     512            code_time = timezone.localtime(timezone.now()).strftime('%H:%M:%S')
     513            self.assertEqual(template_time, code_time)
     514
     515    @override_settings(USE_TZ=True, TIME_ZONE='America/Chicago')
     516    def test_now_uses_time_zones_and_honors_timezone_tag(self):
     517        from django.utils import timezone
     518        with timezone.override('Europe/Warsaw'):
     519            template_time = Template("""{% load tz %}{% timezone "UTC" %}{% now "H:i:s" %}{% endtimezone %}""").render(Context())
     520            code_time = timezone.now().strftime('%H:%M:%S')
     521            self.assertEqual(template_time, code_time)
     522
     523    @override_settings(USE_TZ=True, TIME_ZONE='America/Chicago')
     524    def test_now_uses_time_zones_and_honors_localtime_tag(self):
     525        from django.utils import timezone
     526        template_time = Template('{% load tz %}{% localtime off %}{% now "H:i:s" %}{% endlocaltime %}').render(Context())
     527        code_time = timezone.now().astimezone(timezone.get_default_timezone()).strftime('%H:%M:%S')
     528        self.assertEqual(template_time, code_time)
     529
    507530    def render(self, test_template, vals):
    508531        context = template.Context(vals[1])
    509532        before_stack_size = len(context.dicts)
Back to Top