without arg the active language s dateformat is used with translation overridefr output self engine rendertostring date02l10n d datetime2008 1 1 self assertequaloutput 1 janvier 2008 setupdate03 ddate m def testdate03self 9520 make sure date doesn t blow up on nondates output self engine rendertostringdate03 d failstring self assertequaloutput iso date formats setupdate04 ddate o def testdate04self output self engine rendertostringdate04 d datetime2008 12 29 self assertequaloutput 2009 setupdate05 ddate o def testdate05self output self engine rendertostringdate05 d datetime2010 1 3 self assertequaloutput 2009 timezone name setupdate06 ddate e def testdate06self output self engine rendertostring date06 d datetime2009 3 12 tzinfotimezone getfixedtimezone30 self assertequaloutput 0030 setupdate07 ddate e def testdate07self output self engine rendertostringdate07 d datetime2009 3 12 self assertequaloutput 19370 make sure date doesn t blow up on a midnight time object setupdate08 tdate h i def testdate08self output self engine rendertostringdate08 t time0 1 self assertequaloutput 00 01 setupdate09 tdate h i def testdate09self output self engine rendertostringdate09 t time0 0 self assertequaloutput 00 00 setupdatelazy tdate h i def testdatelazyself output self engine rendertostringdatelazy t time0 0 self assertequaloutput 00 00 class functiontestssimpletestcase def testdateself self assertequaldatedatetime2005 12 29 d f y 29 december 2005 def testnoargsself self assertequaldate self assertequaldatenone def testescapecharactersself self assertequaldatedatetime2005 12 29 rjs of f 29th of december without arg the active language s date_format is used 9520 make sure date doesn t blow up on non dates iso date formats timezone name 19370 make sure date doesn t blow up on a midnight time object
from datetime import datetime, time

from django.template.defaultfilters import date
from django.test import SimpleTestCase
from django.utils import timezone, translation

from ..utils import setup
from .timezone_utils import TimezoneTestCase


class DateTests(TimezoneTestCase):
    @setup({"date01": '{{ d|date:"m" }}'})
    def test_date01(self):
        output = self.engine.render_to_string("date01", {"d": datetime(2008, 1, 1)})
        self.assertEqual(output, "01")

    @setup({"date02": "{{ d|date }}"})
    def test_date02(self):
        output = self.engine.render_to_string("date02", {"d": datetime(2008, 1, 1)})
        self.assertEqual(output, "Jan. 1, 2008")

    @setup({"date02_l10n": "{{ d|date }}"})
    def test_date02_l10n(self):
        
        with translation.override("fr"):
            output = self.engine.render_to_string(
                "date02_l10n", {"d": datetime(2008, 1, 1)}
            )
        self.assertEqual(output, "1 janvier 2008")

    @setup({"date03": '{{ d|date:"m" }}'})
    def test_date03(self):
        
        output = self.engine.render_to_string("date03", {"d": "fail_string"})
        self.assertEqual(output, "")

    
    @setup({"date04": '{{ d|date:"o" }}'})
    def test_date04(self):
        output = self.engine.render_to_string("date04", {"d": datetime(2008, 12, 29)})
        self.assertEqual(output, "2009")

    @setup({"date05": '{{ d|date:"o" }}'})
    def test_date05(self):
        output = self.engine.render_to_string("date05", {"d": datetime(2010, 1, 3)})
        self.assertEqual(output, "2009")

    
    @setup({"date06": '{{ d|date:"e" }}'})
    def test_date06(self):
        output = self.engine.render_to_string(
            "date06",
            {"d": datetime(2009, 3, 12, tzinfo=timezone.get_fixed_timezone(30))},
        )
        self.assertEqual(output, "+0030")

    @setup({"date07": '{{ d|date:"e" }}'})
    def test_date07(self):
        output = self.engine.render_to_string("date07", {"d": datetime(2009, 3, 12)})
        self.assertEqual(output, "")

    
    @setup({"date08": '{{ t|date:"H:i" }}'})
    def test_date08(self):
        output = self.engine.render_to_string("date08", {"t": time(0, 1)})
        self.assertEqual(output, "00:01")

    @setup({"date09": '{{ t|date:"H:i" }}'})
    def test_date09(self):
        output = self.engine.render_to_string("date09", {"t": time(0, 0)})
        self.assertEqual(output, "00:00")

    @setup({"datelazy": '{{ t|date:_("H:i") }}'})
    def test_date_lazy(self):
        output = self.engine.render_to_string("datelazy", {"t": time(0, 0)})
        self.assertEqual(output, "00:00")


class FunctionTests(SimpleTestCase):
    def test_date(self):
        self.assertEqual(date(datetime(2005, 12, 29), "d F Y"), "29 December 2005")

    def test_no_args(self):
        self.assertEqual(date(""), "")
        self.assertEqual(date(None), "")

    def test_escape_characters(self):
        self.assertEqual(date(datetime(2005, 12, 29), r"jS \o\f F"), "29th of December")