blob: 6451b27a434927f4e813965d9d7e6b4a650e26b2 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Barry Warsawb0c22321996-12-06 23:30:07 +00002import time
Fred Drakebc561982001-05-22 17:02:02 +00003import unittest
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +00004import locale
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):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000021 self.assertTrue(time.ctime(self.t)
Fred Drakebc561982001-05-22 17:02:02 +000022 == time.asctime(time.localtime(self.t)))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000023 self.assertTrue(int(time.mktime(time.localtime(self.t)))
Guido van Rossume2a383d2007-01-15 16:59:06 +000024 == int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000025
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
Alexander Belopolsky38e29962010-10-01 14:18:49 +000040 def _bounds_checking(self, func=time.strftime):
Brett Cannond1080a32004-03-02 04:38:10 +000041 # 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)]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000045 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000046 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
47 if time.accept2dyear:
Alexander Belopolsky38e29962010-10-01 14:18:49 +000048 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000049 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000050 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000051 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 # Check month [1, 12] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000053 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000055 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000056 (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
Alexander Belopolsky38e29962010-10-01 14:18:49 +000058 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000060 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000061 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 # Check hour [0, 23]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000063 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000064 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000065 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000066 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 # Check minute [0, 59]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000068 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000069 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000070 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000071 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000073 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000074 (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)
Alexander Belopolsky38e29962010-10-01 14:18:49 +000077 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000078 (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.
Alexander Belopolsky38e29962010-10-01 14:18:49 +000083 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000084 (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
Alexander Belopolsky38e29962010-10-01 14:18:49 +000086 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000088 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000089 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000090
Alexander Belopolsky38e29962010-10-01 14:18:49 +000091 def test_strftime_bounding_check(self):
92 self._bounds_checking(lambda tup: time.strftime('', tup))
93
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094 def test_default_values_for_zero(self):
95 # Make sure that using all zeros uses the proper default values.
96 # No test for daylight savings since strftime() does not change output
97 # based on its value.
98 expected = "2000 01 01 00 00 00 1 001"
99 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000100 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000102 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103 # Should be able to go round-trip from strftime to strptime without
104 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000105 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', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 format = '%' + directive
110 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000111 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000113 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 self.fail("conversion specifier %r failed with '%s' input." %
115 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000116
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000117 def test_strptime_bytes(self):
118 # Make sure only strings are accepted as arguments to strptime.
119 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
120 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
121
Fred Drakebc561982001-05-22 17:02:02 +0000122 def test_asctime(self):
123 time.asctime(time.gmtime(self.t))
124 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000125 self.assertRaises(TypeError, time.asctime, ())
Georg Brandle10608c2011-01-02 22:33:43 +0000126 # XXX: POSIX-compliant asctime should refuse to convert year > 9999,
127 # but glibc implementation does not. For now, just check it doesn't
128 # segfault as it did before, and the result contains no newline.
Alexander Belopolsky3e913c92011-01-02 22:16:10 +0000129 try:
Georg Brandle10608c2011-01-02 22:33:43 +0000130 result = time.asctime((12345, 1, 0, 0, 0, 0, 0, 0, 0))
Alexander Belopolsky3e913c92011-01-02 22:16:10 +0000131 except ValueError:
Georg Brandle10608c2011-01-02 22:33:43 +0000132 # for POSIX-compliant runtimes
Alexander Belopolsky3e913c92011-01-02 22:16:10 +0000133 pass
Georg Brandle10608c2011-01-02 22:33:43 +0000134 else:
135 self.assertNotIn('\n', result)
Fred Drakebc561982001-05-22 17:02:02 +0000136
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000137 def test_asctime_bounding_check(self):
138 self._bounds_checking(time.asctime)
139
Georg Brandle10608c2011-01-02 22:33:43 +0000140 def test_ctime(self):
141 # XXX: POSIX-compliant ctime should refuse to convert year > 9999,
142 # but glibc implementation does not. For now, just check it doesn't
143 # segfault as it did before, and the result contains no newline.
144 try:
145 result = time.ctime(1e12)
146 except ValueError:
147 # for POSIX-compliant runtimes (or 32-bit systems, where time_t
148 # cannot hold timestamps with a five-digit year)
149 pass
150 else:
151 self.assertNotIn('\n', result)
152
R. David Murray6ecf76e2010-12-14 01:22:50 +0000153 @unittest.skipIf(not hasattr(time, "tzset"),
154 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000155 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000156
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000157 from os import environ
158
Tim Peters0eadaac2003-04-24 16:02:54 +0000159 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000160 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000161 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000162
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000163 # These formats are correct for 2002, and possibly future years
164 # This format is the 'standard' as documented at:
165 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
166 # They are also documented in the tzset(3) man page on most Unix
167 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000168 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000169 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
170 utc='UTC+0'
171
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000172 org_TZ = environ.get('TZ',None)
173 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000174 # Make sure we can switch to UTC time and results are correct
175 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000176 # Note that altzone is undefined in UTC, as there is no DST
177 environ['TZ'] = eastern
178 time.tzset()
179 environ['TZ'] = utc
180 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000181 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000182 time.gmtime(xmas2002), time.localtime(xmas2002)
183 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000184 self.assertEqual(time.daylight, 0)
185 self.assertEqual(time.timezone, 0)
186 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000187
188 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000189 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000190 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000191 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
192 self.assertEqual(time.tzname, ('EST', 'EDT'))
193 self.assertEqual(len(time.tzname), 2)
194 self.assertEqual(time.daylight, 1)
195 self.assertEqual(time.timezone, 18000)
196 self.assertEqual(time.altzone, 14400)
197 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
198 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000199
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000200 # Now go to the southern hemisphere.
201 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000202 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000203 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
204 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
205 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
206 self.assertEqual(len(time.tzname), 2)
207 self.assertEqual(time.daylight, 1)
208 self.assertEqual(time.timezone, -36000)
209 self.assertEqual(time.altzone, -39600)
210 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000211
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000212 finally:
213 # Repair TZ environment variable in case any other tests
214 # rely on it.
215 if org_TZ is not None:
216 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000217 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000218 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000219 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000220
Tim Peters1b6f7a92004-06-20 02:50:16 +0000221 def test_insane_timestamps(self):
222 # It's possible that some platform maps time_t to double,
223 # and that this test will fail there. This test should
224 # exempt such platforms (provided they return reasonable
225 # results!).
226 for func in time.ctime, time.gmtime, time.localtime:
227 for unreasonable in -1e200, 1e200:
228 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000229
Fred Drakef901abd2004-08-03 17:58:55 +0000230 def test_ctime_without_arg(self):
231 # Not sure how to check the values, since the clock could tick
232 # at any time. Make sure these are at least accepted and
233 # don't raise errors.
234 time.ctime()
235 time.ctime(None)
236
237 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 gt0 = time.gmtime()
239 gt1 = time.gmtime(None)
240 t0 = time.mktime(gt0)
241 t1 = time.mktime(gt1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000242 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000243
244 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245 lt0 = time.localtime()
246 lt1 = time.localtime(None)
247 t0 = time.mktime(lt0)
248 t1 = time.mktime(lt1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000249 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000250
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000251class TestLocale(unittest.TestCase):
252 def setUp(self):
253 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000254
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000255 def tearDown(self):
256 locale.setlocale(locale.LC_ALL, self.oldloc)
257
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000258 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000259 try:
260 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
261 except locale.Error:
262 # skip this test
263 return
264 # This should not cause an exception
265 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
266
267def test_main():
268 support.run_unittest(TimeTestCase, TestLocale)
Fred Drake2e2be372001-09-20 21:33:42 +0000269
270if __name__ == "__main__":
271 test_main()