blob: a89c511c691cd4ceef30cd228eb8068ddcf3bbb1 [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
Victor Stinner4195b5c2012-02-08 23:03:19 +01004import locale
5import sysconfig
6import sys
7import platform
Barry Warsawb0c22321996-12-06 23:30:07 +00008
Florent Xiclunabceb5282011-11-01 14:11:34 +01009# Max year is only limited by the size of C int.
10SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
11TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
12TIME_MINYEAR = -TIME_MAXYEAR - 1
13
14
Fred Drakebc561982001-05-22 17:02:02 +000015class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000016
Fred Drakebc561982001-05-22 17:02:02 +000017 def setUp(self):
18 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000019
Fred Drakebc561982001-05-22 17:02:02 +000020 def test_data_attributes(self):
21 time.altzone
22 time.daylight
23 time.timezone
24 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000025
Fred Drakebc561982001-05-22 17:02:02 +000026 def test_clock(self):
27 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000028
Victor Stinnere0be4232011-10-25 13:06:09 +020029 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
30 'need time.clock_gettime()')
31 def test_clock_realtime(self):
32 time.clock_gettime(time.CLOCK_REALTIME)
33
34 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
35 'need time.clock_gettime()')
36 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
37 'need time.CLOCK_MONOTONIC')
38 def test_clock_monotonic(self):
39 a = time.clock_gettime(time.CLOCK_MONOTONIC)
40 b = time.clock_gettime(time.CLOCK_MONOTONIC)
41 self.assertLessEqual(a, b)
42
43 @unittest.skipUnless(hasattr(time, 'clock_getres'),
44 'need time.clock_getres()')
45 def test_clock_getres(self):
46 res = time.clock_getres(time.CLOCK_REALTIME)
47 self.assertGreater(res, 0.0)
48 self.assertLessEqual(res, 1.0)
49
Fred Drakebc561982001-05-22 17:02:02 +000050 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000051 self.assertEqual(time.ctime(self.t),
52 time.asctime(time.localtime(self.t)))
53 self.assertEqual(int(time.mktime(time.localtime(self.t))),
54 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000055
56 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +020057 self.assertRaises(ValueError, time.sleep, -2)
58 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +000059 time.sleep(1.2)
60
61 def test_strftime(self):
62 tt = time.gmtime(self.t)
63 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
64 'j', 'm', 'M', 'p', 'S',
65 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
66 format = ' %' + directive
67 try:
68 time.strftime(format, tt)
69 except ValueError:
70 self.fail('conversion specifier: %r failed.' % format)
71
Senthil Kumaran8f377a32011-04-06 12:54:06 +080072 # Issue #10762: Guard against invalid/non-supported format string
73 # so that Python don't crash (Windows crashes when the format string
74 # input to [w]strftime is not kosher.
75 if sys.platform.startswith('win'):
76 with self.assertRaises(ValueError):
77 time.strftime('%f')
78
Florent Xicluna49ce0682011-11-01 12:56:14 +010079 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +000080 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +010081 # of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000082
Victor Stinner73ea29c2011-01-08 01:56:31 +000083 # The year field is tested by other test cases above
84
Thomas Wouters0e3f5912006-08-11 14:57:12 +000085 # Check month [1, 12] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010086 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
87 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000088 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000090 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000091 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010093 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
94 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000095 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000097 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000098 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100100 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000101 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000102 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000103 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000104 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100106 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000107 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000108 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000109 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000110 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000112 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000113 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
114 # C99 only requires allowing for one leap second, but Python's docs say
115 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100116 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
117 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000118 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000119 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
120 # No check for upper-bound day of week;
121 # value forced into range by a ``% 7`` calculation.
122 # Start check at -2 since gettmarg() increments value before taking
123 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100124 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
125 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000126 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000127 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100129 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
130 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000131 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000132 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000133 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000134 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000135
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000136 def test_strftime_bounding_check(self):
137 self._bounds_checking(lambda tup: time.strftime('', tup))
138
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400140 # Make sure that using all zeros uses the proper default
141 # values. No test for daylight savings since strftime() does
142 # not change output based on its value and no test for year
143 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000145 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400146 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000147 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000149 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 # Should be able to go round-trip from strftime to strptime without
151 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000152 tt = time.gmtime(self.t)
153 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
154 'j', 'm', 'M', 'p', 'S',
155 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156 format = '%' + directive
157 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000158 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000160 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000161 self.fail("conversion specifier %r failed with '%s' input." %
162 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000163
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000164 def test_strptime_bytes(self):
165 # Make sure only strings are accepted as arguments to strptime.
166 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
167 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
168
Fred Drakebc561982001-05-22 17:02:02 +0000169 def test_asctime(self):
170 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000171
172 # Max year is only limited by the size of C int.
Florent Xiclunabceb5282011-11-01 14:11:34 +0100173 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
174 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
175 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
176 self.assertRaises(OverflowError, time.asctime,
177 (TIME_MAXYEAR + 1,) + (0,) * 8)
178 self.assertRaises(OverflowError, time.asctime,
179 (TIME_MINYEAR - 1,) + (0,) * 8)
Fred Drakebc561982001-05-22 17:02:02 +0000180 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000181 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000182 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000183
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000184 def test_asctime_bounding_check(self):
185 self._bounds_checking(time.asctime)
186
Georg Brandle10608c2011-01-02 22:33:43 +0000187 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000188 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
189 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
190 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
191 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000192 for year in [-100, 100, 1000, 2000, 10000]:
193 try:
194 testval = time.mktime((year, 1, 10) + (0,)*6)
195 except (ValueError, OverflowError):
196 # If mktime fails, ctime will fail too. This may happen
197 # on some platforms.
198 pass
199 else:
200 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000201
Florent Xiclunae54371e2011-11-11 18:59:30 +0100202 @unittest.skipUnless(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 Petersonc9c0f202009-06-30 23:06:06 +0000230 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000231 time.gmtime(xmas2002), time.localtime(xmas2002)
232 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +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 Petersonc9c0f202009-06-30 23:06:06 +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 Petersonc9c0f202009-06-30 23:06:06 +0000252 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
Victor Stinner0cd47902011-12-08 00:32:51 +0100253
254 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100255 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
256 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
257 # on some operating systems (e.g. FreeBSD), which is wrong. See for
258 # example this bug:
259 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
Victor Stinner0cd47902011-12-08 00:32:51 +0100260 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100261 self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000262 self.assertEqual(len(time.tzname), 2)
263 self.assertEqual(time.daylight, 1)
264 self.assertEqual(time.timezone, -36000)
265 self.assertEqual(time.altzone, -39600)
266 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000267
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000268 finally:
269 # Repair TZ environment variable in case any other tests
270 # rely on it.
271 if org_TZ is not None:
272 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000273 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000274 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000275 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000276
Tim Peters1b6f7a92004-06-20 02:50:16 +0000277 def test_insane_timestamps(self):
278 # It's possible that some platform maps time_t to double,
279 # and that this test will fail there. This test should
280 # exempt such platforms (provided they return reasonable
281 # results!).
282 for func in time.ctime, time.gmtime, time.localtime:
283 for unreasonable in -1e200, 1e200:
284 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000285
Fred Drakef901abd2004-08-03 17:58:55 +0000286 def test_ctime_without_arg(self):
287 # Not sure how to check the values, since the clock could tick
288 # at any time. Make sure these are at least accepted and
289 # don't raise errors.
290 time.ctime()
291 time.ctime(None)
292
293 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 gt0 = time.gmtime()
295 gt1 = time.gmtime(None)
296 t0 = time.mktime(gt0)
297 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000298 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000299
300 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301 lt0 = time.localtime()
302 lt1 = time.localtime(None)
303 t0 = time.mktime(lt0)
304 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000305 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000306
Florent Xiclunae54371e2011-11-11 18:59:30 +0100307 def test_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100308 # Issue #1726687
309 for t in (-2, -1, 0, 1):
310 try:
311 tt = time.localtime(t)
Victor Stinner2cbae982012-01-27 00:50:33 +0100312 except (OverflowError, OSError):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100313 pass
314 else:
315 self.assertEqual(time.mktime(tt), t)
Florent Xiclunae54371e2011-11-11 18:59:30 +0100316
317 # Issue #13309: passing extreme values to mktime() or localtime()
318 # borks the glibc's internal timezone data.
319 @unittest.skipUnless(platform.libc_ver()[0] != 'glibc',
320 "disabled because of a bug in glibc. Issue #13309")
321 def test_mktime_error(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100322 # It may not be possible to reliably make mktime return error
323 # on all platfom. This will make sure that no other exception
324 # than OverflowError is raised for an extreme value.
Florent Xiclunae54371e2011-11-11 18:59:30 +0100325 tt = time.gmtime(self.t)
326 tzname = time.strftime('%Z', tt)
327 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100328 try:
329 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
330 except OverflowError:
331 pass
Florent Xiclunae54371e2011-11-11 18:59:30 +0100332 self.assertEqual(time.strftime('%Z', tt), tzname)
Florent Xiclunabceb5282011-11-01 14:11:34 +0100333
Victor Stinner8b302012012-02-07 23:29:46 +0100334 @unittest.skipUnless(hasattr(time, 'monotonic'),
335 'need time.monotonic()')
336 def test_monotonic(self):
337 t1 = time.monotonic()
338 t2 = time.monotonic()
339 self.assertGreaterEqual(t2, t1)
340
341 t1 = time.monotonic()
342 time.sleep(0.1)
343 t2 = time.monotonic()
344 dt = t2 - t1
345 self.assertGreater(t2, t1)
346 self.assertAlmostEqual(dt, 0.1, delta=0.2)
347
Victor Stinnerb94b2662012-01-18 01:50:21 +0100348 def test_wallclock(self):
Victor Stinnerb94b2662012-01-18 01:50:21 +0100349 t1 = time.wallclock()
Victor Stinner2dd254d2012-01-20 02:24:18 +0100350 t2 = time.wallclock()
Victor Stinner8b302012012-02-07 23:29:46 +0100351 # may fail if the system clock was changed
Victor Stinner4ccc7272012-01-23 23:48:41 +0100352 self.assertGreaterEqual(t2, t1)
Victor Stinner2dd254d2012-01-20 02:24:18 +0100353
354 t1 = time.wallclock()
355 time.sleep(0.1)
356 t2 = time.wallclock()
Victor Stinner2dd254d2012-01-20 02:24:18 +0100357 dt = t2 - t1
Victor Stinner8b302012012-02-07 23:29:46 +0100358 # may fail if the system clock was changed
359 self.assertGreater(t2, t1)
Victor Stinner2dd254d2012-01-20 02:24:18 +0100360 self.assertAlmostEqual(dt, 0.1, delta=0.2)
Antoine Pitrou391166f2012-01-18 22:35:21 +0100361
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100362 def test_localtime_failure(self):
363 # Issue #13847: check for localtime() failure
Victor Stinner53d36452012-01-27 01:03:25 +0100364 invalid_time_t = None
365 for time_t in (-1, 2**30, 2**33, 2**60):
366 try:
367 time.localtime(time_t)
368 except ValueError as err:
369 if str(err) == "timestamp out of range for platform time_t":
370 self.skipTest("need 64-bit time_t")
371 else:
372 raise
373 except OSError:
374 invalid_time_t = time_t
375 break
376 if invalid_time_t is None:
377 self.skipTest("unable to find an invalid time_t value")
378
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100379 self.assertRaises(OSError, time.localtime, invalid_time_t)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100380 self.assertRaises(OSError, time.ctime, invalid_time_t)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100381
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000382class TestLocale(unittest.TestCase):
383 def setUp(self):
384 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000385
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000386 def tearDown(self):
387 locale.setlocale(locale.LC_ALL, self.oldloc)
388
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000389 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000390 try:
391 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
392 except locale.Error:
393 # skip this test
394 return
395 # This should not cause an exception
396 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
397
Victor Stinner73ea29c2011-01-08 01:56:31 +0000398
399class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000400 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000401 raise NotImplementedError()
402
403class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100404 _format = '%d'
405
Victor Stinner73ea29c2011-01-08 01:56:31 +0000406 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000407 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000408
Victor Stinner73ea29c2011-01-08 01:56:31 +0000409 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000410 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000411 self.assertEqual(self.yearstr(12345), '12345')
412 self.assertEqual(self.yearstr(123456789), '123456789')
413
414class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100415
416 # Issue 13305: For years < 1000, the value is not always
417 # padded to 4 digits across platforms. The C standard
418 # assumes year >= 1900, so it does not specify the number
419 # of digits.
420
421 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
422 _format = '%04d'
423 else:
424 _format = '%d'
425
Victor Stinner73ea29c2011-01-08 01:56:31 +0000426 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100427 return time.strftime('%Y', (y,) + (0,) * 8)
428
429 def test_4dyear(self):
430 # Check that we can return the zero padded value.
431 if self._format == '%04d':
432 self.test_year('%04d')
433 else:
434 def year4d(y):
435 return time.strftime('%4Y', (y,) + (0,) * 8)
436 self.test_year('%04d', func=year4d)
437
Florent Xiclunabceb5282011-11-01 14:11:34 +0100438 def skip_if_not_supported(y):
439 msg = "strftime() is limited to [1; 9999] with Visual Studio"
440 # Check that it doesn't crash for year > 9999
441 try:
442 time.strftime('%Y', (y,) + (0,) * 8)
443 except ValueError:
444 cond = False
445 else:
446 cond = True
447 return unittest.skipUnless(cond, msg)
448
449 @skip_if_not_supported(10000)
450 def test_large_year(self):
451 return super().test_large_year()
452
453 @skip_if_not_supported(0)
454 def test_negative(self):
455 return super().test_negative()
456
457 del skip_if_not_supported
458
459
Florent Xicluna49ce0682011-11-01 12:56:14 +0100460class _Test4dYear(_BaseYearTest):
461 _format = '%d'
462
463 def test_year(self, fmt=None, func=None):
464 fmt = fmt or self._format
465 func = func or self.yearstr
466 self.assertEqual(func(1), fmt % 1)
467 self.assertEqual(func(68), fmt % 68)
468 self.assertEqual(func(69), fmt % 69)
469 self.assertEqual(func(99), fmt % 99)
470 self.assertEqual(func(999), fmt % 999)
471 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000472
473 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100474 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100475 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100476 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
477 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000478
Victor Stinner301f1212011-01-08 03:06:52 +0000479 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100480 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000481 self.assertEqual(self.yearstr(-1234), '-1234')
482 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100483 self.assertEqual(self.yearstr(-123456789), str(-123456789))
484 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100485 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
486 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
487 # Skip the value test, but check that no error is raised
488 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100489 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100490 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000491
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000492
Victor Stinner73ea29c2011-01-08 01:56:31 +0000493class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
494 pass
495
496class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000497 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000498
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000499
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000500def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000501 support.run_unittest(
502 TimeTestCase,
503 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000504 TestAsctime4dyear,
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400505 TestStrftime4dyear)
Fred Drake2e2be372001-09-20 21:33:42 +0000506
507if __name__ == "__main__":
508 test_main()