Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_date.py
blob: 1740df47da217039c1675812a09e772e4508f412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from datetime import datetime, time

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

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({'date03': '{{ d|date:"m" }}'})
    def test_date03(self):
        """
        #9520: Make sure |date doesn't blow up on non-dates
        """
        output = self.engine.render_to_string('date03', {'d': 'fail_string'})
        self.assertEqual(output, '')

    # ISO date formats
    @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')

    # Timezone name
    @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, '')

    # #19370: Make sure |date doesn't blow up on a midnight time object
    @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')


class FunctionTests(SimpleTestCase):

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

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