blob: dd630bde243f4a47ad8981a6372238cfd8f5bc31 [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
Antoine Pitroub0a1d622011-11-11 03:04:35 +01008import platform
Barry Warsawb0c22321996-12-06 23:30:07 +00009
Florent Xiclunabceb5282011-11-01 14:11:34 +010010# Max year is only limited by the size of C int.
11SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
12TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
13TIME_MINYEAR = -TIME_MAXYEAR - 1
14
15
Fred Drakebc561982001-05-22 17:02:02 +000016class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000017
Fred Drakebc561982001-05-22 17:02:02 +000018 def setUp(self):
19 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000020
Fred Drakebc561982001-05-22 17:02:02 +000021 def test_data_attributes(self):
22 time.altzone
23 time.daylight
24 time.timezone
25 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000026
Fred Drakebc561982001-05-22 17:02:02 +000027 def test_clock(self):
28 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000029
Victor Stinnere0be4232011-10-25 13:06:09 +020030 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
31 'need time.clock_gettime()')
32 def test_clock_realtime(self):
33 time.clock_gettime(time.CLOCK_REALTIME)
34
35 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
36 'need time.clock_gettime()')
37 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
38 'need time.CLOCK_MONOTONIC')
39 def test_clock_monotonic(self):
40 a = time.clock_gettime(time.CLOCK_MONOTONIC)
41 b = time.clock_gettime(time.CLOCK_MONOTONIC)
42 self.assertLessEqual(a, b)
43
44 @unittest.skipUnless(hasattr(time, 'clock_getres'),
45 'need time.clock_getres()')
46 def test_clock_getres(self):
47 res = time.clock_getres(time.CLOCK_REALTIME)
48 self.assertGreater(res, 0.0)
49 self.assertLessEqual(res, 1.0)
50
Fred Drakebc561982001-05-22 17:02:02 +000051 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000052 self.assertEqual(time.ctime(self.t),
53 time.asctime(time.localtime(self.t)))
54 self.assertEqual(int(time.mktime(time.localtime(self.t))),
55 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000056
57 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +020058 self.assertRaises(ValueError, time.sleep, -2)
59 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +000060 time.sleep(1.2)
61
62 def test_strftime(self):
63 tt = time.gmtime(self.t)
64 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
65 'j', 'm', 'M', 'p', 'S',
66 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
67 format = ' %' + directive
68 try:
69 time.strftime(format, tt)
70 except ValueError:
71 self.fail('conversion specifier: %r failed.' % format)
72
Senthil Kumaran8f377a32011-04-06 12:54:06 +080073 # Issue #10762: Guard against invalid/non-supported format string
74 # so that Python don't crash (Windows crashes when the format string
75 # input to [w]strftime is not kosher.
76 if sys.platform.startswith('win'):
77 with self.assertRaises(ValueError):
78 time.strftime('%f')
79
Florent Xicluna49ce0682011-11-01 12:56:14 +010080 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +000081 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +010082 # of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000083
Victor Stinner73ea29c2011-01-08 01:56:31 +000084 # The year field is tested by other test cases above
85
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 # Check month [1, 12] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010087 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
88 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000089 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000091 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000092 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010094 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
95 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000096 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000098 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000099 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100101 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000102 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000103 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000104 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000105 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100107 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000108 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000109 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000110 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000111 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000113 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000114 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
115 # C99 only requires allowing for one leap second, but Python's docs say
116 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100117 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
118 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000119 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000120 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
121 # No check for upper-bound day of week;
122 # value forced into range by a ``% 7`` calculation.
123 # Start check at -2 since gettmarg() increments value before taking
124 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100125 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
126 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000127 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000128 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100130 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
131 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000132 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000134 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000135 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000136
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000137 def test_strftime_bounding_check(self):
138 self._bounds_checking(lambda tup: time.strftime('', tup))
139
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400141 # Make sure that using all zeros uses the proper default
142 # values. No test for daylight savings since strftime() does
143 # not change output based on its value and no test for year
144 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000146 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400147 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000148 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000150 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 # Should be able to go round-trip from strftime to strptime without
152 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000153 tt = time.gmtime(self.t)
154 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
155 'j', 'm', 'M', 'p', 'S',
156 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157 format = '%' + directive
158 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000159 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000161 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000162 self.fail("conversion specifier %r failed with '%s' input." %
163 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000164
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000165 def test_strptime_bytes(self):
166 # Make sure only strings are accepted as arguments to strptime.
167 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
168 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
169
Fred Drakebc561982001-05-22 17:02:02 +0000170 def test_asctime(self):
171 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000172
173 # Max year is only limited by the size of C int.
Florent Xiclunabceb5282011-11-01 14:11:34 +0100174 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
175 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
176 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
177 self.assertRaises(OverflowError, time.asctime,
178 (TIME_MAXYEAR + 1,) + (0,) * 8)
179 self.assertRaises(OverflowError, time.asctime,
180 (TIME_MINYEAR - 1,) + (0,) * 8)
Fred Drakebc561982001-05-22 17:02:02 +0000181 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000182 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000183 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000184
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000185 def test_asctime_bounding_check(self):
186 self._bounds_checking(time.asctime)
187
Georg Brandle10608c2011-01-02 22:33:43 +0000188 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000189 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
190 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
191 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
192 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000193 for year in [-100, 100, 1000, 2000, 10000]:
194 try:
195 testval = time.mktime((year, 1, 10) + (0,)*6)
196 except (ValueError, OverflowError):
197 # If mktime fails, ctime will fail too. This may happen
198 # on some platforms.
199 pass
200 else:
201 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000202
R. David Murray6ecf76e2010-12-14 01:22:50 +0000203 @unittest.skipIf(not hasattr(time, "tzset"),
204 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000205 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000206
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000207 from os import environ
208
Tim Peters0eadaac2003-04-24 16:02:54 +0000209 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000210 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000211 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000212
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000213 # These formats are correct for 2002, and possibly future years
214 # This format is the 'standard' as documented at:
215 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
216 # They are also documented in the tzset(3) man page on most Unix
217 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000218 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000219 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
220 utc='UTC+0'
221
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000222 org_TZ = environ.get('TZ',None)
223 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000224 # Make sure we can switch to UTC time and results are correct
225 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000226 # Note that altzone is undefined in UTC, as there is no DST
227 environ['TZ'] = eastern
228 time.tzset()
229 environ['TZ'] = utc
230 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000231 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000232 time.gmtime(xmas2002), time.localtime(xmas2002)
233 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000234 self.assertEqual(time.daylight, 0)
235 self.assertEqual(time.timezone, 0)
236 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000237
238 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000239 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000240 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000241 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
242 self.assertEqual(time.tzname, ('EST', 'EDT'))
243 self.assertEqual(len(time.tzname), 2)
244 self.assertEqual(time.daylight, 1)
245 self.assertEqual(time.timezone, 18000)
246 self.assertEqual(time.altzone, 14400)
247 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
248 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000249
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000250 # Now go to the southern hemisphere.
251 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000252 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000253 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
254 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
255 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
256 self.assertEqual(len(time.tzname), 2)
257 self.assertEqual(time.daylight, 1)
258 self.assertEqual(time.timezone, -36000)
259 self.assertEqual(time.altzone, -39600)
260 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000261
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000262 finally:
263 # Repair TZ environment variable in case any other tests
264 # rely on it.
265 if org_TZ is not None:
266 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000267 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000268 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000269 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000270
Tim Peters1b6f7a92004-06-20 02:50:16 +0000271 def test_insane_timestamps(self):
272 # It's possible that some platform maps time_t to double,
273 # and that this test will fail there. This test should
274 # exempt such platforms (provided they return reasonable
275 # results!).
276 for func in time.ctime, time.gmtime, time.localtime:
277 for unreasonable in -1e200, 1e200:
278 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000279
Fred Drakef901abd2004-08-03 17:58:55 +0000280 def test_ctime_without_arg(self):
281 # Not sure how to check the values, since the clock could tick
282 # at any time. Make sure these are at least accepted and
283 # don't raise errors.
284 time.ctime()
285 time.ctime(None)
286
287 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288 gt0 = time.gmtime()
289 gt1 = time.gmtime(None)
290 t0 = time.mktime(gt0)
291 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000292 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000293
294 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295 lt0 = time.localtime()
296 lt1 = time.localtime(None)
297 t0 = time.mktime(lt0)
298 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000299 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000300
Florent Xicluna050c7e62011-11-01 16:58:54 +0100301 # XXX run last to work around issue #13309 on Gentoo
Florent Xicluna725af4d2011-11-01 17:42:24 +0100302 def test_zzz_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100303 # Issue #1726687
304 for t in (-2, -1, 0, 1):
305 try:
306 tt = time.localtime(t)
307 except (OverflowError, ValueError):
308 pass
309 else:
310 self.assertEqual(time.mktime(tt), t)
Florent Xicluna050c7e62011-11-01 16:58:54 +0100311 tt = time.gmtime(self.t)
312 tzname = time.strftime('%Z', tt)
313 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100314 # It may not be possible to reliably make mktime return error
315 # on all platfom. This will make sure that no other exception
316 # than OverflowError is raised for an extreme value.
Antoine Pitroub0a1d622011-11-11 03:04:35 +0100317 if platform.libc_ver()[0] == 'glibc':
318 # Issue #13309: passing extreme values to mktime() or localtime()
319 # borks the glibc's internal timezone data.
320 return
Florent Xiclunabceb5282011-11-01 14:11:34 +0100321 try:
322 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
323 except OverflowError:
324 pass
325
326
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000327class TestLocale(unittest.TestCase):
328 def setUp(self):
329 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000330
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000331 def tearDown(self):
332 locale.setlocale(locale.LC_ALL, self.oldloc)
333
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000334 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000335 try:
336 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
337 except locale.Error:
338 # skip this test
339 return
340 # This should not cause an exception
341 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
342
Victor Stinner73ea29c2011-01-08 01:56:31 +0000343
344class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000345 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000346 raise NotImplementedError()
347
348class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100349 _format = '%d'
350
Victor Stinner73ea29c2011-01-08 01:56:31 +0000351 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000352 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000353
Victor Stinner73ea29c2011-01-08 01:56:31 +0000354 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000355 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000356 self.assertEqual(self.yearstr(12345), '12345')
357 self.assertEqual(self.yearstr(123456789), '123456789')
358
359class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100360
361 # Issue 13305: For years < 1000, the value is not always
362 # padded to 4 digits across platforms. The C standard
363 # assumes year >= 1900, so it does not specify the number
364 # of digits.
365
366 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
367 _format = '%04d'
368 else:
369 _format = '%d'
370
Victor Stinner73ea29c2011-01-08 01:56:31 +0000371 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100372 return time.strftime('%Y', (y,) + (0,) * 8)
373
374 def test_4dyear(self):
375 # Check that we can return the zero padded value.
376 if self._format == '%04d':
377 self.test_year('%04d')
378 else:
379 def year4d(y):
380 return time.strftime('%4Y', (y,) + (0,) * 8)
381 self.test_year('%04d', func=year4d)
382
Florent Xiclunabceb5282011-11-01 14:11:34 +0100383 def skip_if_not_supported(y):
384 msg = "strftime() is limited to [1; 9999] with Visual Studio"
385 # Check that it doesn't crash for year > 9999
386 try:
387 time.strftime('%Y', (y,) + (0,) * 8)
388 except ValueError:
389 cond = False
390 else:
391 cond = True
392 return unittest.skipUnless(cond, msg)
393
394 @skip_if_not_supported(10000)
395 def test_large_year(self):
396 return super().test_large_year()
397
398 @skip_if_not_supported(0)
399 def test_negative(self):
400 return super().test_negative()
401
402 del skip_if_not_supported
403
404
Florent Xicluna49ce0682011-11-01 12:56:14 +0100405class _Test4dYear(_BaseYearTest):
406 _format = '%d'
407
408 def test_year(self, fmt=None, func=None):
409 fmt = fmt or self._format
410 func = func or self.yearstr
411 self.assertEqual(func(1), fmt % 1)
412 self.assertEqual(func(68), fmt % 68)
413 self.assertEqual(func(69), fmt % 69)
414 self.assertEqual(func(99), fmt % 99)
415 self.assertEqual(func(999), fmt % 999)
416 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000417
418 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100419 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100420 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100421 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
422 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000423
Victor Stinner301f1212011-01-08 03:06:52 +0000424 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100425 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000426 self.assertEqual(self.yearstr(-1234), '-1234')
427 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100428 self.assertEqual(self.yearstr(-123456789), str(-123456789))
429 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100430 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
431 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
432 # Skip the value test, but check that no error is raised
433 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100434 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100435 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000436
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000437
Victor Stinner73ea29c2011-01-08 01:56:31 +0000438class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
439 pass
440
441class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000442 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000443
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000444
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000445def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000446 support.run_unittest(
447 TimeTestCase,
448 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000449 TestAsctime4dyear,
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400450 TestStrftime4dyear)
Fred Drake2e2be372001-09-20 21:33:42 +0000451
452if __name__ == "__main__":
453 test_main()