blob: d6de54b36d8a21ad8061aaff59859f687b76351e [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
Alexander Belopolskyb9588b52011-01-04 16:34:30 +00005import sysconfig
Barry Warsawb0c22321996-12-06 23:30:07 +00006
Fred Drakebc561982001-05-22 17:02:02 +00007class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +00008
Fred Drakebc561982001-05-22 17:02:02 +00009 def setUp(self):
10 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000011
Fred Drakebc561982001-05-22 17:02:02 +000012 def test_data_attributes(self):
13 time.altzone
14 time.daylight
15 time.timezone
16 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000017
Fred Drakebc561982001-05-22 17:02:02 +000018 def test_clock(self):
19 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000020
Fred Drakebc561982001-05-22 17:02:02 +000021 def test_conversions(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000022 self.assertTrue(time.ctime(self.t)
Fred Drakebc561982001-05-22 17:02:02 +000023 == time.asctime(time.localtime(self.t)))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000024 self.assertTrue(int(time.mktime(time.localtime(self.t)))
Guido van Rossume2a383d2007-01-15 16:59:06 +000025 == int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000026
27 def test_sleep(self):
28 time.sleep(1.2)
29
30 def test_strftime(self):
31 tt = time.gmtime(self.t)
32 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
33 'j', 'm', 'M', 'p', 'S',
34 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
35 format = ' %' + directive
36 try:
37 time.strftime(format, tt)
38 except ValueError:
39 self.fail('conversion specifier: %r failed.' % format)
40
Alexander Belopolsky38e29962010-10-01 14:18:49 +000041 def _bounds_checking(self, func=time.strftime):
Brett Cannond1080a32004-03-02 04:38:10 +000042 # Make sure that strftime() checks the bounds of the various parts
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000044
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045 # Check year [1900, max(int)]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000046 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000047 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
48 if time.accept2dyear:
Alexander Belopolsky38e29962010-10-01 14:18:49 +000049 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000050 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000051 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000052 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053 # Check month [1, 12] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000054 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000056 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000057 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 # Check day of month [1, 31] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000059 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000061 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000062 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 # Check hour [0, 23]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000064 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000065 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000066 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000067 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 # Check minute [0, 59]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000069 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000070 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000071 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000072 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000074 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000075 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
76 # C99 only requires allowing for one leap second, but Python's docs say
77 # allow two leap seconds (0..61)
Alexander Belopolsky38e29962010-10-01 14:18:49 +000078 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000079 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
80 # No check for upper-bound day of week;
81 # value forced into range by a ``% 7`` calculation.
82 # Start check at -2 since gettmarg() increments value before taking
83 # modulo.
Alexander Belopolsky38e29962010-10-01 14:18:49 +000084 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000085 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 # Check day of the year [1, 366] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000087 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000089 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000090 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000091
Alexander Belopolsky38e29962010-10-01 14:18:49 +000092 def test_strftime_bounding_check(self):
93 self._bounds_checking(lambda tup: time.strftime('', tup))
94
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 def test_default_values_for_zero(self):
96 # Make sure that using all zeros uses the proper default values.
97 # No test for daylight savings since strftime() does not change output
98 # based on its value.
99 expected = "2000 01 01 00 00 00 1 001"
100 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000101 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000103 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 # Should be able to go round-trip from strftime to strptime without
105 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000106 tt = time.gmtime(self.t)
107 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
108 'j', 'm', 'M', 'p', 'S',
109 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 format = '%' + directive
111 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000112 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000114 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 self.fail("conversion specifier %r failed with '%s' input." %
116 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000117
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000118 def test_strptime_bytes(self):
119 # Make sure only strings are accepted as arguments to strptime.
120 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
121 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
122
Fred Drakebc561982001-05-22 17:02:02 +0000123 def test_asctime(self):
124 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000125
126 # Max year is only limited by the size of C int.
Antoine Pitrou1ec121d2011-01-04 22:54:30 +0000127 sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000128 bigyear = (1 << 8 * sizeof_int - 1) - 1
129 asc = time.asctime((bigyear, 6, 1) + (0,)*6)
130 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
131 self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
Fred Drakebc561982001-05-22 17:02:02 +0000132 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000133 self.assertRaises(TypeError, time.asctime, ())
Fred Drakebc561982001-05-22 17:02:02 +0000134
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000135 def test_asctime_bounding_check(self):
136 self._bounds_checking(time.asctime)
137
Georg Brandle10608c2011-01-02 22:33:43 +0000138 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000139 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
140 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
141 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
142 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Georg Brandle10608c2011-01-02 22:33:43 +0000143 try:
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000144 bigval = time.mktime((10000, 1, 10) + (0,)*6)
Georg Brandl3fb97ae2011-01-04 17:27:13 +0000145 except (ValueError, OverflowError):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000146 # If mktime fails, ctime will fail too. This may happen
147 # on some platforms.
Georg Brandle10608c2011-01-02 22:33:43 +0000148 pass
149 else:
Victor Stinnerb996f742011-01-05 03:58:54 +0000150 self.assertEqual(time.ctime(bigval)[-5:], '10000')
Georg Brandle10608c2011-01-02 22:33:43 +0000151
R. David Murray6ecf76e2010-12-14 01:22:50 +0000152 @unittest.skipIf(not hasattr(time, "tzset"),
153 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000154 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000155
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000156 from os import environ
157
Tim Peters0eadaac2003-04-24 16:02:54 +0000158 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000159 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000160 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000161
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000162 # These formats are correct for 2002, and possibly future years
163 # This format is the 'standard' as documented at:
164 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
165 # They are also documented in the tzset(3) man page on most Unix
166 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000167 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000168 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
169 utc='UTC+0'
170
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000171 org_TZ = environ.get('TZ',None)
172 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000173 # Make sure we can switch to UTC time and results are correct
174 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000175 # Note that altzone is undefined in UTC, as there is no DST
176 environ['TZ'] = eastern
177 time.tzset()
178 environ['TZ'] = utc
179 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000180 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000181 time.gmtime(xmas2002), time.localtime(xmas2002)
182 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000183 self.assertEqual(time.daylight, 0)
184 self.assertEqual(time.timezone, 0)
185 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000186
187 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000188 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000189 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000190 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
191 self.assertEqual(time.tzname, ('EST', 'EDT'))
192 self.assertEqual(len(time.tzname), 2)
193 self.assertEqual(time.daylight, 1)
194 self.assertEqual(time.timezone, 18000)
195 self.assertEqual(time.altzone, 14400)
196 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
197 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000198
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000199 # Now go to the southern hemisphere.
200 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000201 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000202 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
203 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
204 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
205 self.assertEqual(len(time.tzname), 2)
206 self.assertEqual(time.daylight, 1)
207 self.assertEqual(time.timezone, -36000)
208 self.assertEqual(time.altzone, -39600)
209 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000210
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000211 finally:
212 # Repair TZ environment variable in case any other tests
213 # rely on it.
214 if org_TZ is not None:
215 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000216 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000217 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000218 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000219
Tim Peters1b6f7a92004-06-20 02:50:16 +0000220 def test_insane_timestamps(self):
221 # It's possible that some platform maps time_t to double,
222 # and that this test will fail there. This test should
223 # exempt such platforms (provided they return reasonable
224 # results!).
225 for func in time.ctime, time.gmtime, time.localtime:
226 for unreasonable in -1e200, 1e200:
227 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000228
Fred Drakef901abd2004-08-03 17:58:55 +0000229 def test_ctime_without_arg(self):
230 # Not sure how to check the values, since the clock could tick
231 # at any time. Make sure these are at least accepted and
232 # don't raise errors.
233 time.ctime()
234 time.ctime(None)
235
236 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000237 gt0 = time.gmtime()
238 gt1 = time.gmtime(None)
239 t0 = time.mktime(gt0)
240 t1 = time.mktime(gt1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000241 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000242
243 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000244 lt0 = time.localtime()
245 lt1 = time.localtime(None)
246 t0 = time.mktime(lt0)
247 t1 = time.mktime(lt1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000248 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000249
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000250class TestLocale(unittest.TestCase):
251 def setUp(self):
252 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000253
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000254 def tearDown(self):
255 locale.setlocale(locale.LC_ALL, self.oldloc)
256
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000257 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000258 try:
259 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
260 except locale.Error:
261 # skip this test
262 return
263 # This should not cause an exception
264 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
265
266def test_main():
267 support.run_unittest(TimeTestCase, TestLocale)
Fred Drake2e2be372001-09-20 21:33:42 +0000268
269if __name__ == "__main__":
270 test_main()