blob: be7ddcc34d6e836cbec8b00e490d60907ad3b21e [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
Victor Stinnerec895392012-04-29 02:41:27 +02008try:
9 import threading
10except ImportError:
11 threading = None
Barry Warsawb0c22321996-12-06 23:30:07 +000012
Florent Xiclunabceb5282011-11-01 14:11:34 +010013# Max year is only limited by the size of C int.
14SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
15TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
16TIME_MINYEAR = -TIME_MAXYEAR - 1
Victor Stinner3c1b3792014-02-17 00:02:43 +010017_PyTime_ROUND_DOWN = 0
18_PyTime_ROUND_UP = 1
Florent Xiclunabceb5282011-11-01 14:11:34 +010019
20
Fred Drakebc561982001-05-22 17:02:02 +000021class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000022
Fred Drakebc561982001-05-22 17:02:02 +000023 def setUp(self):
24 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000025
Fred Drakebc561982001-05-22 17:02:02 +000026 def test_data_attributes(self):
27 time.altzone
28 time.daylight
29 time.timezone
30 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000031
Victor Stinnerec895392012-04-29 02:41:27 +020032 def test_time(self):
33 time.time()
34 info = time.get_clock_info('time')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -040035 self.assertFalse(info.monotonic)
Victor Stinner6222d762012-06-12 23:04:11 +020036 self.assertTrue(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +020037
Fred Drakebc561982001-05-22 17:02:02 +000038 def test_clock(self):
39 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000040
Victor Stinnerec895392012-04-29 02:41:27 +020041 info = time.get_clock_info('clock')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -040042 self.assertTrue(info.monotonic)
Victor Stinner2b89fdf2012-06-12 22:46:37 +020043 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +020044
Victor Stinnere0be4232011-10-25 13:06:09 +020045 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
46 'need time.clock_gettime()')
47 def test_clock_realtime(self):
48 time.clock_gettime(time.CLOCK_REALTIME)
49
50 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
51 'need time.clock_gettime()')
52 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
53 'need time.CLOCK_MONOTONIC')
54 def test_clock_monotonic(self):
55 a = time.clock_gettime(time.CLOCK_MONOTONIC)
56 b = time.clock_gettime(time.CLOCK_MONOTONIC)
57 self.assertLessEqual(a, b)
58
59 @unittest.skipUnless(hasattr(time, 'clock_getres'),
60 'need time.clock_getres()')
61 def test_clock_getres(self):
62 res = time.clock_getres(time.CLOCK_REALTIME)
63 self.assertGreater(res, 0.0)
64 self.assertLessEqual(res, 1.0)
65
Victor Stinner30d79472012-04-03 00:45:07 +020066 @unittest.skipUnless(hasattr(time, 'clock_settime'),
67 'need time.clock_settime()')
68 def test_clock_settime(self):
69 t = time.clock_gettime(time.CLOCK_REALTIME)
70 try:
71 time.clock_settime(time.CLOCK_REALTIME, t)
72 except PermissionError:
73 pass
74
Victor Stinnerec895392012-04-29 02:41:27 +020075 if hasattr(time, 'CLOCK_MONOTONIC'):
76 self.assertRaises(OSError,
77 time.clock_settime, time.CLOCK_MONOTONIC, 0)
Victor Stinner30d79472012-04-03 00:45:07 +020078
Fred Drakebc561982001-05-22 17:02:02 +000079 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000080 self.assertEqual(time.ctime(self.t),
81 time.asctime(time.localtime(self.t)))
82 self.assertEqual(int(time.mktime(time.localtime(self.t))),
83 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000084
85 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +020086 self.assertRaises(ValueError, time.sleep, -2)
87 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +000088 time.sleep(1.2)
89
90 def test_strftime(self):
91 tt = time.gmtime(self.t)
92 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
93 'j', 'm', 'M', 'p', 'S',
94 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
95 format = ' %' + directive
96 try:
97 time.strftime(format, tt)
98 except ValueError:
99 self.fail('conversion specifier: %r failed.' % format)
100
Senthil Kumaran8f377a32011-04-06 12:54:06 +0800101 # Issue #10762: Guard against invalid/non-supported format string
102 # so that Python don't crash (Windows crashes when the format string
103 # input to [w]strftime is not kosher.
104 if sys.platform.startswith('win'):
105 with self.assertRaises(ValueError):
106 time.strftime('%f')
107
Florent Xicluna49ce0682011-11-01 12:56:14 +0100108 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +0000109 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +0100110 # of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +0000111
Victor Stinner73ea29c2011-01-08 01:56:31 +0000112 # The year field is tested by other test cases above
113
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 # Check month [1, 12] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100115 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
116 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000117 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000118 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000119 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000120 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100122 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
123 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000124 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000125 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000126 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000127 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100129 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000130 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000131 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000132 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000133 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100135 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000136 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000137 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000138 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000139 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000141 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000142 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
143 # C99 only requires allowing for one leap second, but Python's docs say
144 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100145 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
146 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000147 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000148 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
149 # No check for upper-bound day of week;
150 # value forced into range by a ``% 7`` calculation.
151 # Start check at -2 since gettmarg() increments value before taking
152 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100153 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
154 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000155 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000156 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100158 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
159 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000160 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000162 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000163 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000164
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000165 def test_strftime_bounding_check(self):
166 self._bounds_checking(lambda tup: time.strftime('', tup))
167
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000168 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400169 # Make sure that using all zeros uses the proper default
170 # values. No test for daylight savings since strftime() does
171 # not change output based on its value and no test for year
172 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000174 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400175 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000176 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000178 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000179 # Should be able to go round-trip from strftime to strptime without
Andrew Svetlov737fb892012-12-18 21:14:22 +0200180 # raising an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000181 tt = time.gmtime(self.t)
182 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
183 'j', 'm', 'M', 'p', 'S',
184 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 format = '%' + directive
186 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000187 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000188 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000189 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 self.fail("conversion specifier %r failed with '%s' input." %
191 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000192
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000193 def test_strptime_bytes(self):
194 # Make sure only strings are accepted as arguments to strptime.
195 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
196 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
197
Ezio Melotti0f389082013-04-04 02:09:20 +0300198 def test_strptime_exception_context(self):
199 # check that this doesn't chain exceptions needlessly (see #17572)
200 with self.assertRaises(ValueError) as e:
201 time.strptime('', '%D')
202 self.assertIs(e.exception.__suppress_context__, True)
Serhiy Storchakacdac3022013-11-24 18:15:37 +0200203 # additional check for IndexError branch (issue #19545)
204 with self.assertRaises(ValueError) as e:
205 time.strptime('19', '%Y %')
206 self.assertIs(e.exception.__suppress_context__, True)
Ezio Melotti0f389082013-04-04 02:09:20 +0300207
Fred Drakebc561982001-05-22 17:02:02 +0000208 def test_asctime(self):
209 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000210
211 # Max year is only limited by the size of C int.
Florent Xiclunabceb5282011-11-01 14:11:34 +0100212 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
213 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
214 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
215 self.assertRaises(OverflowError, time.asctime,
216 (TIME_MAXYEAR + 1,) + (0,) * 8)
217 self.assertRaises(OverflowError, time.asctime,
218 (TIME_MINYEAR - 1,) + (0,) * 8)
Fred Drakebc561982001-05-22 17:02:02 +0000219 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000220 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000221 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000222
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000223 def test_asctime_bounding_check(self):
224 self._bounds_checking(time.asctime)
225
Georg Brandle10608c2011-01-02 22:33:43 +0000226 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000227 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
228 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
229 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
230 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Victor Stinner1ac42612014-02-21 09:27:17 +0100231 for year in [-100, 100, 1000, 2000, 2050, 10000]:
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000232 try:
233 testval = time.mktime((year, 1, 10) + (0,)*6)
234 except (ValueError, OverflowError):
235 # If mktime fails, ctime will fail too. This may happen
236 # on some platforms.
237 pass
238 else:
239 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000240
Florent Xiclunae54371e2011-11-11 18:59:30 +0100241 @unittest.skipUnless(hasattr(time, "tzset"),
242 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000243 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000244
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000245 from os import environ
246
Tim Peters0eadaac2003-04-24 16:02:54 +0000247 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000248 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000249 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000250
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000251 # These formats are correct for 2002, and possibly future years
252 # This format is the 'standard' as documented at:
253 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
254 # They are also documented in the tzset(3) man page on most Unix
255 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000256 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000257 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
258 utc='UTC+0'
259
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000260 org_TZ = environ.get('TZ',None)
261 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000262 # Make sure we can switch to UTC time and results are correct
263 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000264 # Note that altzone is undefined in UTC, as there is no DST
265 environ['TZ'] = eastern
266 time.tzset()
267 environ['TZ'] = utc
268 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000269 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000270 time.gmtime(xmas2002), time.localtime(xmas2002)
271 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000272 self.assertEqual(time.daylight, 0)
273 self.assertEqual(time.timezone, 0)
274 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000275
276 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000277 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000278 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000279 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
280 self.assertEqual(time.tzname, ('EST', 'EDT'))
281 self.assertEqual(len(time.tzname), 2)
282 self.assertEqual(time.daylight, 1)
283 self.assertEqual(time.timezone, 18000)
284 self.assertEqual(time.altzone, 14400)
285 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
286 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000287
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000288 # Now go to the southern hemisphere.
289 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000290 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000291 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
Victor Stinner0cd47902011-12-08 00:32:51 +0100292
293 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100294 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
295 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
296 # on some operating systems (e.g. FreeBSD), which is wrong. See for
297 # example this bug:
298 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
Victor Stinner0cd47902011-12-08 00:32:51 +0100299 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100300 self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000301 self.assertEqual(len(time.tzname), 2)
302 self.assertEqual(time.daylight, 1)
303 self.assertEqual(time.timezone, -36000)
304 self.assertEqual(time.altzone, -39600)
305 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000306
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000307 finally:
308 # Repair TZ environment variable in case any other tests
309 # rely on it.
310 if org_TZ is not None:
311 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000312 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000313 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000314 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000315
Tim Peters1b6f7a92004-06-20 02:50:16 +0000316 def test_insane_timestamps(self):
317 # It's possible that some platform maps time_t to double,
318 # and that this test will fail there. This test should
319 # exempt such platforms (provided they return reasonable
320 # results!).
321 for func in time.ctime, time.gmtime, time.localtime:
322 for unreasonable in -1e200, 1e200:
Victor Stinner5d272cc2012-03-13 13:35:55 +0100323 self.assertRaises(OverflowError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000324
Fred Drakef901abd2004-08-03 17:58:55 +0000325 def test_ctime_without_arg(self):
326 # Not sure how to check the values, since the clock could tick
327 # at any time. Make sure these are at least accepted and
328 # don't raise errors.
329 time.ctime()
330 time.ctime(None)
331
332 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000333 gt0 = time.gmtime()
334 gt1 = time.gmtime(None)
335 t0 = time.mktime(gt0)
336 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000337 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000338
339 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000340 lt0 = time.localtime()
341 lt1 = time.localtime(None)
342 t0 = time.mktime(lt0)
343 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000344 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000345
Florent Xiclunae54371e2011-11-11 18:59:30 +0100346 def test_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100347 # Issue #1726687
348 for t in (-2, -1, 0, 1):
Victor Stinner8c8b4e02014-02-21 23:54:32 +0100349 if sys.platform.startswith('aix') and t == -1:
350 # Issue #11188, #19748: mktime() returns -1 on error. On Linux,
351 # the tm_wday field is used as a sentinel () to detect if -1 is
352 # really an error or a valid timestamp. On AIX, tm_wday is
353 # unchanged even on success and so cannot be used as a
354 # sentinel.
355 continue
Florent Xiclunabceb5282011-11-01 14:11:34 +0100356 try:
357 tt = time.localtime(t)
Victor Stinner2cbae982012-01-27 00:50:33 +0100358 except (OverflowError, OSError):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100359 pass
360 else:
361 self.assertEqual(time.mktime(tt), t)
Florent Xiclunae54371e2011-11-11 18:59:30 +0100362
363 # Issue #13309: passing extreme values to mktime() or localtime()
364 # borks the glibc's internal timezone data.
365 @unittest.skipUnless(platform.libc_ver()[0] != 'glibc',
366 "disabled because of a bug in glibc. Issue #13309")
367 def test_mktime_error(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100368 # It may not be possible to reliably make mktime return error
369 # on all platfom. This will make sure that no other exception
370 # than OverflowError is raised for an extreme value.
Florent Xiclunae54371e2011-11-11 18:59:30 +0100371 tt = time.gmtime(self.t)
372 tzname = time.strftime('%Z', tt)
373 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100374 try:
375 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
376 except OverflowError:
377 pass
Florent Xiclunae54371e2011-11-11 18:59:30 +0100378 self.assertEqual(time.strftime('%Z', tt), tzname)
Florent Xiclunabceb5282011-11-01 14:11:34 +0100379
Victor Stinnerec895392012-04-29 02:41:27 +0200380 @unittest.skipUnless(hasattr(time, 'monotonic'),
381 'need time.monotonic')
382 def test_monotonic(self):
Victor Stinner6c861812013-11-23 00:15:27 +0100383 # monotonic() should not go backward
384 times = [time.monotonic() for n in range(100)]
385 t1 = times[0]
386 for t2 in times[1:]:
387 self.assertGreaterEqual(t2, t1, "times=%s" % times)
388 t1 = t2
389
390 # monotonic() includes time elapsed during a sleep
Victor Stinnerec895392012-04-29 02:41:27 +0200391 t1 = time.monotonic()
Victor Stinnera9c99a62013-07-03 23:07:37 +0200392 time.sleep(0.5)
Victor Stinnerec895392012-04-29 02:41:27 +0200393 t2 = time.monotonic()
Victor Stinner2dd254d2012-01-20 02:24:18 +0100394 dt = t2 - t1
Victor Stinner8b302012012-02-07 23:29:46 +0100395 self.assertGreater(t2, t1)
Zachary Ware487aedb2014-01-02 09:41:10 -0600396 # Issue #20101: On some Windows machines, dt may be slightly low
397 self.assertTrue(0.45 <= dt <= 1.0, dt)
Antoine Pitrou391166f2012-01-18 22:35:21 +0100398
Victor Stinner6c861812013-11-23 00:15:27 +0100399 # monotonic() is a monotonic but non adjustable clock
Victor Stinnerec895392012-04-29 02:41:27 +0200400 info = time.get_clock_info('monotonic')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -0400401 self.assertTrue(info.monotonic)
Victor Stinner6222d762012-06-12 23:04:11 +0200402 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +0200403
404 def test_perf_counter(self):
405 time.perf_counter()
406
407 def test_process_time(self):
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200408 # process_time() should not include time spend during a sleep
Victor Stinnerec895392012-04-29 02:41:27 +0200409 start = time.process_time()
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200410 time.sleep(0.100)
Victor Stinnerec895392012-04-29 02:41:27 +0200411 stop = time.process_time()
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200412 # use 20 ms because process_time() has usually a resolution of 15 ms
413 # on Windows
414 self.assertLess(stop - start, 0.020)
Victor Stinnerec895392012-04-29 02:41:27 +0200415
416 info = time.get_clock_info('process_time')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -0400417 self.assertTrue(info.monotonic)
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200418 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +0200419
Victor Stinnerec895392012-04-29 02:41:27 +0200420 @unittest.skipUnless(hasattr(time, 'monotonic'),
421 'need time.monotonic')
422 @unittest.skipUnless(hasattr(time, 'clock_settime'),
423 'need time.clock_settime')
424 def test_monotonic_settime(self):
425 t1 = time.monotonic()
426 realtime = time.clock_gettime(time.CLOCK_REALTIME)
427 # jump backward with an offset of 1 hour
Victor Stinner071eca32012-03-15 01:17:09 +0100428 try:
Victor Stinnerec895392012-04-29 02:41:27 +0200429 time.clock_settime(time.CLOCK_REALTIME, realtime - 3600)
430 except PermissionError as err:
431 self.skipTest(err)
432 t2 = time.monotonic()
433 time.clock_settime(time.CLOCK_REALTIME, realtime)
434 # monotonic must not be affected by system clock updates
Victor Stinner071eca32012-03-15 01:17:09 +0100435 self.assertGreaterEqual(t2, t1)
436
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100437 def test_localtime_failure(self):
438 # Issue #13847: check for localtime() failure
Victor Stinner53d36452012-01-27 01:03:25 +0100439 invalid_time_t = None
440 for time_t in (-1, 2**30, 2**33, 2**60):
441 try:
442 time.localtime(time_t)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100443 except OverflowError:
444 self.skipTest("need 64-bit time_t")
Victor Stinner53d36452012-01-27 01:03:25 +0100445 except OSError:
446 invalid_time_t = time_t
447 break
448 if invalid_time_t is None:
449 self.skipTest("unable to find an invalid time_t value")
450
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100451 self.assertRaises(OSError, time.localtime, invalid_time_t)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100452 self.assertRaises(OSError, time.ctime, invalid_time_t)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100453
Victor Stinnerec895392012-04-29 02:41:27 +0200454 def test_get_clock_info(self):
455 clocks = ['clock', 'perf_counter', 'process_time', 'time']
456 if hasattr(time, 'monotonic'):
457 clocks.append('monotonic')
458
459 for name in clocks:
460 info = time.get_clock_info(name)
461 #self.assertIsInstance(info, dict)
462 self.assertIsInstance(info.implementation, str)
463 self.assertNotEqual(info.implementation, '')
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400464 self.assertIsInstance(info.monotonic, bool)
Victor Stinnerec895392012-04-29 02:41:27 +0200465 self.assertIsInstance(info.resolution, float)
466 # 0.0 < resolution <= 1.0
467 self.assertGreater(info.resolution, 0.0)
468 self.assertLessEqual(info.resolution, 1.0)
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200469 self.assertIsInstance(info.adjustable, bool)
Victor Stinnerec895392012-04-29 02:41:27 +0200470
471 self.assertRaises(ValueError, time.get_clock_info, 'xxx')
472
473
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000474class TestLocale(unittest.TestCase):
475 def setUp(self):
476 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000477
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000478 def tearDown(self):
479 locale.setlocale(locale.LC_ALL, self.oldloc)
480
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000481 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000482 try:
483 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
484 except locale.Error:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600485 self.skipTest('could not set locale.LC_ALL to fr_FR')
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000486 # This should not cause an exception
487 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
488
Victor Stinner73ea29c2011-01-08 01:56:31 +0000489
Victor Stinner73ea29c2011-01-08 01:56:31 +0000490class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100491 _format = '%d'
492
Victor Stinner73ea29c2011-01-08 01:56:31 +0000493 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000494 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000495
Victor Stinner73ea29c2011-01-08 01:56:31 +0000496 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000497 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000498 self.assertEqual(self.yearstr(12345), '12345')
499 self.assertEqual(self.yearstr(123456789), '123456789')
500
501class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100502
503 # Issue 13305: For years < 1000, the value is not always
504 # padded to 4 digits across platforms. The C standard
505 # assumes year >= 1900, so it does not specify the number
506 # of digits.
507
508 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
509 _format = '%04d'
510 else:
511 _format = '%d'
512
Victor Stinner73ea29c2011-01-08 01:56:31 +0000513 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100514 return time.strftime('%Y', (y,) + (0,) * 8)
515
516 def test_4dyear(self):
517 # Check that we can return the zero padded value.
518 if self._format == '%04d':
519 self.test_year('%04d')
520 else:
521 def year4d(y):
522 return time.strftime('%4Y', (y,) + (0,) * 8)
523 self.test_year('%04d', func=year4d)
524
Florent Xiclunabceb5282011-11-01 14:11:34 +0100525 def skip_if_not_supported(y):
526 msg = "strftime() is limited to [1; 9999] with Visual Studio"
527 # Check that it doesn't crash for year > 9999
528 try:
529 time.strftime('%Y', (y,) + (0,) * 8)
530 except ValueError:
531 cond = False
532 else:
533 cond = True
534 return unittest.skipUnless(cond, msg)
535
536 @skip_if_not_supported(10000)
537 def test_large_year(self):
538 return super().test_large_year()
539
540 @skip_if_not_supported(0)
541 def test_negative(self):
542 return super().test_negative()
543
544 del skip_if_not_supported
545
546
Ezio Melotti3836d702013-04-11 20:29:42 +0300547class _Test4dYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100548 _format = '%d'
549
550 def test_year(self, fmt=None, func=None):
551 fmt = fmt or self._format
552 func = func or self.yearstr
553 self.assertEqual(func(1), fmt % 1)
554 self.assertEqual(func(68), fmt % 68)
555 self.assertEqual(func(69), fmt % 69)
556 self.assertEqual(func(99), fmt % 99)
557 self.assertEqual(func(999), fmt % 999)
558 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000559
560 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100561 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100562 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100563 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
564 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000565
Victor Stinner301f1212011-01-08 03:06:52 +0000566 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100567 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000568 self.assertEqual(self.yearstr(-1234), '-1234')
569 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100570 self.assertEqual(self.yearstr(-123456789), str(-123456789))
571 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100572 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
573 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
574 # Skip the value test, but check that no error is raised
575 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100576 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100577 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000578
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000579
Ezio Melotti3836d702013-04-11 20:29:42 +0300580class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000581 pass
582
Ezio Melotti3836d702013-04-11 20:29:42 +0300583class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase):
Victor Stinner301f1212011-01-08 03:06:52 +0000584 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000585
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000586
Victor Stinner643cd682012-03-02 22:54:03 +0100587class TestPytime(unittest.TestCase):
Victor Stinner5d272cc2012-03-13 13:35:55 +0100588 def setUp(self):
589 self.invalid_values = (
590 -(2 ** 100), 2 ** 100,
591 -(2.0 ** 100.0), 2.0 ** 100.0,
592 )
593
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200594 @support.cpython_only
Victor Stinner5d272cc2012-03-13 13:35:55 +0100595 def test_time_t(self):
596 from _testcapi import pytime_object_to_time_t
Victor Stinner3c1b3792014-02-17 00:02:43 +0100597 for obj, time_t, rnd in (
598 # Round towards zero
599 (0, 0, _PyTime_ROUND_DOWN),
600 (-1, -1, _PyTime_ROUND_DOWN),
601 (-1.0, -1, _PyTime_ROUND_DOWN),
602 (-1.9, -1, _PyTime_ROUND_DOWN),
603 (1.0, 1, _PyTime_ROUND_DOWN),
604 (1.9, 1, _PyTime_ROUND_DOWN),
605 # Round away from zero
606 (0, 0, _PyTime_ROUND_UP),
607 (-1, -1, _PyTime_ROUND_UP),
608 (-1.0, -1, _PyTime_ROUND_UP),
609 (-1.9, -2, _PyTime_ROUND_UP),
610 (1.0, 1, _PyTime_ROUND_UP),
611 (1.9, 2, _PyTime_ROUND_UP),
Victor Stinner5d272cc2012-03-13 13:35:55 +0100612 ):
Victor Stinner3c1b3792014-02-17 00:02:43 +0100613 self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100614
Victor Stinner3c1b3792014-02-17 00:02:43 +0100615 rnd = _PyTime_ROUND_DOWN
Victor Stinner5d272cc2012-03-13 13:35:55 +0100616 for invalid in self.invalid_values:
Victor Stinner3c1b3792014-02-17 00:02:43 +0100617 self.assertRaises(OverflowError,
618 pytime_object_to_time_t, invalid, rnd)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100619
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200620 @support.cpython_only
Victor Stinner5d272cc2012-03-13 13:35:55 +0100621 def test_timeval(self):
622 from _testcapi import pytime_object_to_timeval
Victor Stinner3c1b3792014-02-17 00:02:43 +0100623 for obj, timeval, rnd in (
624 # Round towards zero
625 (0, (0, 0), _PyTime_ROUND_DOWN),
626 (-1, (-1, 0), _PyTime_ROUND_DOWN),
627 (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
628 (1e-6, (0, 1), _PyTime_ROUND_DOWN),
629 (1e-7, (0, 0), _PyTime_ROUND_DOWN),
630 (-1e-6, (-1, 999999), _PyTime_ROUND_DOWN),
631 (-1e-7, (-1, 999999), _PyTime_ROUND_DOWN),
632 (-1.2, (-2, 800000), _PyTime_ROUND_DOWN),
633 (0.9999999, (0, 999999), _PyTime_ROUND_DOWN),
634 (0.0000041, (0, 4), _PyTime_ROUND_DOWN),
635 (1.1234560, (1, 123456), _PyTime_ROUND_DOWN),
636 (1.1234569, (1, 123456), _PyTime_ROUND_DOWN),
637 (-0.0000040, (-1, 999996), _PyTime_ROUND_DOWN),
638 (-0.0000041, (-1, 999995), _PyTime_ROUND_DOWN),
639 (-1.1234560, (-2, 876544), _PyTime_ROUND_DOWN),
640 (-1.1234561, (-2, 876543), _PyTime_ROUND_DOWN),
641 # Round away from zero
642 (0, (0, 0), _PyTime_ROUND_UP),
643 (-1, (-1, 0), _PyTime_ROUND_UP),
644 (-1.0, (-1, 0), _PyTime_ROUND_UP),
645 (1e-6, (0, 1), _PyTime_ROUND_UP),
646 (1e-7, (0, 1), _PyTime_ROUND_UP),
647 (-1e-6, (-1, 999999), _PyTime_ROUND_UP),
648 (-1e-7, (-1, 999999), _PyTime_ROUND_UP),
649 (-1.2, (-2, 800000), _PyTime_ROUND_UP),
650 (0.9999999, (1, 0), _PyTime_ROUND_UP),
651 (0.0000041, (0, 5), _PyTime_ROUND_UP),
652 (1.1234560, (1, 123457), _PyTime_ROUND_UP),
653 (1.1234569, (1, 123457), _PyTime_ROUND_UP),
654 (-0.0000040, (-1, 999996), _PyTime_ROUND_UP),
655 (-0.0000041, (-1, 999995), _PyTime_ROUND_UP),
656 (-1.1234560, (-2, 876544), _PyTime_ROUND_UP),
657 (-1.1234561, (-2, 876543), _PyTime_ROUND_UP),
Victor Stinner5d272cc2012-03-13 13:35:55 +0100658 ):
Victor Stinner3c1b3792014-02-17 00:02:43 +0100659 with self.subTest(obj=obj, round=rnd, timeval=timeval):
660 self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100661
Victor Stinner3c1b3792014-02-17 00:02:43 +0100662 rnd = _PyTime_ROUND_DOWN
Victor Stinner5d272cc2012-03-13 13:35:55 +0100663 for invalid in self.invalid_values:
Victor Stinner3c1b3792014-02-17 00:02:43 +0100664 self.assertRaises(OverflowError,
665 pytime_object_to_timeval, invalid, rnd)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100666
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200667 @support.cpython_only
Victor Stinner643cd682012-03-02 22:54:03 +0100668 def test_timespec(self):
669 from _testcapi import pytime_object_to_timespec
Victor Stinner3c1b3792014-02-17 00:02:43 +0100670 for obj, timespec, rnd in (
671 # Round towards zero
672 (0, (0, 0), _PyTime_ROUND_DOWN),
673 (-1, (-1, 0), _PyTime_ROUND_DOWN),
674 (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
675 (1e-9, (0, 1), _PyTime_ROUND_DOWN),
676 (1e-10, (0, 0), _PyTime_ROUND_DOWN),
677 (-1e-9, (-1, 999999999), _PyTime_ROUND_DOWN),
678 (-1e-10, (-1, 999999999), _PyTime_ROUND_DOWN),
679 (-1.2, (-2, 800000000), _PyTime_ROUND_DOWN),
680 (0.9999999999, (0, 999999999), _PyTime_ROUND_DOWN),
681 (1.1234567890, (1, 123456789), _PyTime_ROUND_DOWN),
682 (1.1234567899, (1, 123456789), _PyTime_ROUND_DOWN),
683 (-1.1234567890, (-2, 876543211), _PyTime_ROUND_DOWN),
684 (-1.1234567891, (-2, 876543210), _PyTime_ROUND_DOWN),
685 # Round away from zero
686 (0, (0, 0), _PyTime_ROUND_UP),
687 (-1, (-1, 0), _PyTime_ROUND_UP),
688 (-1.0, (-1, 0), _PyTime_ROUND_UP),
689 (1e-9, (0, 1), _PyTime_ROUND_UP),
690 (1e-10, (0, 1), _PyTime_ROUND_UP),
691 (-1e-9, (-1, 999999999), _PyTime_ROUND_UP),
692 (-1e-10, (-1, 999999999), _PyTime_ROUND_UP),
693 (-1.2, (-2, 800000000), _PyTime_ROUND_UP),
694 (0.9999999999, (1, 0), _PyTime_ROUND_UP),
695 (1.1234567890, (1, 123456790), _PyTime_ROUND_UP),
696 (1.1234567899, (1, 123456790), _PyTime_ROUND_UP),
697 (-1.1234567890, (-2, 876543211), _PyTime_ROUND_UP),
698 (-1.1234567891, (-2, 876543210), _PyTime_ROUND_UP),
Victor Stinner643cd682012-03-02 22:54:03 +0100699 ):
Victor Stinner3c1b3792014-02-17 00:02:43 +0100700 with self.subTest(obj=obj, round=rnd, timespec=timespec):
701 self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
Victor Stinner643cd682012-03-02 22:54:03 +0100702
Victor Stinner3c1b3792014-02-17 00:02:43 +0100703 rnd = _PyTime_ROUND_DOWN
Victor Stinner5d272cc2012-03-13 13:35:55 +0100704 for invalid in self.invalid_values:
Victor Stinner3c1b3792014-02-17 00:02:43 +0100705 self.assertRaises(OverflowError,
706 pytime_object_to_timespec, invalid, rnd)
Victor Stinner643cd682012-03-02 22:54:03 +0100707
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400708 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
709 def test_localtime_timezone(self):
Victor Stinner643cd682012-03-02 22:54:03 +0100710
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400711 # Get the localtime and examine it for the offset and zone.
712 lt = time.localtime()
713 self.assertTrue(hasattr(lt, "tm_gmtoff"))
714 self.assertTrue(hasattr(lt, "tm_zone"))
715
716 # See if the offset and zone are similar to the module
717 # attributes.
718 if lt.tm_gmtoff is None:
719 self.assertTrue(not hasattr(time, "timezone"))
720 else:
721 self.assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst])
722 if lt.tm_zone is None:
723 self.assertTrue(not hasattr(time, "tzname"))
724 else:
725 self.assertEqual(lt.tm_zone, time.tzname[lt.tm_isdst])
726
727 # Try and make UNIX times from the localtime and a 9-tuple
728 # created from the localtime. Test to see that the times are
729 # the same.
730 t = time.mktime(lt); t9 = time.mktime(lt[:9])
731 self.assertEqual(t, t9)
732
733 # Make localtimes from the UNIX times and compare them to
734 # the original localtime, thus making a round trip.
735 new_lt = time.localtime(t); new_lt9 = time.localtime(t9)
736 self.assertEqual(new_lt, lt)
737 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
738 self.assertEqual(new_lt.tm_zone, lt.tm_zone)
739 self.assertEqual(new_lt9, lt)
740 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
741 self.assertEqual(new_lt9.tm_zone, lt.tm_zone)
742
743 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
744 def test_strptime_timezone(self):
745 t = time.strptime("UTC", "%Z")
746 self.assertEqual(t.tm_zone, 'UTC')
747 t = time.strptime("+0500", "%z")
748 self.assertEqual(t.tm_gmtoff, 5 * 3600)
749
750 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
751 def test_short_times(self):
752
753 import pickle
754
755 # Load a short time structure using pickle.
756 st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\nI1\ntp1\n(dp2\ntp3\nRp4\n."
757 lt = pickle.loads(st)
758 self.assertIs(lt.tm_gmtoff, None)
759 self.assertIs(lt.tm_zone, None)
Victor Stinner643cd682012-03-02 22:54:03 +0100760
Fred Drake2e2be372001-09-20 21:33:42 +0000761
762if __name__ == "__main__":
Ezio Melotti3836d702013-04-11 20:29:42 +0300763 unittest.main()