Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 2 | import enum |
| 3 | import locale |
| 4 | import platform |
| 5 | import sys |
| 6 | import sysconfig |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 7 | import time |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 8 | import unittest |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 9 | try: |
| 10 | import threading |
| 11 | except ImportError: |
| 12 | threading = None |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 13 | try: |
| 14 | import _testcapi |
| 15 | except ImportError: |
| 16 | _testcapi = None |
| 17 | |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 18 | |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 19 | # Max year is only limited by the size of C int. |
| 20 | SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 |
| 21 | TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1 |
| 22 | TIME_MINYEAR = -TIME_MAXYEAR - 1 |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 23 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 24 | US_TO_NS = 10 ** 3 |
| 25 | MS_TO_NS = 10 ** 6 |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 26 | SEC_TO_NS = 10 ** 9 |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 27 | |
| 28 | class _PyTime(enum.IntEnum): |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 29 | # Round towards minus infinity (-inf) |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 30 | ROUND_FLOOR = 0 |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 31 | # Round towards infinity (+inf) |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 32 | ROUND_CEILING = 1 |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 33 | # Round to nearest with ties going away from zero |
| 34 | ROUND_HALF_UP = 2 |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 35 | |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 36 | ALL_ROUNDING_METHODS = (_PyTime.ROUND_FLOOR, _PyTime.ROUND_CEILING, |
| 37 | _PyTime.ROUND_HALF_UP) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 38 | |
| 39 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 40 | class TimeTestCase(unittest.TestCase): |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 41 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 42 | def setUp(self): |
| 43 | self.t = time.time() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 44 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 45 | def test_data_attributes(self): |
| 46 | time.altzone |
| 47 | time.daylight |
| 48 | time.timezone |
| 49 | time.tzname |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 50 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 51 | def test_time(self): |
| 52 | time.time() |
| 53 | info = time.get_clock_info('time') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 54 | self.assertFalse(info.monotonic) |
Victor Stinner | 6222d76 | 2012-06-12 23:04:11 +0200 | [diff] [blame] | 55 | self.assertTrue(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 56 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 57 | def test_clock(self): |
| 58 | time.clock() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 59 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 60 | info = time.get_clock_info('clock') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 61 | self.assertTrue(info.monotonic) |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 62 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 63 | |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 64 | @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 Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 85 | @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 Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 94 | if hasattr(time, 'CLOCK_MONOTONIC'): |
| 95 | self.assertRaises(OSError, |
| 96 | time.clock_settime, time.CLOCK_MONOTONIC, 0) |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 97 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 98 | def test_conversions(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 99 | 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 Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 103 | |
| 104 | def test_sleep(self): |
Victor Stinner | 7f53a50 | 2011-07-05 22:00:25 +0200 | [diff] [blame] | 105 | self.assertRaises(ValueError, time.sleep, -2) |
| 106 | self.assertRaises(ValueError, time.sleep, -1) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 107 | 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 Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 120 | def _bounds_checking(self, func): |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 121 | # Make sure that strftime() checks the bounds of the various parts |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 122 | # of the time tuple (0 is valid for *all* values). |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 123 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 124 | # The year field is tested by other test cases above |
| 125 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 126 | # Check month [1, 12] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 127 | func((1900, 0, 1, 0, 0, 0, 0, 1, -1)) |
| 128 | func((1900, 12, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 129 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 130 | (1900, -1, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 131 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 132 | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 133 | # Check day of month [1, 31] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 134 | func((1900, 1, 0, 0, 0, 0, 0, 1, -1)) |
| 135 | func((1900, 1, 31, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 136 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 137 | (1900, 1, -1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 138 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 139 | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 140 | # Check hour [0, 23] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 141 | func((1900, 1, 1, 23, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 142 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 143 | (1900, 1, 1, -1, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 144 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 145 | (1900, 1, 1, 24, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 146 | # Check minute [0, 59] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 147 | func((1900, 1, 1, 0, 59, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 148 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 149 | (1900, 1, 1, 0, -1, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 150 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 151 | (1900, 1, 1, 0, 60, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 152 | # Check second [0, 61] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 153 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 154 | (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 Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 157 | func((1900, 1, 1, 0, 0, 60, 0, 1, -1)) |
| 158 | func((1900, 1, 1, 0, 0, 61, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 159 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 160 | (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 Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 165 | self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)), |
| 166 | func((1900, 1, 1, 0, 0, 0, +6, 1, -1))) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 167 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 168 | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 169 | # Check day of the year [1, 366] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 170 | func((1900, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 171 | func((1900, 1, 1, 0, 0, 0, 0, 366, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 172 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 173 | (1900, 1, 1, 0, 0, 0, 0, -1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 174 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 175 | (1900, 1, 1, 0, 0, 0, 0, 367, -1)) |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 176 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 177 | def test_strftime_bounding_check(self): |
| 178 | self._bounds_checking(lambda tup: time.strftime('', tup)) |
| 179 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 180 | def test_default_values_for_zero(self): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 181 | # 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 185 | expected = "2000 01 01 00 00 00 1 001" |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 186 | with support.check_warnings(): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 187 | result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 188 | self.assertEqual(expected, result) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 190 | def test_strptime(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 191 | # Should be able to go round-trip from strftime to strptime without |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 192 | # raising an exception. |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 193 | 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 Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 197 | format = '%' + directive |
| 198 | strf_output = time.strftime(format, tt) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 199 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 200 | time.strptime(strf_output, format) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 201 | except ValueError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 202 | self.fail("conversion specifier %r failed with '%s' input." % |
| 203 | (format, strf_output)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 204 | |
Brett Cannon | 7f6b4f8 | 2009-03-30 21:30:26 +0000 | [diff] [blame] | 205 | 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 Melotti | 0f38908 | 2013-04-04 02:09:20 +0300 | [diff] [blame] | 210 | 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 Storchaka | cdac302 | 2013-11-24 18:15:37 +0200 | [diff] [blame] | 215 | # 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 Melotti | 0f38908 | 2013-04-04 02:09:20 +0300 | [diff] [blame] | 219 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 220 | def test_asctime(self): |
| 221 | time.asctime(time.gmtime(self.t)) |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 222 | |
| 223 | # Max year is only limited by the size of C int. |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 224 | 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 Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 231 | self.assertRaises(TypeError, time.asctime, 0) |
Alexander Belopolsky | e2dc082 | 2011-01-02 20:48:22 +0000 | [diff] [blame] | 232 | self.assertRaises(TypeError, time.asctime, ()) |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 233 | self.assertRaises(TypeError, time.asctime, (0,) * 10) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 234 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 235 | def test_asctime_bounding_check(self): |
| 236 | self._bounds_checking(time.asctime) |
| 237 | |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 238 | def test_ctime(self): |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 239 | 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 Stinner | 1ac4261 | 2014-02-21 09:27:17 +0100 | [diff] [blame] | 243 | for year in [-100, 100, 1000, 2000, 2050, 10000]: |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 244 | 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 Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 252 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 253 | @unittest.skipUnless(hasattr(time, "tzset"), |
| 254 | "time module has no attribute tzset") |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 255 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 257 | from os import environ |
| 258 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 259 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 260 | # hemisphere. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 261 | xmas2002 = 1040774400.0 |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 262 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 263 | # 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 Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 268 | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 269 | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
| 270 | utc='UTC+0' |
| 271 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 272 | org_TZ = environ.get('TZ',None) |
| 273 | try: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 274 | # Make sure we can switch to UTC time and results are correct |
| 275 | # Note that unknown timezones default to UTC. |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 276 | # 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 281 | self.assertEqual( |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 282 | time.gmtime(xmas2002), time.localtime(xmas2002) |
| 283 | ) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 284 | self.assertEqual(time.daylight, 0) |
| 285 | self.assertEqual(time.timezone, 0) |
| 286 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 287 | |
| 288 | # Make sure we can switch to US/Eastern |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 289 | environ['TZ'] = eastern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 290 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 291 | 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 Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 299 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 300 | # Now go to the southern hemisphere. |
| 301 | environ['TZ'] = victoria |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 302 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 303 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
Victor Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 304 | |
| 305 | # Issue #11886: Australian Eastern Standard Time (UTC+10) is called |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 306 | # "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 Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 311 | self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 312 | self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 313 | 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 Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 319 | 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 Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 324 | elif 'TZ' in environ: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 325 | del environ['TZ'] |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 326 | time.tzset() |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 327 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 328 | 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 Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 335 | self.assertRaises(OverflowError, func, unreasonable) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 336 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 337 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 345 | gt0 = time.gmtime() |
| 346 | gt1 = time.gmtime(None) |
| 347 | t0 = time.mktime(gt0) |
| 348 | t1 = time.mktime(gt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 349 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 350 | |
| 351 | def test_localtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 352 | lt0 = time.localtime() |
| 353 | lt1 = time.localtime(None) |
| 354 | t0 = time.mktime(lt0) |
| 355 | t1 = time.mktime(lt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 356 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 357 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 358 | def test_mktime(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 359 | # Issue #1726687 |
| 360 | for t in (-2, -1, 0, 1): |
Victor Stinner | 8c8b4e0 | 2014-02-21 23:54:32 +0100 | [diff] [blame] | 361 | 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 Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 368 | try: |
| 369 | tt = time.localtime(t) |
Victor Stinner | 2cbae98 | 2012-01-27 00:50:33 +0100 | [diff] [blame] | 370 | except (OverflowError, OSError): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 371 | pass |
| 372 | else: |
| 373 | self.assertEqual(time.mktime(tt), t) |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 374 | |
| 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 Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 380 | # 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 Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 383 | tt = time.gmtime(self.t) |
| 384 | tzname = time.strftime('%Z', tt) |
| 385 | self.assertNotEqual(tzname, 'LMT') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 386 | try: |
| 387 | time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) |
| 388 | except OverflowError: |
| 389 | pass |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 390 | self.assertEqual(time.strftime('%Z', tt), tzname) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 391 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 392 | @unittest.skipUnless(hasattr(time, 'monotonic'), |
| 393 | 'need time.monotonic') |
| 394 | def test_monotonic(self): |
Victor Stinner | 6c86181 | 2013-11-23 00:15:27 +0100 | [diff] [blame] | 395 | # 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 Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 403 | t1 = time.monotonic() |
Victor Stinner | a9c99a6 | 2013-07-03 23:07:37 +0200 | [diff] [blame] | 404 | time.sleep(0.5) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 405 | t2 = time.monotonic() |
Victor Stinner | 2dd254d | 2012-01-20 02:24:18 +0100 | [diff] [blame] | 406 | dt = t2 - t1 |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 407 | self.assertGreater(t2, t1) |
Zachary Ware | 487aedb | 2014-01-02 09:41:10 -0600 | [diff] [blame] | 408 | # Issue #20101: On some Windows machines, dt may be slightly low |
| 409 | self.assertTrue(0.45 <= dt <= 1.0, dt) |
Antoine Pitrou | 391166f | 2012-01-18 22:35:21 +0100 | [diff] [blame] | 410 | |
Victor Stinner | 6c86181 | 2013-11-23 00:15:27 +0100 | [diff] [blame] | 411 | # monotonic() is a monotonic but non adjustable clock |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 412 | info = time.get_clock_info('monotonic') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 413 | self.assertTrue(info.monotonic) |
Victor Stinner | 6222d76 | 2012-06-12 23:04:11 +0200 | [diff] [blame] | 414 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 415 | |
| 416 | def test_perf_counter(self): |
| 417 | time.perf_counter() |
| 418 | |
| 419 | def test_process_time(self): |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 420 | # process_time() should not include time spend during a sleep |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 421 | start = time.process_time() |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 422 | time.sleep(0.100) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 423 | stop = time.process_time() |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 424 | # use 20 ms because process_time() has usually a resolution of 15 ms |
| 425 | # on Windows |
| 426 | self.assertLess(stop - start, 0.020) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 427 | |
| 428 | info = time.get_clock_info('process_time') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 429 | self.assertTrue(info.monotonic) |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 430 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 431 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 432 | @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 Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 440 | try: |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 441 | 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 Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 447 | self.assertGreaterEqual(t2, t1) |
| 448 | |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 449 | def test_localtime_failure(self): |
| 450 | # Issue #13847: check for localtime() failure |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 451 | invalid_time_t = None |
| 452 | for time_t in (-1, 2**30, 2**33, 2**60): |
| 453 | try: |
| 454 | time.localtime(time_t) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 455 | except OverflowError: |
| 456 | self.skipTest("need 64-bit time_t") |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 457 | 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 Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 463 | self.assertRaises(OSError, time.localtime, invalid_time_t) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 464 | self.assertRaises(OSError, time.ctime, invalid_time_t) |
Victor Stinner | b94b266 | 2012-01-18 01:50:21 +0100 | [diff] [blame] | 465 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 466 | 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 Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 476 | self.assertIsInstance(info.monotonic, bool) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 477 | 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 Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 481 | self.assertIsInstance(info.adjustable, bool) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 482 | |
| 483 | self.assertRaises(ValueError, time.get_clock_info, 'xxx') |
| 484 | |
| 485 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 486 | class TestLocale(unittest.TestCase): |
| 487 | def setUp(self): |
| 488 | self.oldloc = locale.setlocale(locale.LC_ALL) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 489 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 490 | def tearDown(self): |
| 491 | locale.setlocale(locale.LC_ALL, self.oldloc) |
| 492 | |
Martin v. Löwis | a6a9c4d | 2009-05-30 06:15:30 +0000 | [diff] [blame] | 493 | def test_bug_3061(self): |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 494 | try: |
| 495 | tmp = locale.setlocale(locale.LC_ALL, "fr_FR") |
| 496 | except locale.Error: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 497 | self.skipTest('could not set locale.LC_ALL to fr_FR') |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 498 | # This should not cause an exception |
| 499 | time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) |
| 500 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 501 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 502 | class _TestAsctimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 503 | _format = '%d' |
| 504 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 505 | def yearstr(self, y): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 506 | return time.asctime((y,) + (0,) * 8).split()[-1] |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 507 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 508 | def test_large_year(self): |
Victor Stinner | 7369132 | 2011-01-08 02:00:24 +0000 | [diff] [blame] | 509 | # Check that it doesn't crash for year > 9999 |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 510 | self.assertEqual(self.yearstr(12345), '12345') |
| 511 | self.assertEqual(self.yearstr(123456789), '123456789') |
| 512 | |
| 513 | class _TestStrftimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 514 | |
| 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 Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 525 | def yearstr(self, y): |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 526 | 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 Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 537 | 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 Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 559 | class _Test4dYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 560 | _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 Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 571 | |
| 572 | def test_large_year(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 573 | self.assertEqual(self.yearstr(12345), '12345') |
Victor Stinner | 13ed2ea | 2011-03-21 02:11:01 +0100 | [diff] [blame] | 574 | self.assertEqual(self.yearstr(123456789), '123456789') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 575 | self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR)) |
| 576 | self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 577 | |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 578 | def test_negative(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 579 | self.assertEqual(self.yearstr(-1), self._format % -1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 580 | self.assertEqual(self.yearstr(-1234), '-1234') |
| 581 | self.assertEqual(self.yearstr(-123456), '-123456') |
Florent Xicluna | d1bd7f7 | 2011-11-01 23:42:05 +0100 | [diff] [blame] | 582 | self.assertEqual(self.yearstr(-123456789), str(-123456789)) |
| 583 | self.assertEqual(self.yearstr(-1234567890), str(-1234567890)) |
Florent Xicluna | 2fbc185 | 2011-11-02 08:13:43 +0100 | [diff] [blame] | 584 | 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 Xicluna | e2a732e | 2011-11-02 01:28:17 +0100 | [diff] [blame] | 588 | # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR)) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 589 | self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 590 | |
Alexander Belopolsky | b7d40d1 | 2011-01-11 01:21:25 +0000 | [diff] [blame] | 591 | |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 592 | class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase): |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 593 | pass |
| 594 | |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 595 | class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase): |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 596 | pass |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 597 | |
Alexander Belopolsky | 0dd06f4 | 2011-01-08 01:23:02 +0000 | [diff] [blame] | 598 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 599 | class TestPytime(unittest.TestCase): |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 600 | def setUp(self): |
| 601 | self.invalid_values = ( |
| 602 | -(2 ** 100), 2 ** 100, |
| 603 | -(2.0 ** 100.0), 2.0 ** 100.0, |
| 604 | ) |
| 605 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 606 | @support.cpython_only |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 607 | def test_time_t(self): |
| 608 | from _testcapi import pytime_object_to_time_t |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 609 | |
| 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 Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 630 | for obj, time_t, rnd in ( |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 631 | (-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 Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 648 | ): |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 649 | self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 650 | |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 651 | # Test OverflowError |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 652 | rnd = _PyTime.ROUND_FLOOR |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 653 | for invalid in self.invalid_values: |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 654 | self.assertRaises(OverflowError, |
| 655 | pytime_object_to_time_t, invalid, rnd) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 656 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 657 | @support.cpython_only |
Victor Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 658 | def test_object_to_timespec(self): |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 659 | from _testcapi import pytime_object_to_timespec |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 660 | |
| 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 Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 669 | (-1/2**7, (-1, 992187500)), |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 670 | (-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 Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 682 | for obj, timespec, rnd in ( |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 683 | # Round towards minus infinity (-inf) |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 684 | (-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 Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 696 | (-1.1234567890, (-2, 876543210), FLOOR), |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 697 | (-1.1234567891, (-2, 876543210), FLOOR), |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 698 | # Round towards infinity (+inf) |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 699 | (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 Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 713 | ): |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 714 | with self.subTest(obj=obj, round=rnd, timespec=timespec): |
| 715 | self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 716 | |
Victor Stinner | 110f9e3 | 2015-09-04 10:31:16 +0200 | [diff] [blame] | 717 | # Test OverflowError |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 718 | rnd = _PyTime.ROUND_FLOOR |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 719 | for invalid in self.invalid_values: |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 720 | self.assertRaises(OverflowError, |
| 721 | pytime_object_to_timespec, invalid, rnd) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 722 | |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 723 | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
| 724 | def test_localtime_timezone(self): |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 725 | |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 726 | # 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 Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 775 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 776 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 777 | @unittest.skipUnless(_testcapi is not None, |
| 778 | 'need the _testcapi module') |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 779 | class TestPyTime_t(unittest.TestCase): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 780 | """ |
| 781 | Test the _PyTime_t API. |
| 782 | """ |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 783 | 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 Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 790 | def test_FromSecondsObject(self): |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 791 | from _testcapi import PyTime_FromSecondsObject |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 792 | |
| 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 Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 812 | # nanosecond are kept for value <= 2^23 seconds, |
| 813 | # except 2**23-1e-9 with HALF_UP |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 814 | (2**22 - 1e-9, 4194303999999999), |
| 815 | (2**22, 4194304000000000), |
| 816 | (2**22 + 1e-9, 4194304000000001), |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 817 | (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 Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 830 | # close to 2^63 nanoseconds (_PyTime_t limit) |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 831 | (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 Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 837 | self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts) |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 838 | |
| 839 | with self.subTest(round=rnd): |
| 840 | with self.assertRaises(OverflowError): |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 841 | PyTime_FromSecondsObject(9223372037, rnd) |
| 842 | PyTime_FromSecondsObject(9223372037.0, rnd) |
| 843 | PyTime_FromSecondsObject(-9223372037, rnd) |
| 844 | PyTime_FromSecondsObject(-9223372037.0, rnd) |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 845 | |
| 846 | # Conversion giving different results depending on the rounding method |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 847 | FLOOR = _PyTime.ROUND_FLOOR |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 848 | CEILING = _PyTime.ROUND_CEILING |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 849 | HALF_UP = _PyTime.ROUND_HALF_UP |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 850 | for obj, ts, rnd in ( |
| 851 | # close to zero |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 852 | ( 1e-10, 0, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 853 | ( 1e-10, 1, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 854 | ( 1e-10, 0, HALF_UP), |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 855 | (-1e-10, -1, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 856 | (-1e-10, 0, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 857 | (-1e-10, 0, HALF_UP), |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 858 | |
| 859 | # test rounding of the last nanosecond |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 860 | ( 1.1234567899, 1123456789, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 861 | ( 1.1234567899, 1123456790, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 862 | ( 1.1234567899, 1123456790, HALF_UP), |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 863 | (-1.1234567899, -1123456790, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 864 | (-1.1234567899, -1123456789, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 865 | (-1.1234567899, -1123456790, HALF_UP), |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 866 | |
| 867 | # close to 1 second |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 868 | ( 0.9999999999, 999999999, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 869 | ( 0.9999999999, 1000000000, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 870 | ( 0.9999999999, 1000000000, HALF_UP), |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 871 | (-0.9999999999, -1000000000, FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 872 | (-0.9999999999, -999999999, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 873 | (-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 Stinner | 8aad8d6 | 2015-09-02 13:54:28 +0200 | [diff] [blame] | 878 | # 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 Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 881 | ): |
| 882 | with self.subTest(obj=obj, round=rnd, timestamp=ts): |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 883 | 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 Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 929 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 930 | 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 Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 942 | ): |
| 943 | with self.subTest(nanoseconds=ns, timeval=tv, round=rnd): |
| 944 | self.assertEqual(PyTime_AsTimeval(ns, rnd), tv) |
| 945 | |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 946 | FLOOR = _PyTime.ROUND_FLOOR |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 947 | CEILING = _PyTime.ROUND_CEILING |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 948 | HALF_UP = _PyTime.ROUND_HALF_UP |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 949 | for ns, tv, rnd in ( |
| 950 | # nanoseconds |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 951 | (1, (0, 0), FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 952 | (1, (0, 1), CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 953 | (1, (0, 0), HALF_UP), |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 954 | (-1, (-1, 999999), FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 955 | (-1, (0, 0), CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 956 | (-1, (0, 0), HALF_UP), |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 957 | |
| 958 | # seconds + nanoseconds |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 959 | (1234567001, (1, 234567), FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 960 | (1234567001, (1, 234568), CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 961 | (1234567001, (1, 234567), HALF_UP), |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 962 | (-1234567001, (-2, 765432), FLOOR), |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 963 | (-1234567001, (-2, 765433), CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 964 | (-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 Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 975 | ): |
| 976 | with self.subTest(nanoseconds=ns, timeval=tv, round=rnd): |
| 977 | self.assertEqual(PyTime_AsTimeval(ns, rnd), tv) |
| 978 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 979 | @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 Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1000 | 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 Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1017 | HALF_UP = _PyTime.ROUND_HALF_UP |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1018 | for ns, ms, rnd in ( |
| 1019 | # nanoseconds |
| 1020 | (1, 0, FLOOR), |
| 1021 | (1, 1, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1022 | (1, 0, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1023 | (-1, 0, FLOOR), |
| 1024 | (-1, -1, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1025 | (-1, 0, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1026 | |
| 1027 | # seconds + nanoseconds |
| 1028 | (1234 * MS_TO_NS + 1, 1234, FLOOR), |
| 1029 | (1234 * MS_TO_NS + 1, 1235, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1030 | (1234 * MS_TO_NS + 1, 1234, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1031 | (-1234 * MS_TO_NS - 1, -1234, FLOOR), |
| 1032 | (-1234 * MS_TO_NS - 1, -1235, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1033 | (-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 Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1044 | ): |
| 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 Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1069 | HALF_UP = _PyTime.ROUND_HALF_UP |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1070 | for ns, ms, rnd in ( |
| 1071 | # nanoseconds |
| 1072 | (1, 0, FLOOR), |
| 1073 | (1, 1, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1074 | (1, 0, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1075 | (-1, 0, FLOOR), |
| 1076 | (-1, -1, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1077 | (-1, 0, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1078 | |
| 1079 | # seconds + nanoseconds |
| 1080 | (1234 * US_TO_NS + 1, 1234, FLOOR), |
| 1081 | (1234 * US_TO_NS + 1, 1235, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1082 | (1234 * US_TO_NS + 1, 1234, HALF_UP), |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1083 | (-1234 * US_TO_NS - 1, -1234, FLOOR), |
| 1084 | (-1234 * US_TO_NS - 1, -1235, CEILING), |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 1085 | (-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 Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 1094 | ): |
| 1095 | with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd): |
| 1096 | self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms) |
| 1097 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 1098 | |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1099 | @unittest.skipUnless(_testcapi is not None, |
| 1100 | 'need the _testcapi module') |
| 1101 | class 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 Stinner | ead144c | 2015-09-02 11:05:32 +0200 | [diff] [blame] | 1137 | |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1138 | (1.9, 1, FLOOR), |
| 1139 | (1.9, 2, HALF_UP), |
| 1140 | (1.9, 2, CEILING), |
Victor Stinner | ead144c | 2015-09-02 11:05:32 +0200 | [diff] [blame] | 1141 | |
| 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 Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1149 | ): |
| 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 Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 1158 | def test_object_to_timeval(self): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1159 | 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 Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 1170 | (1/2**6, (0, 15625)), |
| 1171 | (-1/2**6, (-1, 984375)), |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1172 | (-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 Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1192 | (0.9999999, (0, 999999), FLOOR), |
| 1193 | (0.9999999, (1, 0), CEILING), |
| 1194 | (0.9999999, (1, 0), HALF_UP), |
Victor Stinner | ead144c | 2015-09-02 11:05:32 +0200 | [diff] [blame] | 1195 | |
| 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 Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1205 | ): |
| 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 Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame^] | 1229 | (-1/2**9, (-1, 998046875)), |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1230 | ): |
| 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 Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1248 | (0.9999999999, (0, 999999999), FLOOR), |
| 1249 | (0.9999999999, (1, 0), CEILING), |
| 1250 | (0.9999999999, (1, 0), HALF_UP), |
Victor Stinner | ead144c | 2015-09-02 11:05:32 +0200 | [diff] [blame] | 1251 | |
| 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 Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 1260 | ): |
| 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 Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1271 | if __name__ == "__main__": |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 1272 | unittest.main() |