PageRenderTime 177ms CodeModel.GetById 154ms app.highlight 14ms RepoModel.GetById 6ms app.codeStats 0ms

/tests/regressiontests/utils/dateformat.py

https://code.google.com/p/mango-py/
Python | 129 lines | 113 code | 13 blank | 3 comment | 2 complexity | 1424fccd6241efc65c5e42b80c87c9ec MD5 | raw file
  1from datetime import datetime, date
  2import os
  3import time
  4
  5from django.utils.dateformat import format
  6from django.utils import dateformat, translation, unittest
  7from django.utils.tzinfo import FixedOffset, LocalTimezone
  8
  9
 10class DateFormatTests(unittest.TestCase):
 11    def setUp(self):
 12        self.old_TZ = os.environ.get('TZ')
 13        os.environ['TZ'] = 'Europe/Copenhagen'
 14        translation.activate('en-us')
 15
 16        try:
 17            # Check if a timezone has been set
 18            time.tzset()
 19            self.tz_tests = True
 20        except AttributeError:
 21            # No timezone available. Don't run the tests that require a TZ
 22            self.tz_tests = False
 23
 24    def tearDown(self):
 25        if self.old_TZ is None:
 26            del os.environ['TZ']
 27        else:
 28            os.environ['TZ'] = self.old_TZ
 29
 30        # Cleanup - force re-evaluation of TZ environment variable.
 31        if self.tz_tests:
 32            time.tzset()
 33
 34    def test_date(self):
 35        d = date(2009, 5, 16)
 36        self.assertEqual(date.fromtimestamp(int(format(d, 'U'))), d)
 37
 38    def test_naive_datetime(self):
 39        dt = datetime(2009, 5, 16, 5, 30, 30)
 40        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt)
 41
 42    def test_datetime_with_local_tzinfo(self):
 43        ltz = LocalTimezone(datetime.now())
 44        dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz)
 45        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
 46        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.replace(tzinfo=None))
 47
 48    def test_datetime_with_tzinfo(self):
 49        tz = FixedOffset(-510)
 50        ltz = LocalTimezone(datetime.now())
 51        dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
 52        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt)
 53        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
 54        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None))
 55        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple())
 56        self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple(), dt.utctimetuple())
 57
 58    def test_epoch(self):
 59        utc = FixedOffset(0)
 60        udt = datetime(1970, 1, 1, tzinfo=utc)
 61        self.assertEqual(format(udt, 'U'), u'0')
 62
 63    def test_empty_format(self):
 64        my_birthday = datetime(1979, 7, 8, 22, 00)
 65
 66        self.assertEqual(dateformat.format(my_birthday, ''), u'')
 67
 68    def test_am_pm(self):
 69        my_birthday = datetime(1979, 7, 8, 22, 00)
 70
 71        self.assertEqual(dateformat.format(my_birthday, 'a'), u'p.m.')
 72
 73    def test_date_formats(self):
 74        my_birthday = datetime(1979, 7, 8, 22, 00)
 75        timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
 76
 77        self.assertEqual(dateformat.format(my_birthday, 'A'), u'PM')
 78        self.assertEqual(dateformat.format(timestamp, 'c'), u'2008-05-19T11:45:23.123456')
 79        self.assertEqual(dateformat.format(my_birthday, 'd'), u'08')
 80        self.assertEqual(dateformat.format(my_birthday, 'j'), u'8')
 81        self.assertEqual(dateformat.format(my_birthday, 'l'), u'Sunday')
 82        self.assertEqual(dateformat.format(my_birthday, 'L'), u'False')
 83        self.assertEqual(dateformat.format(my_birthday, 'm'), u'07')
 84        self.assertEqual(dateformat.format(my_birthday, 'M'), u'Jul')
 85        self.assertEqual(dateformat.format(my_birthday, 'b'), u'jul')
 86        self.assertEqual(dateformat.format(my_birthday, 'n'), u'7')
 87        self.assertEqual(dateformat.format(my_birthday, 'N'), u'July')
 88
 89    def test_time_formats(self):
 90        my_birthday = datetime(1979, 7, 8, 22, 00)
 91
 92        self.assertEqual(dateformat.format(my_birthday, 'P'), u'10 p.m.')
 93        self.assertEqual(dateformat.format(my_birthday, 's'), u'00')
 94        self.assertEqual(dateformat.format(my_birthday, 'S'), u'th')
 95        self.assertEqual(dateformat.format(my_birthday, 't'), u'31')
 96        self.assertEqual(dateformat.format(my_birthday, 'w'), u'0')
 97        self.assertEqual(dateformat.format(my_birthday, 'W'), u'27')
 98        self.assertEqual(dateformat.format(my_birthday, 'y'), u'79')
 99        self.assertEqual(dateformat.format(my_birthday, 'Y'), u'1979')
100        self.assertEqual(dateformat.format(my_birthday, 'z'), u'189')
101
102    def test_dateformat(self):
103        my_birthday = datetime(1979, 7, 8, 22, 00)
104
105        self.assertEqual(dateformat.format(my_birthday, r'Y z \C\E\T'), u'1979 189 CET')
106
107        self.assertEqual(dateformat.format(my_birthday, r'jS o\f F'), u'8th of July')
108
109    def test_futuredates(self):
110        the_future = datetime(2100, 10, 25, 0, 00)
111        self.assertEqual(dateformat.format(the_future, r'Y'), u'2100')
112
113    def test_timezones(self):
114        my_birthday = datetime(1979, 7, 8, 22, 00)
115        summertime = datetime(2005, 10, 30, 1, 00)
116        wintertime = datetime(2005, 10, 30, 4, 00)
117        timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
118
119        if self.tz_tests:
120            self.assertEqual(dateformat.format(my_birthday, 'O'), u'+0100')
121            self.assertEqual(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
122            self.assertEqual(dateformat.format(my_birthday, 'T'), u'CET')
123            self.assertEqual(dateformat.format(my_birthday, 'U'), u'300315600')
124            self.assertEqual(dateformat.format(timestamp, 'u'), u'123456')
125            self.assertEqual(dateformat.format(my_birthday, 'Z'), u'3600')
126            self.assertEqual(dateformat.format(summertime, 'I'), u'1')
127            self.assertEqual(dateformat.format(summertime, 'O'), u'+0200')
128            self.assertEqual(dateformat.format(wintertime, 'I'), u'0')
129            self.assertEqual(dateformat.format(wintertime, 'O'), u'+0100')