blob: 65b273557f3995f0fdf40c23b9f1584eb25a0313 [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
Senthil Kumaran8f377a32011-04-06 12:54:06 +08005import sys
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):
Georg Brandlab91fde2009-08-13 08:51:18 +000022 self.assertTrue(time.ctime(self.t)
Fred Drakebc561982001-05-22 17:02:02 +000023 == time.asctime(time.localtime(self.t)))
Georg Brandlab91fde2009-08-13 08:51:18 +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
Senthil Kumaran8f377a32011-04-06 12:54:06 +080041 # Issue #10762: Guard against invalid/non-supported format string
42 # so that Python don't crash (Windows crashes when the format string
43 # input to [w]strftime is not kosher.
44 if sys.platform.startswith('win'):
45 with self.assertRaises(ValueError):
46 time.strftime('%f')
47
Brett Cannond1080a32004-03-02 04:38:10 +000048 def test_strftime_bounds_checking(self):
49 # Make sure that strftime() checks the bounds of the various parts
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000051
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 # Check year [1900, max(int)]
Brett Cannond1080a32004-03-02 04:38:10 +000053 self.assertRaises(ValueError, time.strftime, '',
54 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
55 if time.accept2dyear:
56 self.assertRaises(ValueError, time.strftime, '',
57 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
58 self.assertRaises(ValueError, time.strftime, '',
59 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060 # Check month [1, 12] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000061 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000063 self.assertRaises(ValueError, time.strftime, '',
64 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065 # Check day of month [1, 31] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000066 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000068 self.assertRaises(ValueError, time.strftime, '',
69 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070 # Check hour [0, 23]
Brett Cannond1080a32004-03-02 04:38:10 +000071 self.assertRaises(ValueError, time.strftime, '',
72 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
73 self.assertRaises(ValueError, time.strftime, '',
74 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 # Check minute [0, 59]
Brett Cannond1080a32004-03-02 04:38:10 +000076 self.assertRaises(ValueError, time.strftime, '',
77 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
78 self.assertRaises(ValueError, time.strftime, '',
79 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080 # Check second [0, 61]
Brett Cannond1080a32004-03-02 04:38:10 +000081 self.assertRaises(ValueError, time.strftime, '',
82 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
83 # C99 only requires allowing for one leap second, but Python's docs say
84 # allow two leap seconds (0..61)
85 self.assertRaises(ValueError, time.strftime, '',
86 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
87 # No check for upper-bound day of week;
88 # value forced into range by a ``% 7`` calculation.
89 # Start check at -2 since gettmarg() increments value before taking
90 # modulo.
91 self.assertRaises(ValueError, time.strftime, '',
92 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 # Check day of the year [1, 366] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000094 self.assertRaises(ValueError, time.strftime, '',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000096 self.assertRaises(ValueError, time.strftime, '',
97 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098 # Check daylight savings flag [-1, 1]
Brett Cannond1080a32004-03-02 04:38:10 +000099 self.assertRaises(ValueError, time.strftime, '',
100 (1900, 1, 1, 0, 0, 0, 0, 1, -2))
101 self.assertRaises(ValueError, time.strftime, '',
102 (1900, 1, 1, 0, 0, 0, 0, 1, 2))
103
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 def test_default_values_for_zero(self):
105 # Make sure that using all zeros uses the proper default values.
106 # No test for daylight savings since strftime() does not change output
107 # based on its value.
108 expected = "2000 01 01 00 00 00 1 001"
109 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000110 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000112 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 # Should be able to go round-trip from strftime to strptime without
114 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000115 tt = time.gmtime(self.t)
116 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
117 'j', 'm', 'M', 'p', 'S',
118 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 format = '%' + directive
120 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000121 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000123 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 self.fail("conversion specifier %r failed with '%s' input." %
125 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000126
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000127 def test_strptime_bytes(self):
128 # Make sure only strings are accepted as arguments to strptime.
129 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
130 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
131
Fred Drakebc561982001-05-22 17:02:02 +0000132 def test_asctime(self):
133 time.asctime(time.gmtime(self.t))
134 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskyd4bf48b2011-01-02 23:09:41 +0000135 self.assertRaises(TypeError, time.asctime, ())
136 # XXX: Posix compiant asctime should refuse to convert
137 # year > 9999, but Linux implementation does not.
138 # self.assertRaises(ValueError, time.asctime,
139 # (12345, 1, 0, 0, 0, 0, 0, 0, 0))
140 # XXX: For now, just make sure we don't have a crash:
141 try:
Alexander Belopolsky622eb172011-01-03 17:06:39 +0000142 time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
Alexander Belopolskyd4bf48b2011-01-02 23:09:41 +0000143 except ValueError:
144 pass
Fred Drakebc561982001-05-22 17:02:02 +0000145
R. David Murrayf3d082c2010-12-14 01:25:30 +0000146 @unittest.skipIf(not hasattr(time, "tzset"),
147 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000148 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000149
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000150 from os import environ
151
Tim Peters0eadaac2003-04-24 16:02:54 +0000152 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000153 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000154 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000155
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000156 # These formats are correct for 2002, and possibly future years
157 # This format is the 'standard' as documented at:
158 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
159 # They are also documented in the tzset(3) man page on most Unix
160 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000161 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000162 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
163 utc='UTC+0'
164
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000165 org_TZ = environ.get('TZ',None)
166 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000167 # Make sure we can switch to UTC time and results are correct
168 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000169 # Note that altzone is undefined in UTC, as there is no DST
170 environ['TZ'] = eastern
171 time.tzset()
172 environ['TZ'] = utc
173 time.tzset()
Georg Brandlab91fde2009-08-13 08:51:18 +0000174 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000175 time.gmtime(xmas2002), time.localtime(xmas2002)
176 )
Georg Brandlab91fde2009-08-13 08:51:18 +0000177 self.assertEqual(time.daylight, 0)
178 self.assertEqual(time.timezone, 0)
179 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000180
181 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000182 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000183 time.tzset()
Georg Brandlab91fde2009-08-13 08:51:18 +0000184 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
185 self.assertEqual(time.tzname, ('EST', 'EDT'))
186 self.assertEqual(len(time.tzname), 2)
187 self.assertEqual(time.daylight, 1)
188 self.assertEqual(time.timezone, 18000)
189 self.assertEqual(time.altzone, 14400)
190 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
191 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000192
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000193 # Now go to the southern hemisphere.
194 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000195 time.tzset()
Georg Brandlab91fde2009-08-13 08:51:18 +0000196 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
197 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
198 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
199 self.assertEqual(len(time.tzname), 2)
200 self.assertEqual(time.daylight, 1)
201 self.assertEqual(time.timezone, -36000)
202 self.assertEqual(time.altzone, -39600)
203 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000204
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000205 finally:
206 # Repair TZ environment variable in case any other tests
207 # rely on it.
208 if org_TZ is not None:
209 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000210 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000211 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000212 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000213
Tim Peters1b6f7a92004-06-20 02:50:16 +0000214 def test_insane_timestamps(self):
215 # It's possible that some platform maps time_t to double,
216 # and that this test will fail there. This test should
217 # exempt such platforms (provided they return reasonable
218 # results!).
219 for func in time.ctime, time.gmtime, time.localtime:
220 for unreasonable in -1e200, 1e200:
221 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000222
Fred Drakef901abd2004-08-03 17:58:55 +0000223 def test_ctime_without_arg(self):
224 # Not sure how to check the values, since the clock could tick
225 # at any time. Make sure these are at least accepted and
226 # don't raise errors.
227 time.ctime()
228 time.ctime(None)
229
230 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000231 gt0 = time.gmtime()
232 gt1 = time.gmtime(None)
233 t0 = time.mktime(gt0)
234 t1 = time.mktime(gt1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000235 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000236
237 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 lt0 = time.localtime()
239 lt1 = time.localtime(None)
240 t0 = time.mktime(lt0)
241 t1 = time.mktime(lt1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000242 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000243
Alexander Belopolsky4fb96f42011-02-15 15:40:59 +0000244 def test_mktime(self):
245 # Issue #1726687
246 for t in (-2, -1, 0, 1):
247 try:
248 tt = time.localtime(t)
249 except (OverflowError, ValueError):
250 pass
Alexander Belopolsky622ce122011-02-15 15:58:04 +0000251 else:
252 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky4fb96f42011-02-15 15:40:59 +0000253
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000254class TestLocale(unittest.TestCase):
255 def setUp(self):
256 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000257
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000258 def tearDown(self):
259 locale.setlocale(locale.LC_ALL, self.oldloc)
260
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000261 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000262 try:
263 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
264 except locale.Error:
265 # skip this test
266 return
267 # This should not cause an exception
268 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
269
270def test_main():
271 support.run_unittest(TimeTestCase, TestLocale)
Fred Drake2e2be372001-09-20 21:33:42 +0000272
273if __name__ == "__main__":
274 test_main()