parts/django/tests/regressiontests/utils/dateformat.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 from datetime import datetime, date
       
     2 import os
       
     3 import time
       
     4 import unittest
       
     5 
       
     6 from django.utils.dateformat import format
       
     7 from django.utils import dateformat, translation
       
     8 from django.utils.tzinfo import FixedOffset, LocalTimezone
       
     9 
       
    10 class 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.assertEquals(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.assertEquals(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.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
       
    46         self.assertEquals(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.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt)
       
    53         self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
       
    54         self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None))
       
    55         self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple())
       
    56         self.assertEquals(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.assertEquals(format(udt, 'U'), u'0')
       
    62 
       
    63     def test_empty_format(self):
       
    64         my_birthday = datetime(1979, 7, 8, 22, 00)
       
    65 
       
    66         self.assertEquals(dateformat.format(my_birthday, ''), u'')
       
    67 
       
    68     def test_am_pm(self):
       
    69         my_birthday = datetime(1979, 7, 8, 22, 00)
       
    70 
       
    71         self.assertEquals(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.assertEquals(dateformat.format(my_birthday, 'A'), u'PM')
       
    78         self.assertEquals(dateformat.format(timestamp, 'c'), u'2008-05-19T11:45:23.123456')
       
    79         self.assertEquals(dateformat.format(my_birthday, 'd'), u'08')
       
    80         self.assertEquals(dateformat.format(my_birthday, 'j'), u'8')
       
    81         self.assertEquals(dateformat.format(my_birthday, 'l'), u'Sunday')
       
    82         self.assertEquals(dateformat.format(my_birthday, 'L'), u'False')
       
    83         self.assertEquals(dateformat.format(my_birthday, 'm'), u'07')
       
    84         self.assertEquals(dateformat.format(my_birthday, 'M'), u'Jul')
       
    85         self.assertEquals(dateformat.format(my_birthday, 'b'), u'jul')
       
    86         self.assertEquals(dateformat.format(my_birthday, 'n'), u'7')
       
    87         self.assertEquals(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.assertEquals(dateformat.format(my_birthday, 'P'), u'10 p.m.')
       
    93         self.assertEquals(dateformat.format(my_birthday, 's'), u'00')
       
    94         self.assertEquals(dateformat.format(my_birthday, 'S'), u'th')
       
    95         self.assertEquals(dateformat.format(my_birthday, 't'), u'31')
       
    96         self.assertEquals(dateformat.format(my_birthday, 'w'), u'0')
       
    97         self.assertEquals(dateformat.format(my_birthday, 'W'), u'27')
       
    98         self.assertEquals(dateformat.format(my_birthday, 'y'), u'79')
       
    99         self.assertEquals(dateformat.format(my_birthday, 'Y'), u'1979')
       
   100         self.assertEquals(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.assertEquals(dateformat.format(my_birthday, r'Y z \C\E\T'), u'1979 189 CET')
       
   106 
       
   107         self.assertEquals(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.assertEquals(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.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100')
       
   121             self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
       
   122             self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET')
       
   123             self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600')
       
   124             self.assertEquals(dateformat.format(timestamp, 'u'), u'123456')
       
   125             self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600')
       
   126             self.assertEquals(dateformat.format(summertime, 'I'), u'1')
       
   127             self.assertEquals(dateformat.format(summertime, 'O'), u'+0200')
       
   128             self.assertEquals(dateformat.format(wintertime, 'I'), u'0')
       
   129             self.assertEquals(dateformat.format(wintertime, 'O'), u'+0100')