0001 from test import test_support 0002 import time 0003 import unittest 0004 0005 0006 class TimeTestCase(unittest.TestCase): 0007 0008 def setUp(self): 0009 self.t = time.time() 0010 0011 def test_data_attributes(self): 0012 time.altzone 0013 time.daylight 0014 time.timezone 0015 time.tzname 0016 0017 def test_clock(self): 0018 time.clock() 0019 0020 def test_conversions(self): 0021 self.assert_(time.ctime(self.t) 0022 == time.asctime(time.localtime(self.t))) 0023 self.assert_(long(time.mktime(time.localtime(self.t))) 0024 == long(self.t)) 0025 0026 def test_sleep(self): 0027 time.sleep(1.2) 0028 0029 def test_strftime(self): 0030 tt = time.gmtime(self.t) 0031 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', 0032 'j', 'm', 'M', 'p', 'S', 0033 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): 0034 format = ' %' + directive 0035 try: 0036 time.strftime(format, tt) 0037 except ValueError: 0038 self.fail('conversion specifier: %r failed.' % format) 0039 0040 def test_strftime_bounds_checking(self): 0041 # Make sure that strftime() checks the bounds of the various parts 0042 #of the time tuple. 0043 0044 # Check year 0045 self.assertRaises(ValueError, time.strftime, '', 0046 (1899, 1, 1, 0, 0, 0, 0, 1, -1)) 0047 if time.accept2dyear: 0048 self.assertRaises(ValueError, time.strftime, '', 0049 (-1, 1, 1, 0, 0, 0, 0, 1, -1)) 0050 self.assertRaises(ValueError, time.strftime, '', 0051 (100, 1, 1, 0, 0, 0, 0, 1, -1)) 0052 # Check month 0053 self.assertRaises(ValueError, time.strftime, '', 0054 (1900, 0, 1, 0, 0, 0, 0, 1, -1)) 0055 self.assertRaises(ValueError, time.strftime, '', 0056 (1900, 13, 1, 0, 0, 0, 0, 1, -1)) 0057 # Check day of month 0058 self.assertRaises(ValueError, time.strftime, '', 0059 (1900, 1, 0, 0, 0, 0, 0, 1, -1)) 0060 self.assertRaises(ValueError, time.strftime, '', 0061 (1900, 1, 32, 0, 0, 0, 0, 1, -1)) 0062 # Check hour 0063 self.assertRaises(ValueError, time.strftime, '', 0064 (1900, 1, 1, -1, 0, 0, 0, 1, -1)) 0065 self.assertRaises(ValueError, time.strftime, '', 0066 (1900, 1, 1, 24, 0, 0, 0, 1, -1)) 0067 # Check minute 0068 self.assertRaises(ValueError, time.strftime, '', 0069 (1900, 1, 1, 0, -1, 0, 0, 1, -1)) 0070 self.assertRaises(ValueError, time.strftime, '', 0071 (1900, 1, 1, 0, 60, 0, 0, 1, -1)) 0072 # Check second 0073 self.assertRaises(ValueError, time.strftime, '', 0074 (1900, 1, 1, 0, 0, -1, 0, 1, -1)) 0075 # C99 only requires allowing for one leap second, but Python's docs say 0076 # allow two leap seconds (0..61) 0077 self.assertRaises(ValueError, time.strftime, '', 0078 (1900, 1, 1, 0, 0, 62, 0, 1, -1)) 0079 # No check for upper-bound day of week; 0080 # value forced into range by a ``% 7`` calculation. 0081 # Start check at -2 since gettmarg() increments value before taking 0082 # modulo. 0083 self.assertRaises(ValueError, time.strftime, '', 0084 (1900, 1, 1, 0, 0, 0, -2, 1, -1)) 0085 # Check day of the year 0086 self.assertRaises(ValueError, time.strftime, '', 0087 (1900, 1, 1, 0, 0, 0, 0, 0, -1)) 0088 self.assertRaises(ValueError, time.strftime, '', 0089 (1900, 1, 1, 0, 0, 0, 0, 367, -1)) 0090 # Check daylight savings flag 0091 self.assertRaises(ValueError, time.strftime, '', 0092 (1900, 1, 1, 0, 0, 0, 0, 1, -2)) 0093 self.assertRaises(ValueError, time.strftime, '', 0094 (1900, 1, 1, 0, 0, 0, 0, 1, 2)) 0095 0096 def test_strptime(self): 0097 tt = time.gmtime(self.t) 0098 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', 0099 'j', 'm', 'M', 'p', 'S', 0100 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): 0101 format = ' %' + directive 0102 try: 0103 time.strptime(time.strftime(format, tt), format) 0104 except ValueError: 0105 self.fail('conversion specifier: %r failed.' % format) 0106 0107 def test_asctime(self): 0108 time.asctime(time.gmtime(self.t)) 0109 self.assertRaises(TypeError, time.asctime, 0) 0110 0111 def test_tzset(self): 0112 if not hasattr(time, "tzset"): 0113 return # Can't test this; don't want the test suite to fail 0114 0115 from os import environ 0116 0117 # Epoch time of midnight Dec 25th 2002. Never DST in northern 0118 # hemisphere. 0119 xmas2002 = 1040774400.0 0120 0121 # These formats are correct for 2002, and possibly future years 0122 # This format is the 'standard' as documented at: 0123 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html 0124 # They are also documented in the tzset(3) man page on most Unix 0125 # systems. 0126 eastern = 'EST+05EDT,M4.1.0,M10.5.0' 0127 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' 0128 utc='UTC+0' 0129 0130 org_TZ = environ.get('TZ',None) 0131 try: 0132 # Make sure we can switch to UTC time and results are correct 0133 # Note that unknown timezones default to UTC. 0134 # Note that altzone is undefined in UTC, as there is no DST 0135 environ['TZ'] = eastern 0136 time.tzset() 0137 environ['TZ'] = utc 0138 time.tzset() 0139 self.failUnlessEqual( 0140 time.gmtime(xmas2002), time.localtime(xmas2002) 0141 ) 0142 self.failUnlessEqual(time.daylight, 0) 0143 self.failUnlessEqual(time.timezone, 0) 0144 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0) 0145 0146 # Make sure we can switch to US/Eastern 0147 environ['TZ'] = eastern 0148 time.tzset() 0149 self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) 0150 self.failUnlessEqual(time.tzname, ('EST', 'EDT')) 0151 self.failUnlessEqual(len(time.tzname), 2) 0152 self.failUnlessEqual(time.daylight, 1) 0153 self.failUnlessEqual(time.timezone, 18000) 0154 self.failUnlessEqual(time.altzone, 14400) 0155 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0) 0156 self.failUnlessEqual(len(time.tzname), 2) 0157 0158 # Now go to the southern hemisphere. 0159 environ['TZ'] = victoria 0160 time.tzset() 0161 self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) 0162 self.failUnless(time.tzname[0] == 'AEST', str(time.tzname[0])) 0163 self.failUnless(time.tzname[1] == 'AEDT', str(time.tzname[1])) 0164 self.failUnlessEqual(len(time.tzname), 2) 0165 self.failUnlessEqual(time.daylight, 1) 0166 self.failUnlessEqual(time.timezone, -36000) 0167 self.failUnlessEqual(time.altzone, -39600) 0168 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 1) 0169 0170 finally: 0171 # Repair TZ environment variable in case any other tests 0172 # rely on it. 0173 if org_TZ is not None: 0174 environ['TZ'] = org_TZ 0175 elif environ.has_key('TZ'): 0176 del environ['TZ'] 0177 time.tzset() 0178 0179 def test_insane_timestamps(self): 0180 # It's possible that some platform maps time_t to double, 0181 # and that this test will fail there. This test should 0182 # exempt such platforms (provided they return reasonable 0183 # results!). 0184 for func in time.ctime, time.gmtime, time.localtime: 0185 for unreasonable in -1e200, 1e200: 0186 self.assertRaises(ValueError, func, unreasonable) 0187 0188 def test_ctime_without_arg(self): 0189 # Not sure how to check the values, since the clock could tick 0190 # at any time. Make sure these are at least accepted and 0191 # don't raise errors. 0192 time.ctime() 0193 time.ctime(None) 0194 0195 def test_gmtime_without_arg(self): 0196 t0 = time.mktime(time.gmtime()) 0197 t1 = time.mktime(time.gmtime(None)) 0198 self.assert_(0 <= (t1-t0) < 0.2) 0199 0200 def test_localtime_without_arg(self): 0201 t0 = time.mktime(time.localtime()) 0202 t1 = time.mktime(time.localtime(None)) 0203 self.assert_(0 <= (t1-t0) < 0.2) 0204 0205 def test_main(): 0206 test_support.run_unittest(TimeTestCase) 0207 0208 0209 if __name__ == "__main__": 0210 test_main() 0211
Generated by PyXR 0.9.4