blob: 4da6703c56c7b3e58d8ffcd8ea694975a4c129e7 [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
Senthil Kumaran792eb5d2011-04-06 14:27:47 +08004import sys
Victor Stinnereeadf5f2017-09-06 01:35:39 +02005import sysconfig
6
7
8# Max year is only limited by the size of C int.
9SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
10TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
Barry Warsawb0c22321996-12-06 23:30:07 +000011
Barry Warsawb0c22321996-12-06 23:30:07 +000012
Fred Drakebc561982001-05-22 17:02:02 +000013class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000014
Fred Drakebc561982001-05-22 17:02:02 +000015 def setUp(self):
16 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000017
Fred Drakebc561982001-05-22 17:02:02 +000018 def test_data_attributes(self):
19 time.altzone
20 time.daylight
21 time.timezone
22 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000023
Fred Drakebc561982001-05-22 17:02:02 +000024 def test_clock(self):
25 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000026
Fred Drakebc561982001-05-22 17:02:02 +000027 def test_conversions(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000028 self.assertTrue(time.ctime(self.t)
Fred Drakebc561982001-05-22 17:02:02 +000029 == time.asctime(time.localtime(self.t)))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000030 self.assertTrue(long(time.mktime(time.localtime(self.t)))
Fred Drakebc561982001-05-22 17:02:02 +000031 == long(self.t))
32
33 def test_sleep(self):
34 time.sleep(1.2)
35
36 def test_strftime(self):
37 tt = time.gmtime(self.t)
38 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
39 'j', 'm', 'M', 'p', 'S',
40 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
41 format = ' %' + directive
42 try:
43 time.strftime(format, tt)
44 except ValueError:
45 self.fail('conversion specifier: %r failed.' % format)
46
Senthil Kumaran792eb5d2011-04-06 14:27:47 +080047 # Issue #10762: Guard against invalid/non-supported format string
48 # so that Python don't crash (Windows crashes when the format string
49 # input to [w]strftime is not kosher.
50 if sys.platform.startswith('win'):
51 with self.assertRaises(ValueError):
52 time.strftime('%f')
53
Victor Stinnereeadf5f2017-09-06 01:35:39 +020054 def _bounds_checking(self, func):
55 # Make sure that strftime() checks the bounds of the various parts
56 # of the time tuple (0 is valid for *all* values).
57
58 # The year field is tested by other test cases above
59
60 # Check month [1, 12] + zero support
61 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
62 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
63 self.assertRaises(ValueError, func,
64 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
65 self.assertRaises(ValueError, func,
66 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
67 # Check day of month [1, 31] + zero support
68 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
69 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
70 self.assertRaises(ValueError, func,
71 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
72 self.assertRaises(ValueError, func,
73 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
74 # Check hour [0, 23]
75 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
76 self.assertRaises(ValueError, func,
77 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
78 self.assertRaises(ValueError, func,
79 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
80 # Check minute [0, 59]
81 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
82 self.assertRaises(ValueError, func,
83 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
84 self.assertRaises(ValueError, func,
85 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
86 # Check second [0, 61]
87 self.assertRaises(ValueError, func,
88 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
89 # C99 only requires allowing for one leap second, but Python's docs say
90 # allow two leap seconds (0..61)
91 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
92 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
93 self.assertRaises(ValueError, func,
94 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
95 # No check for upper-bound day of week;
96 # value forced into range by a ``% 7`` calculation.
97 # Start check at -2 since gettmarg() increments value before taking
98 # modulo.
99 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
100 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
101 self.assertRaises(ValueError, func,
102 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
103 # Check day of the year [1, 366] + zero support
104 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
105 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
106 self.assertRaises(ValueError, func,
107 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
108 self.assertRaises(ValueError, func,
109 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
110
111 def test_strftime_bounding_check(self):
112 self._bounds_checking(lambda tup: time.strftime('', tup))
113
Brett Cannond1080a32004-03-02 04:38:10 +0000114 def test_strftime_bounds_checking(self):
115 # Make sure that strftime() checks the bounds of the various parts
Brett Cannoncaebe222006-07-18 04:41:36 +0000116 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +0000117
Brett Cannoncaebe222006-07-18 04:41:36 +0000118 # Check year [1900, max(int)]
Brett Cannond1080a32004-03-02 04:38:10 +0000119 self.assertRaises(ValueError, time.strftime, '',
120 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
121 if time.accept2dyear:
122 self.assertRaises(ValueError, time.strftime, '',
123 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
124 self.assertRaises(ValueError, time.strftime, '',
125 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000126 # Check month [1, 12] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +0000127 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +0000128 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000129 self.assertRaises(ValueError, time.strftime, '',
130 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000131 # Check day of month [1, 31] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +0000132 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +0000133 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000134 self.assertRaises(ValueError, time.strftime, '',
135 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000136 # Check hour [0, 23]
Brett Cannond1080a32004-03-02 04:38:10 +0000137 self.assertRaises(ValueError, time.strftime, '',
138 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
139 self.assertRaises(ValueError, time.strftime, '',
140 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000141 # Check minute [0, 59]
Brett Cannond1080a32004-03-02 04:38:10 +0000142 self.assertRaises(ValueError, time.strftime, '',
143 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
144 self.assertRaises(ValueError, time.strftime, '',
145 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000146 # Check second [0, 61]
Brett Cannond1080a32004-03-02 04:38:10 +0000147 self.assertRaises(ValueError, time.strftime, '',
148 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
149 # C99 only requires allowing for one leap second, but Python's docs say
150 # allow two leap seconds (0..61)
151 self.assertRaises(ValueError, time.strftime, '',
152 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
153 # No check for upper-bound day of week;
154 # value forced into range by a ``% 7`` calculation.
155 # Start check at -2 since gettmarg() increments value before taking
156 # modulo.
157 self.assertRaises(ValueError, time.strftime, '',
158 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +0000159 # Check day of the year [1, 366] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +0000160 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +0000161 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000162 self.assertRaises(ValueError, time.strftime, '',
163 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000164
Brett Cannoncaebe222006-07-18 04:41:36 +0000165 def test_default_values_for_zero(self):
166 # Make sure that using all zeros uses the proper default values.
167 # No test for daylight savings since strftime() does not change output
168 # based on its value.
169 expected = "2000 01 01 00 00 00 1 001"
170 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melotti2623a372010-11-21 13:34:58 +0000171 self.assertEqual(expected, result)
Brett Cannoncaebe222006-07-18 04:41:36 +0000172
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000173 def test_strptime(self):
Brett Cannone0426012006-09-23 19:53:20 +0000174 # Should be able to go round-trip from strftime to strptime without
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200175 # raising an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000176 tt = time.gmtime(self.t)
177 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
178 'j', 'm', 'M', 'p', 'S',
179 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Brett Cannone0426012006-09-23 19:53:20 +0000180 format = '%' + directive
181 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000182 try:
Brett Cannone0426012006-09-23 19:53:20 +0000183 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000184 except ValueError:
Brett Cannone0426012006-09-23 19:53:20 +0000185 self.fail("conversion specifier %r failed with '%s' input." %
186 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000187
Fred Drakebc561982001-05-22 17:02:02 +0000188 def test_asctime(self):
189 time.asctime(time.gmtime(self.t))
190 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolsky8009e8e2011-01-02 23:23:54 +0000191 self.assertRaises(TypeError, time.asctime, ())
Victor Stinnereeadf5f2017-09-06 01:35:39 +0200192
193 # Max year is only limited by the size of C int.
194 asc = time.asctime((TIME_MAXYEAR, 6, 1) + (0,) * 6)
195 self.assertEqual(asc[-len(str(TIME_MAXYEAR)):], str(TIME_MAXYEAR))
196 self.assertRaises(OverflowError, time.asctime,
197 (TIME_MAXYEAR + 1,) + (0,) * 8)
198 self.assertRaises(TypeError, time.asctime, 0)
199 self.assertRaises(TypeError, time.asctime, ())
200 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000201
R. David Murrayfe3b4372010-12-14 01:28:59 +0000202 @unittest.skipIf(not hasattr(time, "tzset"),
203 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000204 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000205
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000206 from os import environ
207
Tim Peters0eadaac2003-04-24 16:02:54 +0000208 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000209 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000210 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000211
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000212 # These formats are correct for 2002, and possibly future years
213 # This format is the 'standard' as documented at:
214 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
215 # They are also documented in the tzset(3) man page on most Unix
216 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000217 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000218 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
219 utc='UTC+0'
220
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000221 org_TZ = environ.get('TZ',None)
222 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000223 # Make sure we can switch to UTC time and results are correct
224 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000225 # Note that altzone is undefined in UTC, as there is no DST
226 environ['TZ'] = eastern
227 time.tzset()
228 environ['TZ'] = utc
229 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000230 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000231 time.gmtime(xmas2002), time.localtime(xmas2002)
232 )
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000233 self.assertEqual(time.daylight, 0)
234 self.assertEqual(time.timezone, 0)
235 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000236
237 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000238 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000239 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000240 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
241 self.assertEqual(time.tzname, ('EST', 'EDT'))
242 self.assertEqual(len(time.tzname), 2)
243 self.assertEqual(time.daylight, 1)
244 self.assertEqual(time.timezone, 18000)
245 self.assertEqual(time.altzone, 14400)
246 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
247 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000248
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000249 # Now go to the southern hemisphere.
250 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000251 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000252 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
Victor Stinner0de2aae2011-12-08 00:32:51 +0100253
254 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
255 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" on some
256 # operating systems (e.g. FreeBSD), which is wrong. See for example
257 # this bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
258 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000259 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
260 self.assertEqual(len(time.tzname), 2)
261 self.assertEqual(time.daylight, 1)
262 self.assertEqual(time.timezone, -36000)
263 self.assertEqual(time.altzone, -39600)
264 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000265
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000266 finally:
267 # Repair TZ environment variable in case any other tests
268 # rely on it.
269 if org_TZ is not None:
270 environ['TZ'] = org_TZ
271 elif environ.has_key('TZ'):
272 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000273 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000274
Tim Peters1b6f7a92004-06-20 02:50:16 +0000275 def test_insane_timestamps(self):
276 # It's possible that some platform maps time_t to double,
277 # and that this test will fail there. This test should
278 # exempt such platforms (provided they return reasonable
279 # results!).
280 for func in time.ctime, time.gmtime, time.localtime:
281 for unreasonable in -1e200, 1e200:
282 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000283
Fred Drakef901abd2004-08-03 17:58:55 +0000284 def test_ctime_without_arg(self):
285 # Not sure how to check the values, since the clock could tick
286 # at any time. Make sure these are at least accepted and
287 # don't raise errors.
288 time.ctime()
289 time.ctime(None)
290
291 def test_gmtime_without_arg(self):
Neal Norwitzde7f5022006-06-15 05:55:49 +0000292 gt0 = time.gmtime()
293 gt1 = time.gmtime(None)
294 t0 = time.mktime(gt0)
295 t1 = time.mktime(gt1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000296 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000297
298 def test_localtime_without_arg(self):
Neal Norwitzde7f5022006-06-15 05:55:49 +0000299 lt0 = time.localtime()
300 lt1 = time.localtime(None)
301 t0 = time.mktime(lt0)
302 t1 = time.mktime(lt1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000303 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000304
Alexander Belopolsky6233b362011-02-15 15:51:17 +0000305 def test_mktime(self):
306 # Issue #1726687
307 for t in (-2, -1, 0, 1):
308 try:
309 tt = time.localtime(t)
310 except (OverflowError, ValueError):
311 pass
Alexander Belopolskyf9ad7d42011-02-15 16:01:11 +0000312 else:
313 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky6233b362011-02-15 15:51:17 +0000314
315
Fred Drake2e2be372001-09-20 21:33:42 +0000316def test_main():
317 test_support.run_unittest(TimeTestCase)
318
319
320if __name__ == "__main__":
321 test_main()