blob: 75ab666faa85cf4fb1eebf73b6e52772fd5457e6 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Victor Stinner992c43f2015-03-27 17:12:45 +01002import enum
3import locale
4import platform
5import sys
6import sysconfig
Barry Warsawb0c22321996-12-06 23:30:07 +00007import time
Fred Drakebc561982001-05-22 17:02:02 +00008import unittest
Victor Stinnerec895392012-04-29 02:41:27 +02009try:
10 import threading
11except ImportError:
12 threading = None
Victor Stinner34dc0f42015-03-27 18:19:03 +010013try:
14 import _testcapi
15except ImportError:
16 _testcapi = None
17
Barry Warsawb0c22321996-12-06 23:30:07 +000018
Florent Xiclunabceb5282011-11-01 14:11:34 +010019# Max year is only limited by the size of C int.
20SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
21TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
22TIME_MINYEAR = -TIME_MAXYEAR - 1
Victor Stinner992c43f2015-03-27 17:12:45 +010023
Victor Stinner62d1c702015-04-01 17:47:07 +020024US_TO_NS = 10 ** 3
25MS_TO_NS = 10 ** 6
Victor Stinner4bfb4602015-03-27 22:27:24 +010026SEC_TO_NS = 10 ** 9
Victor Stinner992c43f2015-03-27 17:12:45 +010027
28class _PyTime(enum.IntEnum):
Victor Stinnerbcdd7772015-03-30 03:52:49 +020029 # Round towards minus infinity (-inf)
Victor Stinnera695f832015-03-30 03:57:14 +020030 ROUND_FLOOR = 0
Victor Stinnerbcdd7772015-03-30 03:52:49 +020031 # Round towards infinity (+inf)
Victor Stinnera695f832015-03-30 03:57:14 +020032 ROUND_CEILING = 1
Victor Stinner74474232015-09-02 01:43:56 +020033 # Round to nearest with ties going away from zero
34 ROUND_HALF_UP = 2
Victor Stinner992c43f2015-03-27 17:12:45 +010035
Victor Stinner74474232015-09-02 01:43:56 +020036ALL_ROUNDING_METHODS = (_PyTime.ROUND_FLOOR, _PyTime.ROUND_CEILING,
37 _PyTime.ROUND_HALF_UP)
Florent Xiclunabceb5282011-11-01 14:11:34 +010038
39
Fred Drakebc561982001-05-22 17:02:02 +000040class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000041
Fred Drakebc561982001-05-22 17:02:02 +000042 def setUp(self):
43 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000044
Fred Drakebc561982001-05-22 17:02:02 +000045 def test_data_attributes(self):
46 time.altzone
47 time.daylight
48 time.timezone
49 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000050
Victor Stinnerec895392012-04-29 02:41:27 +020051 def test_time(self):
52 time.time()
53 info = time.get_clock_info('time')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -040054 self.assertFalse(info.monotonic)
Victor Stinner6222d762012-06-12 23:04:11 +020055 self.assertTrue(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +020056
Fred Drakebc561982001-05-22 17:02:02 +000057 def test_clock(self):
58 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000059
Victor Stinnerec895392012-04-29 02:41:27 +020060 info = time.get_clock_info('clock')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -040061 self.assertTrue(info.monotonic)
Victor Stinner2b89fdf2012-06-12 22:46:37 +020062 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +020063
Victor Stinnere0be4232011-10-25 13:06:09 +020064 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
65 'need time.clock_gettime()')
66 def test_clock_realtime(self):
67 time.clock_gettime(time.CLOCK_REALTIME)
68
69 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
70 'need time.clock_gettime()')
71 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
72 'need time.CLOCK_MONOTONIC')
73 def test_clock_monotonic(self):
74 a = time.clock_gettime(time.CLOCK_MONOTONIC)
75 b = time.clock_gettime(time.CLOCK_MONOTONIC)
76 self.assertLessEqual(a, b)
77
78 @unittest.skipUnless(hasattr(time, 'clock_getres'),
79 'need time.clock_getres()')
80 def test_clock_getres(self):
81 res = time.clock_getres(time.CLOCK_REALTIME)
82 self.assertGreater(res, 0.0)
83 self.assertLessEqual(res, 1.0)
84
Victor Stinner30d79472012-04-03 00:45:07 +020085 @unittest.skipUnless(hasattr(time, 'clock_settime'),
86 'need time.clock_settime()')
87 def test_clock_settime(self):
88 t = time.clock_gettime(time.CLOCK_REALTIME)
89 try:
90 time.clock_settime(time.CLOCK_REALTIME, t)
91 except PermissionError:
92 pass
93
Victor Stinnerec895392012-04-29 02:41:27 +020094 if hasattr(time, 'CLOCK_MONOTONIC'):
95 self.assertRaises(OSError,
96 time.clock_settime, time.CLOCK_MONOTONIC, 0)
Victor Stinner30d79472012-04-03 00:45:07 +020097
Fred Drakebc561982001-05-22 17:02:02 +000098 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000099 self.assertEqual(time.ctime(self.t),
100 time.asctime(time.localtime(self.t)))
101 self.assertEqual(int(time.mktime(time.localtime(self.t))),
102 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +0000103
104 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +0200105 self.assertRaises(ValueError, time.sleep, -2)
106 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +0000107 time.sleep(1.2)
108
109 def test_strftime(self):
110 tt = time.gmtime(self.t)
111 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
112 'j', 'm', 'M', 'p', 'S',
113 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
114 format = ' %' + directive
115 try:
116 time.strftime(format, tt)
117 except ValueError:
118 self.fail('conversion specifier: %r failed.' % format)
119
Florent Xicluna49ce0682011-11-01 12:56:14 +0100120 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +0000121 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +0100122 # of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +0000123
Victor Stinner73ea29c2011-01-08 01:56:31 +0000124 # The year field is tested by other test cases above
125
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126 # Check month [1, 12] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100127 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
128 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000129 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000131 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000132 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100134 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
135 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000136 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137 (1900, 1, -1, 0, 0, 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, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100141 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000142 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000143 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000144 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000145 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100147 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000148 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000149 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000150 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000151 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000153 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000154 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
155 # C99 only requires allowing for one leap second, but Python's docs say
156 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100157 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
158 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000159 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000160 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
161 # No check for upper-bound day of week;
162 # value forced into range by a ``% 7`` calculation.
163 # Start check at -2 since gettmarg() increments value before taking
164 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100165 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
166 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000167 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000168 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000169 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100170 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
171 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000172 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000174 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000175 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000176
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000177 def test_strftime_bounding_check(self):
178 self._bounds_checking(lambda tup: time.strftime('', tup))
179
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400181 # Make sure that using all zeros uses the proper default
182 # values. No test for daylight savings since strftime() does
183 # not change output based on its value and no test for year
184 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000186 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400187 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000188 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000190 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 # Should be able to go round-trip from strftime to strptime without
Andrew Svetlov737fb892012-12-18 21:14:22 +0200192 # raising an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000193 tt = time.gmtime(self.t)
194 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
195 'j', 'm', 'M', 'p', 'S',
196 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197 format = '%' + directive
198 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000199 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000201 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202 self.fail("conversion specifier %r failed with '%s' input." %
203 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000204
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000205 def test_strptime_bytes(self):
206 # Make sure only strings are accepted as arguments to strptime.
207 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
208 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
209
Ezio Melotti0f389082013-04-04 02:09:20 +0300210 def test_strptime_exception_context(self):
211 # check that this doesn't chain exceptions needlessly (see #17572)
212 with self.assertRaises(ValueError) as e:
213 time.strptime('', '%D')
214 self.assertIs(e.exception.__suppress_context__, True)
Serhiy Storchakacdac3022013-11-24 18:15:37 +0200215 # additional check for IndexError branch (issue #19545)
216 with self.assertRaises(ValueError) as e:
217 time.strptime('19', '%Y %')
218 self.assertIs(e.exception.__suppress_context__, True)
Ezio Melotti0f389082013-04-04 02:09:20 +0300219
Fred Drakebc561982001-05-22 17:02:02 +0000220 def test_asctime(self):
221 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000222
223 # Max year is only limited by the size of C int.
Florent Xiclunabceb5282011-11-01 14:11:34 +0100224 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
225 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
226 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
227 self.assertRaises(OverflowError, time.asctime,
228 (TIME_MAXYEAR + 1,) + (0,) * 8)
229 self.assertRaises(OverflowError, time.asctime,
230 (TIME_MINYEAR - 1,) + (0,) * 8)
Fred Drakebc561982001-05-22 17:02:02 +0000231 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000232 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000233 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000234
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000235 def test_asctime_bounding_check(self):
236 self._bounds_checking(time.asctime)
237
Georg Brandle10608c2011-01-02 22:33:43 +0000238 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000239 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
240 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
241 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
242 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Victor Stinner1ac42612014-02-21 09:27:17 +0100243 for year in [-100, 100, 1000, 2000, 2050, 10000]:
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000244 try:
245 testval = time.mktime((year, 1, 10) + (0,)*6)
246 except (ValueError, OverflowError):
247 # If mktime fails, ctime will fail too. This may happen
248 # on some platforms.
249 pass
250 else:
251 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000252
Florent Xiclunae54371e2011-11-11 18:59:30 +0100253 @unittest.skipUnless(hasattr(time, "tzset"),
254 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000255 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000256
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000257 from os import environ
258
Tim Peters0eadaac2003-04-24 16:02:54 +0000259 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000260 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000261 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000262
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000263 # These formats are correct for 2002, and possibly future years
264 # This format is the 'standard' as documented at:
265 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
266 # They are also documented in the tzset(3) man page on most Unix
267 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000268 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000269 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
270 utc='UTC+0'
271
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000272 org_TZ = environ.get('TZ',None)
273 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000274 # Make sure we can switch to UTC time and results are correct
275 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000276 # Note that altzone is undefined in UTC, as there is no DST
277 environ['TZ'] = eastern
278 time.tzset()
279 environ['TZ'] = utc
280 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000281 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000282 time.gmtime(xmas2002), time.localtime(xmas2002)
283 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000284 self.assertEqual(time.daylight, 0)
285 self.assertEqual(time.timezone, 0)
286 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000287
288 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000289 environ['TZ'] = eastern
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))
292 self.assertEqual(time.tzname, ('EST', 'EDT'))
293 self.assertEqual(len(time.tzname), 2)
294 self.assertEqual(time.daylight, 1)
295 self.assertEqual(time.timezone, 18000)
296 self.assertEqual(time.altzone, 14400)
297 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
298 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000299
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000300 # Now go to the southern hemisphere.
301 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000302 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000303 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
Victor Stinner0cd47902011-12-08 00:32:51 +0100304
305 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100306 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
307 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
308 # on some operating systems (e.g. FreeBSD), which is wrong. See for
309 # example this bug:
310 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
Victor Stinner0cd47902011-12-08 00:32:51 +0100311 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100312 self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000313 self.assertEqual(len(time.tzname), 2)
314 self.assertEqual(time.daylight, 1)
315 self.assertEqual(time.timezone, -36000)
316 self.assertEqual(time.altzone, -39600)
317 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000318
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000319 finally:
320 # Repair TZ environment variable in case any other tests
321 # rely on it.
322 if org_TZ is not None:
323 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000324 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000325 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000326 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000327
Tim Peters1b6f7a92004-06-20 02:50:16 +0000328 def test_insane_timestamps(self):
329 # It's possible that some platform maps time_t to double,
330 # and that this test will fail there. This test should
331 # exempt such platforms (provided they return reasonable
332 # results!).
333 for func in time.ctime, time.gmtime, time.localtime:
334 for unreasonable in -1e200, 1e200:
Victor Stinner5d272cc2012-03-13 13:35:55 +0100335 self.assertRaises(OverflowError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000336
Fred Drakef901abd2004-08-03 17:58:55 +0000337 def test_ctime_without_arg(self):
338 # Not sure how to check the values, since the clock could tick
339 # at any time. Make sure these are at least accepted and
340 # don't raise errors.
341 time.ctime()
342 time.ctime(None)
343
344 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345 gt0 = time.gmtime()
346 gt1 = time.gmtime(None)
347 t0 = time.mktime(gt0)
348 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000349 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000350
351 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352 lt0 = time.localtime()
353 lt1 = time.localtime(None)
354 t0 = time.mktime(lt0)
355 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000356 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000357
Florent Xiclunae54371e2011-11-11 18:59:30 +0100358 def test_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100359 # Issue #1726687
360 for t in (-2, -1, 0, 1):
Victor Stinner8c8b4e02014-02-21 23:54:32 +0100361 if sys.platform.startswith('aix') and t == -1:
362 # Issue #11188, #19748: mktime() returns -1 on error. On Linux,
363 # the tm_wday field is used as a sentinel () to detect if -1 is
364 # really an error or a valid timestamp. On AIX, tm_wday is
365 # unchanged even on success and so cannot be used as a
366 # sentinel.
367 continue
Florent Xiclunabceb5282011-11-01 14:11:34 +0100368 try:
369 tt = time.localtime(t)
Victor Stinner2cbae982012-01-27 00:50:33 +0100370 except (OverflowError, OSError):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100371 pass
372 else:
373 self.assertEqual(time.mktime(tt), t)
Florent Xiclunae54371e2011-11-11 18:59:30 +0100374
375 # Issue #13309: passing extreme values to mktime() or localtime()
376 # borks the glibc's internal timezone data.
377 @unittest.skipUnless(platform.libc_ver()[0] != 'glibc',
378 "disabled because of a bug in glibc. Issue #13309")
379 def test_mktime_error(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100380 # It may not be possible to reliably make mktime return error
381 # on all platfom. This will make sure that no other exception
382 # than OverflowError is raised for an extreme value.
Florent Xiclunae54371e2011-11-11 18:59:30 +0100383 tt = time.gmtime(self.t)
384 tzname = time.strftime('%Z', tt)
385 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100386 try:
387 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
388 except OverflowError:
389 pass
Florent Xiclunae54371e2011-11-11 18:59:30 +0100390 self.assertEqual(time.strftime('%Z', tt), tzname)
Florent Xiclunabceb5282011-11-01 14:11:34 +0100391
Victor Stinnerec895392012-04-29 02:41:27 +0200392 @unittest.skipUnless(hasattr(time, 'monotonic'),
393 'need time.monotonic')
394 def test_monotonic(self):
Victor Stinner6c861812013-11-23 00:15:27 +0100395 # monotonic() should not go backward
396 times = [time.monotonic() for n in range(100)]
397 t1 = times[0]
398 for t2 in times[1:]:
399 self.assertGreaterEqual(t2, t1, "times=%s" % times)
400 t1 = t2
401
402 # monotonic() includes time elapsed during a sleep
Victor Stinnerec895392012-04-29 02:41:27 +0200403 t1 = time.monotonic()
Victor Stinnera9c99a62013-07-03 23:07:37 +0200404 time.sleep(0.5)
Victor Stinnerec895392012-04-29 02:41:27 +0200405 t2 = time.monotonic()
Victor Stinner2dd254d2012-01-20 02:24:18 +0100406 dt = t2 - t1
Victor Stinner8b302012012-02-07 23:29:46 +0100407 self.assertGreater(t2, t1)
Zachary Ware487aedb2014-01-02 09:41:10 -0600408 # Issue #20101: On some Windows machines, dt may be slightly low
409 self.assertTrue(0.45 <= dt <= 1.0, dt)
Antoine Pitrou391166f2012-01-18 22:35:21 +0100410
Victor Stinner6c861812013-11-23 00:15:27 +0100411 # monotonic() is a monotonic but non adjustable clock
Victor Stinnerec895392012-04-29 02:41:27 +0200412 info = time.get_clock_info('monotonic')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -0400413 self.assertTrue(info.monotonic)
Victor Stinner6222d762012-06-12 23:04:11 +0200414 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +0200415
416 def test_perf_counter(self):
417 time.perf_counter()
418
419 def test_process_time(self):
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200420 # process_time() should not include time spend during a sleep
Victor Stinnerec895392012-04-29 02:41:27 +0200421 start = time.process_time()
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200422 time.sleep(0.100)
Victor Stinnerec895392012-04-29 02:41:27 +0200423 stop = time.process_time()
Victor Stinner0dec1bf2012-06-01 22:45:23 +0200424 # use 20 ms because process_time() has usually a resolution of 15 ms
425 # on Windows
426 self.assertLess(stop - start, 0.020)
Victor Stinnerec895392012-04-29 02:41:27 +0200427
428 info = time.get_clock_info('process_time')
Benjamin Peterson1c5ae552012-05-01 11:14:32 -0400429 self.assertTrue(info.monotonic)
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200430 self.assertFalse(info.adjustable)
Victor Stinnerec895392012-04-29 02:41:27 +0200431
Victor Stinnerec895392012-04-29 02:41:27 +0200432 @unittest.skipUnless(hasattr(time, 'monotonic'),
433 'need time.monotonic')
434 @unittest.skipUnless(hasattr(time, 'clock_settime'),
435 'need time.clock_settime')
436 def test_monotonic_settime(self):
437 t1 = time.monotonic()
438 realtime = time.clock_gettime(time.CLOCK_REALTIME)
439 # jump backward with an offset of 1 hour
Victor Stinner071eca32012-03-15 01:17:09 +0100440 try:
Victor Stinnerec895392012-04-29 02:41:27 +0200441 time.clock_settime(time.CLOCK_REALTIME, realtime - 3600)
442 except PermissionError as err:
443 self.skipTest(err)
444 t2 = time.monotonic()
445 time.clock_settime(time.CLOCK_REALTIME, realtime)
446 # monotonic must not be affected by system clock updates
Victor Stinner071eca32012-03-15 01:17:09 +0100447 self.assertGreaterEqual(t2, t1)
448
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100449 def test_localtime_failure(self):
450 # Issue #13847: check for localtime() failure
Victor Stinner53d36452012-01-27 01:03:25 +0100451 invalid_time_t = None
452 for time_t in (-1, 2**30, 2**33, 2**60):
453 try:
454 time.localtime(time_t)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100455 except OverflowError:
456 self.skipTest("need 64-bit time_t")
Victor Stinner53d36452012-01-27 01:03:25 +0100457 except OSError:
458 invalid_time_t = time_t
459 break
460 if invalid_time_t is None:
461 self.skipTest("unable to find an invalid time_t value")
462
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100463 self.assertRaises(OSError, time.localtime, invalid_time_t)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100464 self.assertRaises(OSError, time.ctime, invalid_time_t)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100465
Victor Stinnerec895392012-04-29 02:41:27 +0200466 def test_get_clock_info(self):
467 clocks = ['clock', 'perf_counter', 'process_time', 'time']
468 if hasattr(time, 'monotonic'):
469 clocks.append('monotonic')
470
471 for name in clocks:
472 info = time.get_clock_info(name)
473 #self.assertIsInstance(info, dict)
474 self.assertIsInstance(info.implementation, str)
475 self.assertNotEqual(info.implementation, '')
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400476 self.assertIsInstance(info.monotonic, bool)
Victor Stinnerec895392012-04-29 02:41:27 +0200477 self.assertIsInstance(info.resolution, float)
478 # 0.0 < resolution <= 1.0
479 self.assertGreater(info.resolution, 0.0)
480 self.assertLessEqual(info.resolution, 1.0)
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200481 self.assertIsInstance(info.adjustable, bool)
Victor Stinnerec895392012-04-29 02:41:27 +0200482
483 self.assertRaises(ValueError, time.get_clock_info, 'xxx')
484
485
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000486class TestLocale(unittest.TestCase):
487 def setUp(self):
488 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000489
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000490 def tearDown(self):
491 locale.setlocale(locale.LC_ALL, self.oldloc)
492
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000493 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000494 try:
495 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
496 except locale.Error:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600497 self.skipTest('could not set locale.LC_ALL to fr_FR')
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000498 # This should not cause an exception
499 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
500
Victor Stinner73ea29c2011-01-08 01:56:31 +0000501
Victor Stinner73ea29c2011-01-08 01:56:31 +0000502class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100503 _format = '%d'
504
Victor Stinner73ea29c2011-01-08 01:56:31 +0000505 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000506 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000507
Victor Stinner73ea29c2011-01-08 01:56:31 +0000508 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000509 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000510 self.assertEqual(self.yearstr(12345), '12345')
511 self.assertEqual(self.yearstr(123456789), '123456789')
512
513class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100514
515 # Issue 13305: For years < 1000, the value is not always
516 # padded to 4 digits across platforms. The C standard
517 # assumes year >= 1900, so it does not specify the number
518 # of digits.
519
520 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
521 _format = '%04d'
522 else:
523 _format = '%d'
524
Victor Stinner73ea29c2011-01-08 01:56:31 +0000525 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100526 return time.strftime('%Y', (y,) + (0,) * 8)
527
528 def test_4dyear(self):
529 # Check that we can return the zero padded value.
530 if self._format == '%04d':
531 self.test_year('%04d')
532 else:
533 def year4d(y):
534 return time.strftime('%4Y', (y,) + (0,) * 8)
535 self.test_year('%04d', func=year4d)
536
Florent Xiclunabceb5282011-11-01 14:11:34 +0100537 def skip_if_not_supported(y):
538 msg = "strftime() is limited to [1; 9999] with Visual Studio"
539 # Check that it doesn't crash for year > 9999
540 try:
541 time.strftime('%Y', (y,) + (0,) * 8)
542 except ValueError:
543 cond = False
544 else:
545 cond = True
546 return unittest.skipUnless(cond, msg)
547
548 @skip_if_not_supported(10000)
549 def test_large_year(self):
550 return super().test_large_year()
551
552 @skip_if_not_supported(0)
553 def test_negative(self):
554 return super().test_negative()
555
556 del skip_if_not_supported
557
558
Ezio Melotti3836d702013-04-11 20:29:42 +0300559class _Test4dYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100560 _format = '%d'
561
562 def test_year(self, fmt=None, func=None):
563 fmt = fmt or self._format
564 func = func or self.yearstr
565 self.assertEqual(func(1), fmt % 1)
566 self.assertEqual(func(68), fmt % 68)
567 self.assertEqual(func(69), fmt % 69)
568 self.assertEqual(func(99), fmt % 99)
569 self.assertEqual(func(999), fmt % 999)
570 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000571
572 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100573 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100574 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100575 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
576 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000577
Victor Stinner301f1212011-01-08 03:06:52 +0000578 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100579 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000580 self.assertEqual(self.yearstr(-1234), '-1234')
581 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100582 self.assertEqual(self.yearstr(-123456789), str(-123456789))
583 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100584 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
585 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
586 # Skip the value test, but check that no error is raised
587 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100588 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100589 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000590
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000591
Ezio Melotti3836d702013-04-11 20:29:42 +0300592class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000593 pass
594
Ezio Melotti3836d702013-04-11 20:29:42 +0300595class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase):
Victor Stinner301f1212011-01-08 03:06:52 +0000596 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000597
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000598
Victor Stinner643cd682012-03-02 22:54:03 +0100599class TestPytime(unittest.TestCase):
Victor Stinner5d272cc2012-03-13 13:35:55 +0100600 def setUp(self):
601 self.invalid_values = (
602 -(2 ** 100), 2 ** 100,
603 -(2.0 ** 100.0), 2.0 ** 100.0,
604 )
605
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200606 @support.cpython_only
Victor Stinner5d272cc2012-03-13 13:35:55 +0100607 def test_time_t(self):
608 from _testcapi import pytime_object_to_time_t
Victor Stinner110f9e32015-09-04 10:31:16 +0200609
610 # Conversion giving the same result for all rounding methods
611 for rnd in ALL_ROUNDING_METHODS:
612 for obj, seconds in (
613 # int
614 (-1, -1),
615 (0, 0),
616 (1, 1),
617
618 # float
619 (-1.0, -1),
620 (1.0, 1),
621 ):
622 with self.subTest(obj=obj, round=rnd, seconds=seconds):
623 self.assertEqual(pytime_object_to_time_t(obj, rnd),
624 seconds)
625
626 # Conversion giving different results depending on the rounding method
627 FLOOR = _PyTime.ROUND_FLOOR
628 CEILING = _PyTime.ROUND_CEILING
629 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner3c1b3792014-02-17 00:02:43 +0100630 for obj, time_t, rnd in (
Victor Stinner110f9e32015-09-04 10:31:16 +0200631 (-1.9, -2, FLOOR),
632 (-1.9, -1, CEILING),
633 (-1.9, -2, HALF_UP),
634
635 (1.9, 1, FLOOR),
636 (1.9, 2, CEILING),
637 (1.9, 2, HALF_UP),
638
639 # half up
640 (-0.999, -1, HALF_UP),
641 (-0.510, -1, HALF_UP),
642 (-0.500, -1, HALF_UP),
643 (-0.490, 0, HALF_UP),
644 ( 0.490, 0, HALF_UP),
645 ( 0.500, 1, HALF_UP),
646 ( 0.510, 1, HALF_UP),
647 ( 0.999, 1, HALF_UP),
Victor Stinner5d272cc2012-03-13 13:35:55 +0100648 ):
Victor Stinner3c1b3792014-02-17 00:02:43 +0100649 self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100650
Victor Stinner110f9e32015-09-04 10:31:16 +0200651 # Test OverflowError
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200652 rnd = _PyTime.ROUND_FLOOR
Victor Stinner5d272cc2012-03-13 13:35:55 +0100653 for invalid in self.invalid_values:
Victor Stinner3c1b3792014-02-17 00:02:43 +0100654 self.assertRaises(OverflowError,
655 pytime_object_to_time_t, invalid, rnd)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100656
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200657 @support.cpython_only
Victor Stinneradfefa52015-09-04 23:57:25 +0200658 def test_object_to_timespec(self):
Victor Stinner643cd682012-03-02 22:54:03 +0100659 from _testcapi import pytime_object_to_timespec
Victor Stinner110f9e32015-09-04 10:31:16 +0200660
661 # Conversion giving the same result for all rounding methods
662 for rnd in ALL_ROUNDING_METHODS:
663 for obj, timespec in (
664 # int
665 (0, (0, 0)),
666 (-1, (-1, 0)),
667
668 # float
Victor Stinneradfefa52015-09-04 23:57:25 +0200669 (-1/2**7, (-1, 992187500)),
Victor Stinner110f9e32015-09-04 10:31:16 +0200670 (-1.0, (-1, 0)),
671 (-1e-9, (-1, 999999999)),
672 (1e-9, (0, 1)),
673 (1.0, (1, 0)),
674 ):
675 with self.subTest(obj=obj, round=rnd, timespec=timespec):
676 self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
677
678 # Conversion giving different results depending on the rounding method
679 FLOOR = _PyTime.ROUND_FLOOR
680 CEILING = _PyTime.ROUND_CEILING
681 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner3c1b3792014-02-17 00:02:43 +0100682 for obj, timespec, rnd in (
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200683 # Round towards minus infinity (-inf)
Victor Stinner110f9e32015-09-04 10:31:16 +0200684 (-1e-10, (0, 0), CEILING),
685 (-1e-10, (-1, 999999999), FLOOR),
686 (-1e-10, (0, 0), HALF_UP),
687 (1e-10, (0, 0), FLOOR),
688 (1e-10, (0, 1), CEILING),
689 (1e-10, (0, 0), HALF_UP),
690
691 (0.9999999999, (0, 999999999), FLOOR),
692 (0.9999999999, (1, 0), CEILING),
693
694 (1.1234567890, (1, 123456789), FLOOR),
695 (1.1234567899, (1, 123456789), FLOOR),
Victor Stinneradfefa52015-09-04 23:57:25 +0200696 (-1.1234567890, (-2, 876543210), FLOOR),
Victor Stinner110f9e32015-09-04 10:31:16 +0200697 (-1.1234567891, (-2, 876543210), FLOOR),
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200698 # Round towards infinity (+inf)
Victor Stinner110f9e32015-09-04 10:31:16 +0200699 (1.1234567890, (1, 123456790), CEILING),
700 (1.1234567899, (1, 123456790), CEILING),
701 (-1.1234567890, (-2, 876543211), CEILING),
702 (-1.1234567891, (-2, 876543211), CEILING),
703
704 # half up
705 (-0.6e-9, (-1, 999999999), HALF_UP),
706 # skipped, 0.5e-6 is inexact in base 2
707 #(-0.5e-9, (-1, 999999999), HALF_UP),
708 (-0.4e-9, (0, 0), HALF_UP),
709
710 (0.4e-9, (0, 0), HALF_UP),
711 (0.5e-9, (0, 1), HALF_UP),
712 (0.6e-9, (0, 1), HALF_UP),
Victor Stinner643cd682012-03-02 22:54:03 +0100713 ):
Victor Stinner3c1b3792014-02-17 00:02:43 +0100714 with self.subTest(obj=obj, round=rnd, timespec=timespec):
715 self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
Victor Stinner643cd682012-03-02 22:54:03 +0100716
Victor Stinner110f9e32015-09-04 10:31:16 +0200717 # Test OverflowError
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200718 rnd = _PyTime.ROUND_FLOOR
Victor Stinner5d272cc2012-03-13 13:35:55 +0100719 for invalid in self.invalid_values:
Victor Stinner3c1b3792014-02-17 00:02:43 +0100720 self.assertRaises(OverflowError,
721 pytime_object_to_timespec, invalid, rnd)
Victor Stinner643cd682012-03-02 22:54:03 +0100722
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400723 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
724 def test_localtime_timezone(self):
Victor Stinner643cd682012-03-02 22:54:03 +0100725
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400726 # Get the localtime and examine it for the offset and zone.
727 lt = time.localtime()
728 self.assertTrue(hasattr(lt, "tm_gmtoff"))
729 self.assertTrue(hasattr(lt, "tm_zone"))
730
731 # See if the offset and zone are similar to the module
732 # attributes.
733 if lt.tm_gmtoff is None:
734 self.assertTrue(not hasattr(time, "timezone"))
735 else:
736 self.assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst])
737 if lt.tm_zone is None:
738 self.assertTrue(not hasattr(time, "tzname"))
739 else:
740 self.assertEqual(lt.tm_zone, time.tzname[lt.tm_isdst])
741
742 # Try and make UNIX times from the localtime and a 9-tuple
743 # created from the localtime. Test to see that the times are
744 # the same.
745 t = time.mktime(lt); t9 = time.mktime(lt[:9])
746 self.assertEqual(t, t9)
747
748 # Make localtimes from the UNIX times and compare them to
749 # the original localtime, thus making a round trip.
750 new_lt = time.localtime(t); new_lt9 = time.localtime(t9)
751 self.assertEqual(new_lt, lt)
752 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
753 self.assertEqual(new_lt.tm_zone, lt.tm_zone)
754 self.assertEqual(new_lt9, lt)
755 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
756 self.assertEqual(new_lt9.tm_zone, lt.tm_zone)
757
758 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
759 def test_strptime_timezone(self):
760 t = time.strptime("UTC", "%Z")
761 self.assertEqual(t.tm_zone, 'UTC')
762 t = time.strptime("+0500", "%z")
763 self.assertEqual(t.tm_gmtoff, 5 * 3600)
764
765 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
766 def test_short_times(self):
767
768 import pickle
769
770 # Load a short time structure using pickle.
771 st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\nI1\ntp1\n(dp2\ntp3\nRp4\n."
772 lt = pickle.loads(st)
773 self.assertIs(lt.tm_gmtoff, None)
774 self.assertIs(lt.tm_zone, None)
Victor Stinner643cd682012-03-02 22:54:03 +0100775
Fred Drake2e2be372001-09-20 21:33:42 +0000776
Victor Stinner34dc0f42015-03-27 18:19:03 +0100777@unittest.skipUnless(_testcapi is not None,
778 'need the _testcapi module')
Victor Stinner992c43f2015-03-27 17:12:45 +0100779class TestPyTime_t(unittest.TestCase):
Victor Stinneracea9f62015-09-02 10:39:40 +0200780 """
781 Test the _PyTime_t API.
782 """
Victor Stinner13019fd2015-04-03 13:10:54 +0200783 def test_FromSeconds(self):
784 from _testcapi import PyTime_FromSeconds
785 for seconds in (0, 3, -456, _testcapi.INT_MAX, _testcapi.INT_MIN):
786 with self.subTest(seconds=seconds):
787 self.assertEqual(PyTime_FromSeconds(seconds),
788 seconds * SEC_TO_NS)
789
Victor Stinner992c43f2015-03-27 17:12:45 +0100790 def test_FromSecondsObject(self):
Victor Stinner4bfb4602015-03-27 22:27:24 +0100791 from _testcapi import PyTime_FromSecondsObject
Victor Stinner992c43f2015-03-27 17:12:45 +0100792
793 # Conversion giving the same result for all rounding methods
794 for rnd in ALL_ROUNDING_METHODS:
795 for obj, ts in (
796 # integers
797 (0, 0),
798 (1, SEC_TO_NS),
799 (-3, -3 * SEC_TO_NS),
800
801 # float: subseconds
802 (0.0, 0),
803 (1e-9, 1),
804 (1e-6, 10 ** 3),
805 (1e-3, 10 ** 6),
806
807 # float: seconds
808 (2.0, 2 * SEC_TO_NS),
809 (123.0, 123 * SEC_TO_NS),
810 (-7.0, -7 * SEC_TO_NS),
811
Victor Stinner74474232015-09-02 01:43:56 +0200812 # nanosecond are kept for value <= 2^23 seconds,
813 # except 2**23-1e-9 with HALF_UP
Victor Stinner992c43f2015-03-27 17:12:45 +0100814 (2**22 - 1e-9, 4194303999999999),
815 (2**22, 4194304000000000),
816 (2**22 + 1e-9, 4194304000000001),
Victor Stinner992c43f2015-03-27 17:12:45 +0100817 (2**23, 8388608000000000),
818
819 # start loosing precision for value > 2^23 seconds
820 (2**23 + 1e-9, 8388608000000002),
821
822 # nanoseconds are lost for value > 2^23 seconds
823 (2**24 - 1e-9, 16777215999999998),
824 (2**24, 16777216000000000),
825 (2**24 + 1e-9, 16777216000000000),
826 (2**25 - 1e-9, 33554432000000000),
827 (2**25 , 33554432000000000),
828 (2**25 + 1e-9, 33554432000000000),
829
Victor Stinner4bfb4602015-03-27 22:27:24 +0100830 # close to 2^63 nanoseconds (_PyTime_t limit)
Victor Stinner992c43f2015-03-27 17:12:45 +0100831 (9223372036, 9223372036 * SEC_TO_NS),
832 (9223372036.0, 9223372036 * SEC_TO_NS),
833 (-9223372036, -9223372036 * SEC_TO_NS),
834 (-9223372036.0, -9223372036 * SEC_TO_NS),
835 ):
836 with self.subTest(obj=obj, round=rnd, timestamp=ts):
Victor Stinner4bfb4602015-03-27 22:27:24 +0100837 self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
Victor Stinner992c43f2015-03-27 17:12:45 +0100838
839 with self.subTest(round=rnd):
840 with self.assertRaises(OverflowError):
Victor Stinner4bfb4602015-03-27 22:27:24 +0100841 PyTime_FromSecondsObject(9223372037, rnd)
842 PyTime_FromSecondsObject(9223372037.0, rnd)
843 PyTime_FromSecondsObject(-9223372037, rnd)
844 PyTime_FromSecondsObject(-9223372037.0, rnd)
Victor Stinner992c43f2015-03-27 17:12:45 +0100845
846 # Conversion giving different results depending on the rounding method
Victor Stinner02937aa2015-03-28 05:02:39 +0100847 FLOOR = _PyTime.ROUND_FLOOR
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200848 CEILING = _PyTime.ROUND_CEILING
Victor Stinner74474232015-09-02 01:43:56 +0200849 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner992c43f2015-03-27 17:12:45 +0100850 for obj, ts, rnd in (
851 # close to zero
Victor Stinner02937aa2015-03-28 05:02:39 +0100852 ( 1e-10, 0, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200853 ( 1e-10, 1, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200854 ( 1e-10, 0, HALF_UP),
Victor Stinner02937aa2015-03-28 05:02:39 +0100855 (-1e-10, -1, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200856 (-1e-10, 0, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200857 (-1e-10, 0, HALF_UP),
Victor Stinner992c43f2015-03-27 17:12:45 +0100858
859 # test rounding of the last nanosecond
Victor Stinner02937aa2015-03-28 05:02:39 +0100860 ( 1.1234567899, 1123456789, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200861 ( 1.1234567899, 1123456790, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200862 ( 1.1234567899, 1123456790, HALF_UP),
Victor Stinner02937aa2015-03-28 05:02:39 +0100863 (-1.1234567899, -1123456790, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200864 (-1.1234567899, -1123456789, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200865 (-1.1234567899, -1123456790, HALF_UP),
Victor Stinner992c43f2015-03-27 17:12:45 +0100866
867 # close to 1 second
Victor Stinner02937aa2015-03-28 05:02:39 +0100868 ( 0.9999999999, 999999999, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200869 ( 0.9999999999, 1000000000, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200870 ( 0.9999999999, 1000000000, HALF_UP),
Victor Stinner02937aa2015-03-28 05:02:39 +0100871 (-0.9999999999, -1000000000, FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200872 (-0.9999999999, -999999999, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200873 (-0.9999999999, -1000000000, HALF_UP),
874
875 # close to 2^23 seconds
876 (2**23 - 1e-9, 8388607999999999, FLOOR),
877 (2**23 - 1e-9, 8388607999999999, CEILING),
Victor Stinner8aad8d62015-09-02 13:54:28 +0200878 # Issue #23517: skip HALF_UP test because the result is different
879 # depending on the FPU and how the compiler optimize the code :-/
880 #(2**23 - 1e-9, 8388608000000000, HALF_UP),
Victor Stinner992c43f2015-03-27 17:12:45 +0100881 ):
882 with self.subTest(obj=obj, round=rnd, timestamp=ts):
Victor Stinner4bfb4602015-03-27 22:27:24 +0100883 self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
884
885 def test_AsSecondsDouble(self):
886 from _testcapi import PyTime_AsSecondsDouble
887
888 for nanoseconds, seconds in (
889 # near 1 nanosecond
890 ( 0, 0.0),
891 ( 1, 1e-9),
892 (-1, -1e-9),
893
894 # near 1 second
895 (SEC_TO_NS + 1, 1.0 + 1e-9),
896 (SEC_TO_NS, 1.0),
897 (SEC_TO_NS - 1, 1.0 - 1e-9),
898
899 # a few seconds
900 (123 * SEC_TO_NS, 123.0),
901 (-567 * SEC_TO_NS, -567.0),
902
903 # nanosecond are kept for value <= 2^23 seconds
904 (4194303999999999, 2**22 - 1e-9),
905 (4194304000000000, 2**22),
906 (4194304000000001, 2**22 + 1e-9),
907
908 # start loosing precision for value > 2^23 seconds
909 (8388608000000002, 2**23 + 1e-9),
910
911 # nanoseconds are lost for value > 2^23 seconds
912 (16777215999999998, 2**24 - 1e-9),
913 (16777215999999999, 2**24 - 1e-9),
914 (16777216000000000, 2**24 ),
915 (16777216000000001, 2**24 ),
916 (16777216000000002, 2**24 + 2e-9),
917
918 (33554432000000000, 2**25 ),
919 (33554432000000002, 2**25 ),
920 (33554432000000004, 2**25 + 4e-9),
921
922 # close to 2^63 nanoseconds (_PyTime_t limit)
923 (9223372036 * SEC_TO_NS, 9223372036.0),
924 (-9223372036 * SEC_TO_NS, -9223372036.0),
925 ):
926 with self.subTest(nanoseconds=nanoseconds, seconds=seconds):
927 self.assertEqual(PyTime_AsSecondsDouble(nanoseconds),
928 seconds)
Victor Stinner992c43f2015-03-27 17:12:45 +0100929
Victor Stinner95e9cef2015-03-28 01:26:47 +0100930 def test_timeval(self):
931 from _testcapi import PyTime_AsTimeval
932 for rnd in ALL_ROUNDING_METHODS:
933 for ns, tv in (
934 # microseconds
935 (0, (0, 0)),
936 (1000, (0, 1)),
937 (-1000, (-1, 999999)),
938
939 # seconds
940 (2 * SEC_TO_NS, (2, 0)),
941 (-3 * SEC_TO_NS, (-3, 0)),
Victor Stinner95e9cef2015-03-28 01:26:47 +0100942 ):
943 with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
944 self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
945
Victor Stinner02937aa2015-03-28 05:02:39 +0100946 FLOOR = _PyTime.ROUND_FLOOR
Victor Stinnerbcdd7772015-03-30 03:52:49 +0200947 CEILING = _PyTime.ROUND_CEILING
Victor Stinner74474232015-09-02 01:43:56 +0200948 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner95e9cef2015-03-28 01:26:47 +0100949 for ns, tv, rnd in (
950 # nanoseconds
Victor Stinner02937aa2015-03-28 05:02:39 +0100951 (1, (0, 0), FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200952 (1, (0, 1), CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200953 (1, (0, 0), HALF_UP),
Victor Stinner02937aa2015-03-28 05:02:39 +0100954 (-1, (-1, 999999), FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200955 (-1, (0, 0), CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200956 (-1, (0, 0), HALF_UP),
Victor Stinner95e9cef2015-03-28 01:26:47 +0100957
958 # seconds + nanoseconds
Victor Stinner02937aa2015-03-28 05:02:39 +0100959 (1234567001, (1, 234567), FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200960 (1234567001, (1, 234568), CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200961 (1234567001, (1, 234567), HALF_UP),
Victor Stinner02937aa2015-03-28 05:02:39 +0100962 (-1234567001, (-2, 765432), FLOOR),
Victor Stinnera695f832015-03-30 03:57:14 +0200963 (-1234567001, (-2, 765433), CEILING),
Victor Stinner74474232015-09-02 01:43:56 +0200964 (-1234567001, (-2, 765433), HALF_UP),
965
966 # half up
967 (499, (0, 0), HALF_UP),
968 (500, (0, 1), HALF_UP),
969 (501, (0, 1), HALF_UP),
970 (999, (0, 1), HALF_UP),
971 (-499, (0, 0), HALF_UP),
972 (-500, (0, 0), HALF_UP),
973 (-501, (-1, 999999), HALF_UP),
974 (-999, (-1, 999999), HALF_UP),
Victor Stinner95e9cef2015-03-28 01:26:47 +0100975 ):
976 with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
977 self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
978
Victor Stinner34dc0f42015-03-27 18:19:03 +0100979 @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'),
980 'need _testcapi.PyTime_AsTimespec')
981 def test_timespec(self):
982 from _testcapi import PyTime_AsTimespec
983 for ns, ts in (
984 # nanoseconds
985 (0, (0, 0)),
986 (1, (0, 1)),
987 (-1, (-1, 999999999)),
988
989 # seconds
990 (2 * SEC_TO_NS, (2, 0)),
991 (-3 * SEC_TO_NS, (-3, 0)),
992
993 # seconds + nanoseconds
994 (1234567890, (1, 234567890)),
995 (-1234567890, (-2, 765432110)),
996 ):
997 with self.subTest(nanoseconds=ns, timespec=ts):
998 self.assertEqual(PyTime_AsTimespec(ns), ts)
999
Victor Stinner62d1c702015-04-01 17:47:07 +02001000 def test_milliseconds(self):
1001 from _testcapi import PyTime_AsMilliseconds
1002 for rnd in ALL_ROUNDING_METHODS:
1003 for ns, tv in (
1004 # milliseconds
1005 (1 * MS_TO_NS, 1),
1006 (-2 * MS_TO_NS, -2),
1007
1008 # seconds
1009 (2 * SEC_TO_NS, 2000),
1010 (-3 * SEC_TO_NS, -3000),
1011 ):
1012 with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
1013 self.assertEqual(PyTime_AsMilliseconds(ns, rnd), tv)
1014
1015 FLOOR = _PyTime.ROUND_FLOOR
1016 CEILING = _PyTime.ROUND_CEILING
Victor Stinner74474232015-09-02 01:43:56 +02001017 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner62d1c702015-04-01 17:47:07 +02001018 for ns, ms, rnd in (
1019 # nanoseconds
1020 (1, 0, FLOOR),
1021 (1, 1, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001022 (1, 0, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001023 (-1, 0, FLOOR),
1024 (-1, -1, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001025 (-1, 0, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001026
1027 # seconds + nanoseconds
1028 (1234 * MS_TO_NS + 1, 1234, FLOOR),
1029 (1234 * MS_TO_NS + 1, 1235, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001030 (1234 * MS_TO_NS + 1, 1234, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001031 (-1234 * MS_TO_NS - 1, -1234, FLOOR),
1032 (-1234 * MS_TO_NS - 1, -1235, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001033 (-1234 * MS_TO_NS - 1, -1234, HALF_UP),
1034
1035 # half up
1036 (499999, 0, HALF_UP),
1037 (499999, 0, HALF_UP),
1038 (500000, 1, HALF_UP),
1039 (999999, 1, HALF_UP),
1040 (-499999, 0, HALF_UP),
1041 (-500000, -1, HALF_UP),
1042 (-500001, -1, HALF_UP),
1043 (-999999, -1, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001044 ):
1045 with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
1046 self.assertEqual(PyTime_AsMilliseconds(ns, rnd), ms)
1047
1048 def test_microseconds(self):
1049 from _testcapi import PyTime_AsMicroseconds
1050 for rnd in ALL_ROUNDING_METHODS:
1051 for ns, tv in (
1052 # microseconds
1053 (1 * US_TO_NS, 1),
1054 (-2 * US_TO_NS, -2),
1055
1056 # milliseconds
1057 (1 * MS_TO_NS, 1000),
1058 (-2 * MS_TO_NS, -2000),
1059
1060 # seconds
1061 (2 * SEC_TO_NS, 2000000),
1062 (-3 * SEC_TO_NS, -3000000),
1063 ):
1064 with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
1065 self.assertEqual(PyTime_AsMicroseconds(ns, rnd), tv)
1066
1067 FLOOR = _PyTime.ROUND_FLOOR
1068 CEILING = _PyTime.ROUND_CEILING
Victor Stinner74474232015-09-02 01:43:56 +02001069 HALF_UP = _PyTime.ROUND_HALF_UP
Victor Stinner62d1c702015-04-01 17:47:07 +02001070 for ns, ms, rnd in (
1071 # nanoseconds
1072 (1, 0, FLOOR),
1073 (1, 1, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001074 (1, 0, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001075 (-1, 0, FLOOR),
1076 (-1, -1, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001077 (-1, 0, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001078
1079 # seconds + nanoseconds
1080 (1234 * US_TO_NS + 1, 1234, FLOOR),
1081 (1234 * US_TO_NS + 1, 1235, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001082 (1234 * US_TO_NS + 1, 1234, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001083 (-1234 * US_TO_NS - 1, -1234, FLOOR),
1084 (-1234 * US_TO_NS - 1, -1235, CEILING),
Victor Stinner74474232015-09-02 01:43:56 +02001085 (-1234 * US_TO_NS - 1, -1234, HALF_UP),
1086
1087 # half up
1088 (1499, 1, HALF_UP),
1089 (1500, 2, HALF_UP),
1090 (1501, 2, HALF_UP),
1091 (-1499, -1, HALF_UP),
1092 (-1500, -2, HALF_UP),
1093 (-1501, -2, HALF_UP),
Victor Stinner62d1c702015-04-01 17:47:07 +02001094 ):
1095 with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
1096 self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms)
1097
Victor Stinner992c43f2015-03-27 17:12:45 +01001098
Victor Stinneracea9f62015-09-02 10:39:40 +02001099@unittest.skipUnless(_testcapi is not None,
1100 'need the _testcapi module')
1101class TestOldPyTime(unittest.TestCase):
1102 """
1103 Test the old _PyTime_t API: _PyTime_ObjectToXXX() functions.
1104 """
1105 def setUp(self):
1106 self.invalid_values = (
1107 -(2 ** 100), 2 ** 100,
1108 -(2.0 ** 100.0), 2.0 ** 100.0,
1109 )
1110
1111 @support.cpython_only
1112 def test_time_t(self):
1113 from _testcapi import pytime_object_to_time_t
1114
1115 # Conversion giving the same result for all rounding methods
1116 for rnd in ALL_ROUNDING_METHODS:
1117 for obj, time_t in (
1118 # int
1119 (0, 0),
1120 (-1, -1),
1121
1122 # float
1123 (1.0, 1),
1124 (-1.0, -1),
1125 ):
1126 self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
1127
1128
1129 # Conversion giving different results depending on the rounding method
1130 FLOOR = _PyTime.ROUND_FLOOR
1131 CEILING = _PyTime.ROUND_CEILING
1132 HALF_UP = _PyTime.ROUND_HALF_UP
1133 for obj, time_t, rnd in (
1134 (-1.9, -2, FLOOR),
1135 (-1.9, -2, HALF_UP),
1136 (-1.9, -1, CEILING),
Victor Stinneread144c2015-09-02 11:05:32 +02001137
Victor Stinneracea9f62015-09-02 10:39:40 +02001138 (1.9, 1, FLOOR),
1139 (1.9, 2, HALF_UP),
1140 (1.9, 2, CEILING),
Victor Stinneread144c2015-09-02 11:05:32 +02001141
1142 (-0.6, -1, HALF_UP),
1143 (-0.5, -1, HALF_UP),
1144 (-0.4, 0, HALF_UP),
1145
1146 (0.4, 0, HALF_UP),
1147 (0.5, 1, HALF_UP),
1148 (0.6, 1, HALF_UP),
Victor Stinneracea9f62015-09-02 10:39:40 +02001149 ):
1150 self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
1151
1152 # Test OverflowError
1153 rnd = _PyTime.ROUND_FLOOR
1154 for invalid in self.invalid_values:
1155 self.assertRaises(OverflowError,
1156 pytime_object_to_time_t, invalid, rnd)
1157
Victor Stinneradfefa52015-09-04 23:57:25 +02001158 def test_object_to_timeval(self):
Victor Stinneracea9f62015-09-02 10:39:40 +02001159 from _testcapi import pytime_object_to_timeval
1160
1161 # Conversion giving the same result for all rounding methods
1162 for rnd in ALL_ROUNDING_METHODS:
1163 for obj, timeval in (
1164 # int
1165 (0, (0, 0)),
1166 (-1, (-1, 0)),
1167
1168 # float
1169 (-1.0, (-1, 0)),
Victor Stinneradfefa52015-09-04 23:57:25 +02001170 (1/2**6, (0, 15625)),
1171 (-1/2**6, (-1, 984375)),
Victor Stinneracea9f62015-09-02 10:39:40 +02001172 (-1e-6, (-1, 999999)),
1173 (1e-6, (0, 1)),
1174 ):
1175 with self.subTest(obj=obj, round=rnd, timeval=timeval):
1176 self.assertEqual(pytime_object_to_timeval(obj, rnd),
1177 timeval)
1178
1179 # Conversion giving different results depending on the rounding method
1180 FLOOR = _PyTime.ROUND_FLOOR
1181 CEILING = _PyTime.ROUND_CEILING
1182 HALF_UP = _PyTime.ROUND_HALF_UP
1183 for obj, timeval, rnd in (
1184 (-1e-7, (-1, 999999), FLOOR),
1185 (-1e-7, (0, 0), CEILING),
1186 (-1e-7, (0, 0), HALF_UP),
1187
1188 (1e-7, (0, 0), FLOOR),
1189 (1e-7, (0, 1), CEILING),
1190 (1e-7, (0, 0), HALF_UP),
1191
Victor Stinneracea9f62015-09-02 10:39:40 +02001192 (0.9999999, (0, 999999), FLOOR),
1193 (0.9999999, (1, 0), CEILING),
1194 (0.9999999, (1, 0), HALF_UP),
Victor Stinneread144c2015-09-02 11:05:32 +02001195
1196 (-0.6e-6, (-1, 999999), HALF_UP),
1197 # skipped, -0.5e-6 is inexact in base 2
1198 #(-0.5e-6, (-1, 999999), HALF_UP),
1199 (-0.4e-6, (0, 0), HALF_UP),
1200
1201 (0.4e-6, (0, 0), HALF_UP),
1202 # skipped, 0.5e-6 is inexact in base 2
1203 #(0.5e-6, (0, 1), HALF_UP),
1204 (0.6e-6, (0, 1), HALF_UP),
Victor Stinneracea9f62015-09-02 10:39:40 +02001205 ):
1206 with self.subTest(obj=obj, round=rnd, timeval=timeval):
1207 self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)
1208
1209 rnd = _PyTime.ROUND_FLOOR
1210 for invalid in self.invalid_values:
1211 self.assertRaises(OverflowError,
1212 pytime_object_to_timeval, invalid, rnd)
1213
1214 @support.cpython_only
1215 def test_timespec(self):
1216 from _testcapi import pytime_object_to_timespec
1217
1218 # Conversion giving the same result for all rounding methods
1219 for rnd in ALL_ROUNDING_METHODS:
1220 for obj, timespec in (
1221 # int
1222 (0, (0, 0)),
1223 (-1, (-1, 0)),
1224
1225 # float
1226 (-1.0, (-1, 0)),
1227 (-1e-9, (-1, 999999999)),
1228 (1e-9, (0, 1)),
Victor Stinneradfefa52015-09-04 23:57:25 +02001229 (-1/2**9, (-1, 998046875)),
Victor Stinneracea9f62015-09-02 10:39:40 +02001230 ):
1231 with self.subTest(obj=obj, round=rnd, timespec=timespec):
1232 self.assertEqual(pytime_object_to_timespec(obj, rnd),
1233 timespec)
1234
1235 # Conversion giving different results depending on the rounding method
1236 FLOOR = _PyTime.ROUND_FLOOR
1237 CEILING = _PyTime.ROUND_CEILING
1238 HALF_UP = _PyTime.ROUND_HALF_UP
1239 for obj, timespec, rnd in (
1240 (-1e-10, (-1, 999999999), FLOOR),
1241 (-1e-10, (0, 0), CEILING),
1242 (-1e-10, (0, 0), HALF_UP),
1243
1244 (1e-10, (0, 0), FLOOR),
1245 (1e-10, (0, 1), CEILING),
1246 (1e-10, (0, 0), HALF_UP),
1247
Victor Stinneracea9f62015-09-02 10:39:40 +02001248 (0.9999999999, (0, 999999999), FLOOR),
1249 (0.9999999999, (1, 0), CEILING),
1250 (0.9999999999, (1, 0), HALF_UP),
Victor Stinneread144c2015-09-02 11:05:32 +02001251
1252 (-0.6e-9, (-1, 999999999), HALF_UP),
1253 # skipped, 0.5e-6 is inexact in base 2
1254 #(-0.5e-9, (-1, 999999999), HALF_UP),
1255 (-0.4e-9, (0, 0), HALF_UP),
1256
1257 (0.4e-9, (0, 0), HALF_UP),
1258 (0.5e-9, (0, 1), HALF_UP),
1259 (0.6e-9, (0, 1), HALF_UP),
Victor Stinneracea9f62015-09-02 10:39:40 +02001260 ):
1261 with self.subTest(obj=obj, round=rnd, timespec=timespec):
1262 self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
1263
1264 # Test OverflowError
1265 rnd = FLOOR
1266 for invalid in self.invalid_values:
1267 self.assertRaises(OverflowError,
1268 pytime_object_to_timespec, invalid, rnd)
1269
1270
Fred Drake2e2be372001-09-20 21:33:42 +00001271if __name__ == "__main__":
Ezio Melotti3836d702013-04-11 20:29:42 +03001272 unittest.main()