blob: afdf43e55d372d589f606c26d3afee068d5fef1d [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
Senthil Kumaran8f377a32011-04-06 12:54:06 +08006import sys
Alexander Belopolskyc64708a2011-01-07 19:59:19 +00007import warnings
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
R. David Murray6ecf76e2010-12-14 01:22:50 +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 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))
253 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
254 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
255 self.assertEqual(len(time.tzname), 2)
256 self.assertEqual(time.daylight, 1)
257 self.assertEqual(time.timezone, -36000)
258 self.assertEqual(time.altzone, -39600)
259 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000260
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000261 finally:
262 # Repair TZ environment variable in case any other tests
263 # rely on it.
264 if org_TZ is not None:
265 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000266 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000267 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000268 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000269
Tim Peters1b6f7a92004-06-20 02:50:16 +0000270 def test_insane_timestamps(self):
271 # It's possible that some platform maps time_t to double,
272 # and that this test will fail there. This test should
273 # exempt such platforms (provided they return reasonable
274 # results!).
275 for func in time.ctime, time.gmtime, time.localtime:
276 for unreasonable in -1e200, 1e200:
277 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000278
Fred Drakef901abd2004-08-03 17:58:55 +0000279 def test_ctime_without_arg(self):
280 # Not sure how to check the values, since the clock could tick
281 # at any time. Make sure these are at least accepted and
282 # don't raise errors.
283 time.ctime()
284 time.ctime(None)
285
286 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 gt0 = time.gmtime()
288 gt1 = time.gmtime(None)
289 t0 = time.mktime(gt0)
290 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000291 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000292
293 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 lt0 = time.localtime()
295 lt1 = time.localtime(None)
296 t0 = time.mktime(lt0)
297 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000298 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000299
Florent Xicluna050c7e62011-11-01 16:58:54 +0100300 # XXX run last to work around issue #13309 on Gentoo
Florent Xicluna725af4d2011-11-01 17:42:24 +0100301 def test_zzz_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100302 # Issue #1726687
303 for t in (-2, -1, 0, 1):
304 try:
305 tt = time.localtime(t)
306 except (OverflowError, ValueError):
307 pass
308 else:
309 self.assertEqual(time.mktime(tt), t)
Florent Xicluna050c7e62011-11-01 16:58:54 +0100310 tt = time.gmtime(self.t)
311 tzname = time.strftime('%Z', tt)
312 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100313 # It may not be possible to reliably make mktime return error
314 # on all platfom. This will make sure that no other exception
315 # than OverflowError is raised for an extreme value.
316 try:
317 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
318 except OverflowError:
319 pass
Florent Xicluna725af4d2011-11-01 17:42:24 +0100320 msg = "Issue #13309: the '%Z' specifier reports erroneous timezone"
321 msg += " after time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))."
Florent Xicluna050c7e62011-11-01 16:58:54 +0100322 self.assertEqual(time.strftime('%Z', tt), tzname, msg)
Florent Xiclunabceb5282011-11-01 14:11:34 +0100323
324
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000325class TestLocale(unittest.TestCase):
326 def setUp(self):
327 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000328
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000329 def tearDown(self):
330 locale.setlocale(locale.LC_ALL, self.oldloc)
331
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000332 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000333 try:
334 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
335 except locale.Error:
336 # skip this test
337 return
338 # This should not cause an exception
339 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
340
Victor Stinner73ea29c2011-01-08 01:56:31 +0000341
342class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000343 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000344 raise NotImplementedError()
345
346class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100347 _format = '%d'
348
Victor Stinner73ea29c2011-01-08 01:56:31 +0000349 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000350 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000351
Victor Stinner73ea29c2011-01-08 01:56:31 +0000352 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000353 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000354 self.assertEqual(self.yearstr(12345), '12345')
355 self.assertEqual(self.yearstr(123456789), '123456789')
356
357class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100358
359 # Issue 13305: For years < 1000, the value is not always
360 # padded to 4 digits across platforms. The C standard
361 # assumes year >= 1900, so it does not specify the number
362 # of digits.
363
364 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
365 _format = '%04d'
366 else:
367 _format = '%d'
368
Victor Stinner73ea29c2011-01-08 01:56:31 +0000369 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100370 return time.strftime('%Y', (y,) + (0,) * 8)
371
372 def test_4dyear(self):
373 # Check that we can return the zero padded value.
374 if self._format == '%04d':
375 self.test_year('%04d')
376 else:
377 def year4d(y):
378 return time.strftime('%4Y', (y,) + (0,) * 8)
379 self.test_year('%04d', func=year4d)
380
Florent Xiclunabceb5282011-11-01 14:11:34 +0100381 def skip_if_not_supported(y):
382 msg = "strftime() is limited to [1; 9999] with Visual Studio"
383 # Check that it doesn't crash for year > 9999
384 try:
385 time.strftime('%Y', (y,) + (0,) * 8)
386 except ValueError:
387 cond = False
388 else:
389 cond = True
390 return unittest.skipUnless(cond, msg)
391
392 @skip_if_not_supported(10000)
393 def test_large_year(self):
394 return super().test_large_year()
395
396 @skip_if_not_supported(0)
397 def test_negative(self):
398 return super().test_negative()
399
400 del skip_if_not_supported
401
402
Florent Xicluna49ce0682011-11-01 12:56:14 +0100403class _Test4dYear(_BaseYearTest):
404 _format = '%d'
405
406 def test_year(self, fmt=None, func=None):
407 fmt = fmt or self._format
408 func = func or self.yearstr
409 self.assertEqual(func(1), fmt % 1)
410 self.assertEqual(func(68), fmt % 68)
411 self.assertEqual(func(69), fmt % 69)
412 self.assertEqual(func(99), fmt % 99)
413 self.assertEqual(func(999), fmt % 999)
414 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000415
416 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100417 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100418 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100419 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
420 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000421
Victor Stinner301f1212011-01-08 03:06:52 +0000422 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100423 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000424 self.assertEqual(self.yearstr(-1234), '-1234')
425 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100426 self.assertEqual(self.yearstr(-123456789), str(-123456789))
427 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100428 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
429 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
430 # Skip the value test, but check that no error is raised
431 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100432 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100433 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000434
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000435
Victor Stinner73ea29c2011-01-08 01:56:31 +0000436class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
437 pass
438
439class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000440 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000441
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000442
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000443def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000444 support.run_unittest(
445 TimeTestCase,
446 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000447 TestAsctime4dyear,
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400448 TestStrftime4dyear)
Fred Drake2e2be372001-09-20 21:33:42 +0000449
450if __name__ == "__main__":
451 test_main()