blob: f4be7590f1c18929ebe66cea8edcfa8fd9c43620 [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test import test_support
Barry Warsawb0c22321996-12-06 23:30:07 +00002import time
Fred Drakebc561982001-05-22 17:02:02 +00003import unittest
Barry Warsawb0c22321996-12-06 23:30:07 +00004
Barry Warsawb0c22321996-12-06 23:30:07 +00005
Fred Drakebc561982001-05-22 17:02:02 +00006class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +00007
Fred Drakebc561982001-05-22 17:02:02 +00008 def setUp(self):
9 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000010
Fred Drakebc561982001-05-22 17:02:02 +000011 def test_data_attributes(self):
12 time.altzone
13 time.daylight
14 time.timezone
15 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000016
Fred Drakebc561982001-05-22 17:02:02 +000017 def test_clock(self):
18 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000019
Fred Drakebc561982001-05-22 17:02:02 +000020 def test_conversions(self):
21 self.assert_(time.ctime(self.t)
22 == time.asctime(time.localtime(self.t)))
23 self.assert_(long(time.mktime(time.localtime(self.t)))
24 == long(self.t))
25
26 def test_sleep(self):
27 time.sleep(1.2)
28
29 def test_strftime(self):
30 tt = time.gmtime(self.t)
31 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
32 'j', 'm', 'M', 'p', 'S',
33 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
34 format = ' %' + directive
35 try:
36 time.strftime(format, tt)
37 except ValueError:
38 self.fail('conversion specifier: %r failed.' % format)
39
Brett Cannond1080a32004-03-02 04:38:10 +000040 def test_strftime_bounds_checking(self):
41 # Make sure that strftime() checks the bounds of the various parts
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000043
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 # Check year [1900, max(int)]
Brett Cannond1080a32004-03-02 04:38:10 +000045 self.assertRaises(ValueError, time.strftime, '',
46 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
47 if time.accept2dyear:
48 self.assertRaises(ValueError, time.strftime, '',
49 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
50 self.assertRaises(ValueError, time.strftime, '',
51 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 # Check month [1, 12] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000053 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000055 self.assertRaises(ValueError, time.strftime, '',
56 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 # Check day of month [1, 31] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000058 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000060 self.assertRaises(ValueError, time.strftime, '',
61 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 # Check hour [0, 23]
Brett Cannond1080a32004-03-02 04:38:10 +000063 self.assertRaises(ValueError, time.strftime, '',
64 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
65 self.assertRaises(ValueError, time.strftime, '',
66 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 # Check minute [0, 59]
Brett Cannond1080a32004-03-02 04:38:10 +000068 self.assertRaises(ValueError, time.strftime, '',
69 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
70 self.assertRaises(ValueError, time.strftime, '',
71 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 # Check second [0, 61]
Brett Cannond1080a32004-03-02 04:38:10 +000073 self.assertRaises(ValueError, time.strftime, '',
74 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
75 # C99 only requires allowing for one leap second, but Python's docs say
76 # allow two leap seconds (0..61)
77 self.assertRaises(ValueError, time.strftime, '',
78 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
79 # No check for upper-bound day of week;
80 # value forced into range by a ``% 7`` calculation.
81 # Start check at -2 since gettmarg() increments value before taking
82 # modulo.
83 self.assertRaises(ValueError, time.strftime, '',
84 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000085 # Check day of the year [1, 366] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000086 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000088 self.assertRaises(ValueError, time.strftime, '',
89 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 # Check daylight savings flag [-1, 1]
Brett Cannond1080a32004-03-02 04:38:10 +000091 self.assertRaises(ValueError, time.strftime, '',
92 (1900, 1, 1, 0, 0, 0, 0, 1, -2))
93 self.assertRaises(ValueError, time.strftime, '',
94 (1900, 1, 1, 0, 0, 0, 0, 1, 2))
95
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096 def test_default_values_for_zero(self):
97 # Make sure that using all zeros uses the proper default values.
98 # No test for daylight savings since strftime() does not change output
99 # based on its value.
100 expected = "2000 01 01 00 00 00 1 001"
101 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
102 self.assertEquals(expected, result)
103
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000104 def test_strptime(self):
105 tt = time.gmtime(self.t)
106 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
107 'j', 'm', 'M', 'p', 'S',
108 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
109 format = ' %' + directive
110 try:
111 time.strptime(time.strftime(format, tt), format)
112 except ValueError:
113 self.fail('conversion specifier: %r failed.' % format)
114
Fred Drakebc561982001-05-22 17:02:02 +0000115 def test_asctime(self):
116 time.asctime(time.gmtime(self.t))
117 self.assertRaises(TypeError, time.asctime, 0)
118
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000119 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000120 if not hasattr(time, "tzset"):
121 return # Can't test this; don't want the test suite to fail
122
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000123 from os import environ
124
Tim Peters0eadaac2003-04-24 16:02:54 +0000125 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000126 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000127 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000128
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000129 # These formats are correct for 2002, and possibly future years
130 # This format is the 'standard' as documented at:
131 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
132 # They are also documented in the tzset(3) man page on most Unix
133 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000134 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000135 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
136 utc='UTC+0'
137
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000138 org_TZ = environ.get('TZ',None)
139 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000140 # Make sure we can switch to UTC time and results are correct
141 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000142 # Note that altzone is undefined in UTC, as there is no DST
143 environ['TZ'] = eastern
144 time.tzset()
145 environ['TZ'] = utc
146 time.tzset()
147 self.failUnlessEqual(
148 time.gmtime(xmas2002), time.localtime(xmas2002)
149 )
Tim Peters0eadaac2003-04-24 16:02:54 +0000150 self.failUnlessEqual(time.daylight, 0)
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000151 self.failUnlessEqual(time.timezone, 0)
152 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000153
154 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000155 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000156 time.tzset()
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000157 self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
158 self.failUnlessEqual(time.tzname, ('EST', 'EDT'))
159 self.failUnlessEqual(len(time.tzname), 2)
160 self.failUnlessEqual(time.daylight, 1)
161 self.failUnlessEqual(time.timezone, 18000)
162 self.failUnlessEqual(time.altzone, 14400)
163 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0)
164 self.failUnlessEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000165
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000166 # Now go to the southern hemisphere.
167 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000168 time.tzset()
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000169 self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
170 self.failUnless(time.tzname[0] == 'AEST', str(time.tzname[0]))
171 self.failUnless(time.tzname[1] == 'AEDT', str(time.tzname[1]))
172 self.failUnlessEqual(len(time.tzname), 2)
173 self.failUnlessEqual(time.daylight, 1)
174 self.failUnlessEqual(time.timezone, -36000)
175 self.failUnlessEqual(time.altzone, -39600)
176 self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000177
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000178 finally:
179 # Repair TZ environment variable in case any other tests
180 # rely on it.
181 if org_TZ is not None:
182 environ['TZ'] = org_TZ
183 elif environ.has_key('TZ'):
184 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000185 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000186
Tim Peters1b6f7a92004-06-20 02:50:16 +0000187 def test_insane_timestamps(self):
188 # It's possible that some platform maps time_t to double,
189 # and that this test will fail there. This test should
190 # exempt such platforms (provided they return reasonable
191 # results!).
192 for func in time.ctime, time.gmtime, time.localtime:
193 for unreasonable in -1e200, 1e200:
194 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000195
Fred Drakef901abd2004-08-03 17:58:55 +0000196 def test_ctime_without_arg(self):
197 # Not sure how to check the values, since the clock could tick
198 # at any time. Make sure these are at least accepted and
199 # don't raise errors.
200 time.ctime()
201 time.ctime(None)
202
203 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204 gt0 = time.gmtime()
205 gt1 = time.gmtime(None)
206 t0 = time.mktime(gt0)
207 t1 = time.mktime(gt1)
Fred Drakef901abd2004-08-03 17:58:55 +0000208 self.assert_(0 <= (t1-t0) < 0.2)
209
210 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211 lt0 = time.localtime()
212 lt1 = time.localtime(None)
213 t0 = time.mktime(lt0)
214 t1 = time.mktime(lt1)
Fred Drakef901abd2004-08-03 17:58:55 +0000215 self.assert_(0 <= (t1-t0) < 0.2)
216
Fred Drake2e2be372001-09-20 21:33:42 +0000217def test_main():
218 test_support.run_unittest(TimeTestCase)
219
220
221if __name__ == "__main__":
222 test_main()