Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 2 | import decimal |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3 | import enum |
| 4 | import locale |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 5 | import math |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 6 | import platform |
| 7 | import sys |
| 8 | import sysconfig |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 9 | import time |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 10 | import unittest |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 11 | try: |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 12 | import _testcapi |
| 13 | except ImportError: |
| 14 | _testcapi = None |
| 15 | |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 16 | |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 17 | # Max year is only limited by the size of C int. |
| 18 | SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 |
| 19 | TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1 |
| 20 | TIME_MINYEAR = -TIME_MAXYEAR - 1 |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 21 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 22 | SEC_TO_US = 10 ** 6 |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 23 | US_TO_NS = 10 ** 3 |
| 24 | MS_TO_NS = 10 ** 6 |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 25 | SEC_TO_NS = 10 ** 9 |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 26 | NS_TO_SEC = 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 | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 33 | # Round to nearest with ties going to nearest even integer |
| 34 | ROUND_HALF_EVEN = 2 |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 35 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 36 | # Rounding modes supported by PyTime |
| 37 | ROUNDING_MODES = ( |
| 38 | # (PyTime rounding method, decimal rounding method) |
| 39 | (_PyTime.ROUND_FLOOR, decimal.ROUND_FLOOR), |
| 40 | (_PyTime.ROUND_CEILING, decimal.ROUND_CEILING), |
| 41 | (_PyTime.ROUND_HALF_EVEN, decimal.ROUND_HALF_EVEN), |
| 42 | ) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 43 | |
| 44 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 45 | class TimeTestCase(unittest.TestCase): |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 46 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 47 | def setUp(self): |
| 48 | self.t = time.time() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 49 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 50 | def test_data_attributes(self): |
| 51 | time.altzone |
| 52 | time.daylight |
| 53 | time.timezone |
| 54 | time.tzname |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 55 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 56 | def test_time(self): |
| 57 | time.time() |
| 58 | info = time.get_clock_info('time') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 59 | self.assertFalse(info.monotonic) |
Victor Stinner | 6222d76 | 2012-06-12 23:04:11 +0200 | [diff] [blame] | 60 | self.assertTrue(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 61 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 62 | def test_clock(self): |
| 63 | time.clock() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 64 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 65 | info = time.get_clock_info('clock') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 66 | self.assertTrue(info.monotonic) |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 67 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 68 | |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 69 | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
| 70 | 'need time.clock_gettime()') |
| 71 | def test_clock_realtime(self): |
| 72 | time.clock_gettime(time.CLOCK_REALTIME) |
| 73 | |
| 74 | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
| 75 | 'need time.clock_gettime()') |
| 76 | @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'), |
| 77 | 'need time.CLOCK_MONOTONIC') |
| 78 | def test_clock_monotonic(self): |
| 79 | a = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 80 | b = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 81 | self.assertLessEqual(a, b) |
| 82 | |
| 83 | @unittest.skipUnless(hasattr(time, 'clock_getres'), |
| 84 | 'need time.clock_getres()') |
| 85 | def test_clock_getres(self): |
| 86 | res = time.clock_getres(time.CLOCK_REALTIME) |
| 87 | self.assertGreater(res, 0.0) |
| 88 | self.assertLessEqual(res, 1.0) |
| 89 | |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 90 | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
| 91 | 'need time.clock_settime()') |
| 92 | def test_clock_settime(self): |
| 93 | t = time.clock_gettime(time.CLOCK_REALTIME) |
| 94 | try: |
| 95 | time.clock_settime(time.CLOCK_REALTIME, t) |
| 96 | except PermissionError: |
| 97 | pass |
| 98 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 99 | if hasattr(time, 'CLOCK_MONOTONIC'): |
| 100 | self.assertRaises(OSError, |
| 101 | time.clock_settime, time.CLOCK_MONOTONIC, 0) |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 102 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 103 | def test_conversions(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 104 | self.assertEqual(time.ctime(self.t), |
| 105 | time.asctime(time.localtime(self.t))) |
| 106 | self.assertEqual(int(time.mktime(time.localtime(self.t))), |
| 107 | int(self.t)) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 108 | |
| 109 | def test_sleep(self): |
Victor Stinner | 7f53a50 | 2011-07-05 22:00:25 +0200 | [diff] [blame] | 110 | self.assertRaises(ValueError, time.sleep, -2) |
| 111 | self.assertRaises(ValueError, time.sleep, -1) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 112 | time.sleep(1.2) |
| 113 | |
| 114 | def test_strftime(self): |
| 115 | tt = time.gmtime(self.t) |
| 116 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 117 | 'j', 'm', 'M', 'p', 'S', |
| 118 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 119 | format = ' %' + directive |
| 120 | try: |
| 121 | time.strftime(format, tt) |
| 122 | except ValueError: |
| 123 | self.fail('conversion specifier: %r failed.' % format) |
| 124 | |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 125 | self.assertRaises(TypeError, time.strftime, b'%S', tt) |
| 126 | # embedded null character |
| 127 | self.assertRaises(ValueError, time.strftime, '%S\0', tt) |
| 128 | |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 129 | def _bounds_checking(self, func): |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 130 | # Make sure that strftime() checks the bounds of the various parts |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 131 | # of the time tuple (0 is valid for *all* values). |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 132 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 133 | # The year field is tested by other test cases above |
| 134 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 135 | # Check month [1, 12] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 136 | func((1900, 0, 1, 0, 0, 0, 0, 1, -1)) |
| 137 | func((1900, 12, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 138 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 139 | (1900, -1, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 140 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 141 | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 142 | # Check day of month [1, 31] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 143 | func((1900, 1, 0, 0, 0, 0, 0, 1, -1)) |
| 144 | func((1900, 1, 31, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 145 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 146 | (1900, 1, -1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 147 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 148 | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 149 | # Check hour [0, 23] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 150 | func((1900, 1, 1, 23, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 151 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 152 | (1900, 1, 1, -1, 0, 0, 0, 1, -1)) |
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, 24, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 155 | # Check minute [0, 59] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 156 | func((1900, 1, 1, 0, 59, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 157 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 158 | (1900, 1, 1, 0, -1, 0, 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, 60, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 161 | # Check second [0, 61] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 162 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 163 | (1900, 1, 1, 0, 0, -1, 0, 1, -1)) |
| 164 | # C99 only requires allowing for one leap second, but Python's docs say |
| 165 | # allow two leap seconds (0..61) |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 166 | func((1900, 1, 1, 0, 0, 60, 0, 1, -1)) |
| 167 | func((1900, 1, 1, 0, 0, 61, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 168 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 169 | (1900, 1, 1, 0, 0, 62, 0, 1, -1)) |
| 170 | # No check for upper-bound day of week; |
| 171 | # value forced into range by a ``% 7`` calculation. |
| 172 | # Start check at -2 since gettmarg() increments value before taking |
| 173 | # modulo. |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 174 | self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)), |
| 175 | func((1900, 1, 1, 0, 0, 0, +6, 1, -1))) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 176 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 177 | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 178 | # Check day of the year [1, 366] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 179 | func((1900, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 180 | func((1900, 1, 1, 0, 0, 0, 0, 366, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 181 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 182 | (1900, 1, 1, 0, 0, 0, 0, -1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 183 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 184 | (1900, 1, 1, 0, 0, 0, 0, 367, -1)) |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 185 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 186 | def test_strftime_bounding_check(self): |
| 187 | self._bounds_checking(lambda tup: time.strftime('', tup)) |
| 188 | |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 189 | def test_strftime_format_check(self): |
| 190 | # Test that strftime does not crash on invalid format strings |
| 191 | # that may trigger a buffer overread. When not triggered, |
| 192 | # strftime may succeed or raise ValueError depending on |
| 193 | # the platform. |
| 194 | for x in [ '', 'A', '%A', '%AA' ]: |
| 195 | for y in range(0x0, 0x10): |
| 196 | for z in [ '%', 'A%', 'AA%', '%A%', 'A%A%', '%#' ]: |
| 197 | try: |
| 198 | time.strftime(x * y + z) |
| 199 | except ValueError: |
| 200 | pass |
| 201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | def test_default_values_for_zero(self): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 203 | # Make sure that using all zeros uses the proper default |
| 204 | # values. No test for daylight savings since strftime() does |
| 205 | # not change output based on its value and no test for year |
| 206 | # because systems vary in their support for year 0. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | expected = "2000 01 01 00 00 00 1 001" |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 208 | with support.check_warnings(): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 209 | 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] | 210 | self.assertEqual(expected, result) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 212 | def test_strptime(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 213 | # Should be able to go round-trip from strftime to strptime without |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 214 | # raising an exception. |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 215 | tt = time.gmtime(self.t) |
| 216 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 217 | 'j', 'm', 'M', 'p', 'S', |
| 218 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 219 | format = '%' + directive |
| 220 | strf_output = time.strftime(format, tt) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 221 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 222 | time.strptime(strf_output, format) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 223 | except ValueError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 224 | self.fail("conversion specifier %r failed with '%s' input." % |
| 225 | (format, strf_output)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 226 | |
Brett Cannon | 7f6b4f8 | 2009-03-30 21:30:26 +0000 | [diff] [blame] | 227 | def test_strptime_bytes(self): |
| 228 | # Make sure only strings are accepted as arguments to strptime. |
| 229 | self.assertRaises(TypeError, time.strptime, b'2009', "%Y") |
| 230 | self.assertRaises(TypeError, time.strptime, '2009', b'%Y') |
| 231 | |
Ezio Melotti | 0f38908 | 2013-04-04 02:09:20 +0300 | [diff] [blame] | 232 | def test_strptime_exception_context(self): |
| 233 | # check that this doesn't chain exceptions needlessly (see #17572) |
| 234 | with self.assertRaises(ValueError) as e: |
| 235 | time.strptime('', '%D') |
| 236 | self.assertIs(e.exception.__suppress_context__, True) |
Serhiy Storchaka | cdac302 | 2013-11-24 18:15:37 +0200 | [diff] [blame] | 237 | # additional check for IndexError branch (issue #19545) |
| 238 | with self.assertRaises(ValueError) as e: |
| 239 | time.strptime('19', '%Y %') |
| 240 | self.assertIs(e.exception.__suppress_context__, True) |
Ezio Melotti | 0f38908 | 2013-04-04 02:09:20 +0300 | [diff] [blame] | 241 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 242 | def test_asctime(self): |
| 243 | time.asctime(time.gmtime(self.t)) |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 244 | |
| 245 | # Max year is only limited by the size of C int. |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 246 | for bigyear in TIME_MAXYEAR, TIME_MINYEAR: |
| 247 | asc = time.asctime((bigyear, 6, 1) + (0,) * 6) |
| 248 | self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) |
| 249 | self.assertRaises(OverflowError, time.asctime, |
| 250 | (TIME_MAXYEAR + 1,) + (0,) * 8) |
| 251 | self.assertRaises(OverflowError, time.asctime, |
| 252 | (TIME_MINYEAR - 1,) + (0,) * 8) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 253 | self.assertRaises(TypeError, time.asctime, 0) |
Alexander Belopolsky | e2dc082 | 2011-01-02 20:48:22 +0000 | [diff] [blame] | 254 | self.assertRaises(TypeError, time.asctime, ()) |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 255 | self.assertRaises(TypeError, time.asctime, (0,) * 10) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 256 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 257 | def test_asctime_bounding_check(self): |
| 258 | self._bounds_checking(time.asctime) |
| 259 | |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 260 | def test_ctime(self): |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 261 | t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1)) |
| 262 | self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973') |
| 263 | t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 264 | self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000') |
Victor Stinner | 1ac4261 | 2014-02-21 09:27:17 +0100 | [diff] [blame] | 265 | for year in [-100, 100, 1000, 2000, 2050, 10000]: |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 266 | try: |
| 267 | testval = time.mktime((year, 1, 10) + (0,)*6) |
| 268 | except (ValueError, OverflowError): |
| 269 | # If mktime fails, ctime will fail too. This may happen |
| 270 | # on some platforms. |
| 271 | pass |
| 272 | else: |
| 273 | self.assertEqual(time.ctime(testval)[20:], str(year)) |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 274 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 275 | @unittest.skipUnless(hasattr(time, "tzset"), |
| 276 | "time module has no attribute tzset") |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 277 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 279 | from os import environ |
| 280 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 281 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 282 | # hemisphere. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 283 | xmas2002 = 1040774400.0 |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 284 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 285 | # These formats are correct for 2002, and possibly future years |
| 286 | # This format is the 'standard' as documented at: |
| 287 | # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html |
| 288 | # They are also documented in the tzset(3) man page on most Unix |
| 289 | # systems. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 290 | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 291 | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
| 292 | utc='UTC+0' |
| 293 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 294 | org_TZ = environ.get('TZ',None) |
| 295 | try: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 296 | # Make sure we can switch to UTC time and results are correct |
| 297 | # Note that unknown timezones default to UTC. |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 298 | # Note that altzone is undefined in UTC, as there is no DST |
| 299 | environ['TZ'] = eastern |
| 300 | time.tzset() |
| 301 | environ['TZ'] = utc |
| 302 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 303 | self.assertEqual( |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 304 | time.gmtime(xmas2002), time.localtime(xmas2002) |
| 305 | ) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 306 | self.assertEqual(time.daylight, 0) |
| 307 | self.assertEqual(time.timezone, 0) |
| 308 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 309 | |
| 310 | # Make sure we can switch to US/Eastern |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 311 | environ['TZ'] = eastern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 312 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 313 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 314 | self.assertEqual(time.tzname, ('EST', 'EDT')) |
| 315 | self.assertEqual(len(time.tzname), 2) |
| 316 | self.assertEqual(time.daylight, 1) |
| 317 | self.assertEqual(time.timezone, 18000) |
| 318 | self.assertEqual(time.altzone, 14400) |
| 319 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
| 320 | self.assertEqual(len(time.tzname), 2) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 321 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 322 | # Now go to the southern hemisphere. |
| 323 | environ['TZ'] = victoria |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 324 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 325 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
Victor Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 326 | |
| 327 | # Issue #11886: Australian Eastern Standard Time (UTC+10) is called |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 328 | # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" |
| 329 | # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone), |
| 330 | # on some operating systems (e.g. FreeBSD), which is wrong. See for |
| 331 | # example this bug: |
| 332 | # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810 |
Victor Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 333 | self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 334 | self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 335 | self.assertEqual(len(time.tzname), 2) |
| 336 | self.assertEqual(time.daylight, 1) |
| 337 | self.assertEqual(time.timezone, -36000) |
| 338 | self.assertEqual(time.altzone, -39600) |
| 339 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 1) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 341 | finally: |
| 342 | # Repair TZ environment variable in case any other tests |
| 343 | # rely on it. |
| 344 | if org_TZ is not None: |
| 345 | environ['TZ'] = org_TZ |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 346 | elif 'TZ' in environ: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 347 | del environ['TZ'] |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 348 | time.tzset() |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 349 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 350 | def test_insane_timestamps(self): |
| 351 | # It's possible that some platform maps time_t to double, |
| 352 | # and that this test will fail there. This test should |
| 353 | # exempt such platforms (provided they return reasonable |
| 354 | # results!). |
| 355 | for func in time.ctime, time.gmtime, time.localtime: |
| 356 | for unreasonable in -1e200, 1e200: |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 357 | self.assertRaises(OverflowError, func, unreasonable) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 358 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 359 | def test_ctime_without_arg(self): |
| 360 | # Not sure how to check the values, since the clock could tick |
| 361 | # at any time. Make sure these are at least accepted and |
| 362 | # don't raise errors. |
| 363 | time.ctime() |
| 364 | time.ctime(None) |
| 365 | |
| 366 | def test_gmtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 367 | gt0 = time.gmtime() |
| 368 | gt1 = time.gmtime(None) |
| 369 | t0 = time.mktime(gt0) |
| 370 | t1 = time.mktime(gt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 371 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 372 | |
| 373 | def test_localtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 374 | lt0 = time.localtime() |
| 375 | lt1 = time.localtime(None) |
| 376 | t0 = time.mktime(lt0) |
| 377 | t1 = time.mktime(lt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 378 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 379 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 380 | def test_mktime(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 381 | # Issue #1726687 |
| 382 | for t in (-2, -1, 0, 1): |
Victor Stinner | 8c8b4e0 | 2014-02-21 23:54:32 +0100 | [diff] [blame] | 383 | if sys.platform.startswith('aix') and t == -1: |
| 384 | # Issue #11188, #19748: mktime() returns -1 on error. On Linux, |
| 385 | # the tm_wday field is used as a sentinel () to detect if -1 is |
| 386 | # really an error or a valid timestamp. On AIX, tm_wday is |
| 387 | # unchanged even on success and so cannot be used as a |
| 388 | # sentinel. |
| 389 | continue |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 390 | try: |
| 391 | tt = time.localtime(t) |
Victor Stinner | 2cbae98 | 2012-01-27 00:50:33 +0100 | [diff] [blame] | 392 | except (OverflowError, OSError): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 393 | pass |
| 394 | else: |
| 395 | self.assertEqual(time.mktime(tt), t) |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 396 | |
| 397 | # Issue #13309: passing extreme values to mktime() or localtime() |
| 398 | # borks the glibc's internal timezone data. |
| 399 | @unittest.skipUnless(platform.libc_ver()[0] != 'glibc', |
| 400 | "disabled because of a bug in glibc. Issue #13309") |
| 401 | def test_mktime_error(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 402 | # It may not be possible to reliably make mktime return error |
| 403 | # on all platfom. This will make sure that no other exception |
| 404 | # than OverflowError is raised for an extreme value. |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 405 | tt = time.gmtime(self.t) |
| 406 | tzname = time.strftime('%Z', tt) |
| 407 | self.assertNotEqual(tzname, 'LMT') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 408 | try: |
| 409 | time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) |
| 410 | except OverflowError: |
| 411 | pass |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 412 | self.assertEqual(time.strftime('%Z', tt), tzname) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 413 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 414 | @unittest.skipUnless(hasattr(time, 'monotonic'), |
| 415 | 'need time.monotonic') |
| 416 | def test_monotonic(self): |
Victor Stinner | 6c86181 | 2013-11-23 00:15:27 +0100 | [diff] [blame] | 417 | # monotonic() should not go backward |
| 418 | times = [time.monotonic() for n in range(100)] |
| 419 | t1 = times[0] |
| 420 | for t2 in times[1:]: |
| 421 | self.assertGreaterEqual(t2, t1, "times=%s" % times) |
| 422 | t1 = t2 |
| 423 | |
| 424 | # monotonic() includes time elapsed during a sleep |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 425 | t1 = time.monotonic() |
Victor Stinner | a9c99a6 | 2013-07-03 23:07:37 +0200 | [diff] [blame] | 426 | time.sleep(0.5) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 427 | t2 = time.monotonic() |
Victor Stinner | 2dd254d | 2012-01-20 02:24:18 +0100 | [diff] [blame] | 428 | dt = t2 - t1 |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 429 | self.assertGreater(t2, t1) |
Zachary Ware | 487aedb | 2014-01-02 09:41:10 -0600 | [diff] [blame] | 430 | # Issue #20101: On some Windows machines, dt may be slightly low |
| 431 | self.assertTrue(0.45 <= dt <= 1.0, dt) |
Antoine Pitrou | 391166f | 2012-01-18 22:35:21 +0100 | [diff] [blame] | 432 | |
Victor Stinner | 6c86181 | 2013-11-23 00:15:27 +0100 | [diff] [blame] | 433 | # monotonic() is a monotonic but non adjustable clock |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 434 | info = time.get_clock_info('monotonic') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 435 | self.assertTrue(info.monotonic) |
Victor Stinner | 6222d76 | 2012-06-12 23:04:11 +0200 | [diff] [blame] | 436 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 437 | |
| 438 | def test_perf_counter(self): |
| 439 | time.perf_counter() |
| 440 | |
| 441 | def test_process_time(self): |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 442 | # process_time() should not include time spend during a sleep |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 443 | start = time.process_time() |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 444 | time.sleep(0.100) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 445 | stop = time.process_time() |
Victor Stinner | 0dec1bf | 2012-06-01 22:45:23 +0200 | [diff] [blame] | 446 | # use 20 ms because process_time() has usually a resolution of 15 ms |
| 447 | # on Windows |
| 448 | self.assertLess(stop - start, 0.020) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 449 | |
| 450 | info = time.get_clock_info('process_time') |
Benjamin Peterson | 1c5ae55 | 2012-05-01 11:14:32 -0400 | [diff] [blame] | 451 | self.assertTrue(info.monotonic) |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 452 | self.assertFalse(info.adjustable) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 453 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 454 | @unittest.skipUnless(hasattr(time, 'monotonic'), |
| 455 | 'need time.monotonic') |
| 456 | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
| 457 | 'need time.clock_settime') |
| 458 | def test_monotonic_settime(self): |
| 459 | t1 = time.monotonic() |
| 460 | realtime = time.clock_gettime(time.CLOCK_REALTIME) |
| 461 | # jump backward with an offset of 1 hour |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 462 | try: |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 463 | time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) |
| 464 | except PermissionError as err: |
| 465 | self.skipTest(err) |
| 466 | t2 = time.monotonic() |
| 467 | time.clock_settime(time.CLOCK_REALTIME, realtime) |
| 468 | # monotonic must not be affected by system clock updates |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 469 | self.assertGreaterEqual(t2, t1) |
| 470 | |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 471 | def test_localtime_failure(self): |
| 472 | # Issue #13847: check for localtime() failure |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 473 | invalid_time_t = None |
| 474 | for time_t in (-1, 2**30, 2**33, 2**60): |
| 475 | try: |
| 476 | time.localtime(time_t) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 477 | except OverflowError: |
| 478 | self.skipTest("need 64-bit time_t") |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 479 | except OSError: |
| 480 | invalid_time_t = time_t |
| 481 | break |
| 482 | if invalid_time_t is None: |
| 483 | self.skipTest("unable to find an invalid time_t value") |
| 484 | |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 485 | self.assertRaises(OSError, time.localtime, invalid_time_t) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 486 | self.assertRaises(OSError, time.ctime, invalid_time_t) |
Victor Stinner | b94b266 | 2012-01-18 01:50:21 +0100 | [diff] [blame] | 487 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 488 | # Issue #26669: check for localtime() failure |
| 489 | self.assertRaises(ValueError, time.localtime, float("nan")) |
| 490 | self.assertRaises(ValueError, time.ctime, float("nan")) |
| 491 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 492 | def test_get_clock_info(self): |
| 493 | clocks = ['clock', 'perf_counter', 'process_time', 'time'] |
| 494 | if hasattr(time, 'monotonic'): |
| 495 | clocks.append('monotonic') |
| 496 | |
| 497 | for name in clocks: |
| 498 | info = time.get_clock_info(name) |
| 499 | #self.assertIsInstance(info, dict) |
| 500 | self.assertIsInstance(info.implementation, str) |
| 501 | self.assertNotEqual(info.implementation, '') |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 502 | self.assertIsInstance(info.monotonic, bool) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 503 | self.assertIsInstance(info.resolution, float) |
| 504 | # 0.0 < resolution <= 1.0 |
| 505 | self.assertGreater(info.resolution, 0.0) |
| 506 | self.assertLessEqual(info.resolution, 1.0) |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 507 | self.assertIsInstance(info.adjustable, bool) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 508 | |
| 509 | self.assertRaises(ValueError, time.get_clock_info, 'xxx') |
| 510 | |
| 511 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 512 | class TestLocale(unittest.TestCase): |
| 513 | def setUp(self): |
| 514 | self.oldloc = locale.setlocale(locale.LC_ALL) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 515 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 516 | def tearDown(self): |
| 517 | locale.setlocale(locale.LC_ALL, self.oldloc) |
| 518 | |
Martin v. Löwis | a6a9c4d | 2009-05-30 06:15:30 +0000 | [diff] [blame] | 519 | def test_bug_3061(self): |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 520 | try: |
| 521 | tmp = locale.setlocale(locale.LC_ALL, "fr_FR") |
| 522 | except locale.Error: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 523 | 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] | 524 | # This should not cause an exception |
| 525 | time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) |
| 526 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 527 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 528 | class _TestAsctimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 529 | _format = '%d' |
| 530 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 531 | def yearstr(self, y): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 532 | return time.asctime((y,) + (0,) * 8).split()[-1] |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 533 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 534 | def test_large_year(self): |
Victor Stinner | 7369132 | 2011-01-08 02:00:24 +0000 | [diff] [blame] | 535 | # Check that it doesn't crash for year > 9999 |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 536 | self.assertEqual(self.yearstr(12345), '12345') |
| 537 | self.assertEqual(self.yearstr(123456789), '123456789') |
| 538 | |
| 539 | class _TestStrftimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 540 | |
| 541 | # Issue 13305: For years < 1000, the value is not always |
| 542 | # padded to 4 digits across platforms. The C standard |
| 543 | # assumes year >= 1900, so it does not specify the number |
| 544 | # of digits. |
| 545 | |
| 546 | if time.strftime('%Y', (1,) + (0,) * 8) == '0001': |
| 547 | _format = '%04d' |
| 548 | else: |
| 549 | _format = '%d' |
| 550 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 551 | def yearstr(self, y): |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 552 | return time.strftime('%Y', (y,) + (0,) * 8) |
| 553 | |
| 554 | def test_4dyear(self): |
| 555 | # Check that we can return the zero padded value. |
| 556 | if self._format == '%04d': |
| 557 | self.test_year('%04d') |
| 558 | else: |
| 559 | def year4d(y): |
| 560 | return time.strftime('%4Y', (y,) + (0,) * 8) |
| 561 | self.test_year('%04d', func=year4d) |
| 562 | |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 563 | def skip_if_not_supported(y): |
| 564 | msg = "strftime() is limited to [1; 9999] with Visual Studio" |
| 565 | # Check that it doesn't crash for year > 9999 |
| 566 | try: |
| 567 | time.strftime('%Y', (y,) + (0,) * 8) |
| 568 | except ValueError: |
| 569 | cond = False |
| 570 | else: |
| 571 | cond = True |
| 572 | return unittest.skipUnless(cond, msg) |
| 573 | |
| 574 | @skip_if_not_supported(10000) |
| 575 | def test_large_year(self): |
| 576 | return super().test_large_year() |
| 577 | |
| 578 | @skip_if_not_supported(0) |
| 579 | def test_negative(self): |
| 580 | return super().test_negative() |
| 581 | |
| 582 | del skip_if_not_supported |
| 583 | |
| 584 | |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 585 | class _Test4dYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 586 | _format = '%d' |
| 587 | |
| 588 | def test_year(self, fmt=None, func=None): |
| 589 | fmt = fmt or self._format |
| 590 | func = func or self.yearstr |
| 591 | self.assertEqual(func(1), fmt % 1) |
| 592 | self.assertEqual(func(68), fmt % 68) |
| 593 | self.assertEqual(func(69), fmt % 69) |
| 594 | self.assertEqual(func(99), fmt % 99) |
| 595 | self.assertEqual(func(999), fmt % 999) |
| 596 | self.assertEqual(func(9999), fmt % 9999) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 597 | |
| 598 | def test_large_year(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 599 | self.assertEqual(self.yearstr(12345), '12345') |
Victor Stinner | 13ed2ea | 2011-03-21 02:11:01 +0100 | [diff] [blame] | 600 | self.assertEqual(self.yearstr(123456789), '123456789') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 601 | self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR)) |
| 602 | self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 603 | |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 604 | def test_negative(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 605 | self.assertEqual(self.yearstr(-1), self._format % -1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 606 | self.assertEqual(self.yearstr(-1234), '-1234') |
| 607 | self.assertEqual(self.yearstr(-123456), '-123456') |
Florent Xicluna | d1bd7f7 | 2011-11-01 23:42:05 +0100 | [diff] [blame] | 608 | self.assertEqual(self.yearstr(-123456789), str(-123456789)) |
| 609 | self.assertEqual(self.yearstr(-1234567890), str(-1234567890)) |
Florent Xicluna | 2fbc185 | 2011-11-02 08:13:43 +0100 | [diff] [blame] | 610 | self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900)) |
| 611 | # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900 |
| 612 | # Skip the value test, but check that no error is raised |
| 613 | self.yearstr(TIME_MINYEAR) |
Florent Xicluna | e2a732e | 2011-11-02 01:28:17 +0100 | [diff] [blame] | 614 | # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR)) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 615 | self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 616 | |
Alexander Belopolsky | b7d40d1 | 2011-01-11 01:21:25 +0000 | [diff] [blame] | 617 | |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 618 | class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase): |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 619 | pass |
| 620 | |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 621 | class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase): |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 622 | pass |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 623 | |
Alexander Belopolsky | 0dd06f4 | 2011-01-08 01:23:02 +0000 | [diff] [blame] | 624 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 625 | class TestPytime(unittest.TestCase): |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 626 | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
| 627 | def test_localtime_timezone(self): |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 628 | |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 629 | # Get the localtime and examine it for the offset and zone. |
| 630 | lt = time.localtime() |
| 631 | self.assertTrue(hasattr(lt, "tm_gmtoff")) |
| 632 | self.assertTrue(hasattr(lt, "tm_zone")) |
| 633 | |
| 634 | # See if the offset and zone are similar to the module |
| 635 | # attributes. |
| 636 | if lt.tm_gmtoff is None: |
| 637 | self.assertTrue(not hasattr(time, "timezone")) |
| 638 | else: |
| 639 | self.assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst]) |
| 640 | if lt.tm_zone is None: |
| 641 | self.assertTrue(not hasattr(time, "tzname")) |
| 642 | else: |
| 643 | self.assertEqual(lt.tm_zone, time.tzname[lt.tm_isdst]) |
| 644 | |
| 645 | # Try and make UNIX times from the localtime and a 9-tuple |
| 646 | # created from the localtime. Test to see that the times are |
| 647 | # the same. |
| 648 | t = time.mktime(lt); t9 = time.mktime(lt[:9]) |
| 649 | self.assertEqual(t, t9) |
| 650 | |
| 651 | # Make localtimes from the UNIX times and compare them to |
| 652 | # the original localtime, thus making a round trip. |
| 653 | new_lt = time.localtime(t); new_lt9 = time.localtime(t9) |
| 654 | self.assertEqual(new_lt, lt) |
| 655 | self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff) |
| 656 | self.assertEqual(new_lt.tm_zone, lt.tm_zone) |
| 657 | self.assertEqual(new_lt9, lt) |
| 658 | self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff) |
| 659 | self.assertEqual(new_lt9.tm_zone, lt.tm_zone) |
| 660 | |
| 661 | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
| 662 | def test_strptime_timezone(self): |
| 663 | t = time.strptime("UTC", "%Z") |
| 664 | self.assertEqual(t.tm_zone, 'UTC') |
| 665 | t = time.strptime("+0500", "%z") |
| 666 | self.assertEqual(t.tm_gmtoff, 5 * 3600) |
| 667 | |
| 668 | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
| 669 | def test_short_times(self): |
| 670 | |
| 671 | import pickle |
| 672 | |
| 673 | # Load a short time structure using pickle. |
| 674 | st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\nI1\ntp1\n(dp2\ntp3\nRp4\n." |
| 675 | lt = pickle.loads(st) |
| 676 | self.assertIs(lt.tm_gmtoff, None) |
| 677 | self.assertIs(lt.tm_zone, None) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 678 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 679 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 680 | @unittest.skipIf(_testcapi is None, 'need the _testcapi module') |
| 681 | class CPyTimeTestCase: |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 682 | """ |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 683 | Base class to test the C _PyTime_t API. |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 684 | """ |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 685 | OVERFLOW_SECONDS = None |
| 686 | |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 687 | def setUp(self): |
| 688 | from _testcapi import SIZEOF_TIME_T |
| 689 | bits = SIZEOF_TIME_T * 8 - 1 |
| 690 | self.time_t_min = -2 ** bits |
| 691 | self.time_t_max = 2 ** bits - 1 |
| 692 | |
| 693 | def time_t_filter(self, seconds): |
| 694 | return (self.time_t_min <= seconds <= self.time_t_max) |
| 695 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 696 | def _rounding_values(self, use_float): |
| 697 | "Build timestamps used to test rounding." |
| 698 | |
| 699 | units = [1, US_TO_NS, MS_TO_NS, SEC_TO_NS] |
| 700 | if use_float: |
| 701 | # picoseconds are only tested to pytime_converter accepting floats |
| 702 | units.append(1e-3) |
| 703 | |
| 704 | values = ( |
| 705 | # small values |
| 706 | 1, 2, 5, 7, 123, 456, 1234, |
| 707 | # 10^k - 1 |
| 708 | 9, |
| 709 | 99, |
| 710 | 999, |
| 711 | 9999, |
| 712 | 99999, |
| 713 | 999999, |
| 714 | # test half even rounding near 0.5, 1.5, 2.5, 3.5, 4.5 |
| 715 | 499, 500, 501, |
| 716 | 1499, 1500, 1501, |
| 717 | 2500, |
| 718 | 3500, |
| 719 | 4500, |
| 720 | ) |
| 721 | |
| 722 | ns_timestamps = [0] |
| 723 | for unit in units: |
| 724 | for value in values: |
| 725 | ns = value * unit |
| 726 | ns_timestamps.extend((-ns, ns)) |
| 727 | for pow2 in (0, 5, 10, 15, 22, 23, 24, 30, 33): |
| 728 | ns = (2 ** pow2) * SEC_TO_NS |
| 729 | ns_timestamps.extend(( |
| 730 | -ns-1, -ns, -ns+1, |
| 731 | ns-1, ns, ns+1 |
| 732 | )) |
| 733 | for seconds in (_testcapi.INT_MIN, _testcapi.INT_MAX): |
| 734 | ns_timestamps.append(seconds * SEC_TO_NS) |
| 735 | if use_float: |
Victor Stinner | 717a32b | 2016-08-17 11:07:21 +0200 | [diff] [blame] | 736 | # numbers with an exact representation in IEEE 754 (base 2) |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 737 | for pow2 in (3, 7, 10, 15): |
| 738 | ns = 2.0 ** (-pow2) |
| 739 | ns_timestamps.extend((-ns, ns)) |
| 740 | |
| 741 | # seconds close to _PyTime_t type limit |
| 742 | ns = (2 ** 63 // SEC_TO_NS) * SEC_TO_NS |
| 743 | ns_timestamps.extend((-ns, ns)) |
| 744 | |
| 745 | return ns_timestamps |
| 746 | |
| 747 | def _check_rounding(self, pytime_converter, expected_func, |
| 748 | use_float, unit_to_sec, value_filter=None): |
| 749 | |
| 750 | def convert_values(ns_timestamps): |
| 751 | if use_float: |
| 752 | unit_to_ns = SEC_TO_NS / float(unit_to_sec) |
| 753 | values = [ns / unit_to_ns for ns in ns_timestamps] |
| 754 | else: |
| 755 | unit_to_ns = SEC_TO_NS // unit_to_sec |
| 756 | values = [ns // unit_to_ns for ns in ns_timestamps] |
| 757 | |
| 758 | if value_filter: |
| 759 | values = filter(value_filter, values) |
| 760 | |
| 761 | # remove duplicates and sort |
| 762 | return sorted(set(values)) |
| 763 | |
| 764 | # test rounding |
| 765 | ns_timestamps = self._rounding_values(use_float) |
| 766 | valid_values = convert_values(ns_timestamps) |
| 767 | for time_rnd, decimal_rnd in ROUNDING_MODES : |
| 768 | context = decimal.getcontext() |
| 769 | context.rounding = decimal_rnd |
| 770 | |
| 771 | for value in valid_values: |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 772 | debug_info = {'value': value, 'rounding': decimal_rnd} |
| 773 | try: |
| 774 | result = pytime_converter(value, time_rnd) |
| 775 | expected = expected_func(value) |
| 776 | except Exception as exc: |
| 777 | self.fail("Error on timestamp conversion: %s" % debug_info) |
| 778 | self.assertEqual(result, |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 779 | expected, |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 780 | debug_info) |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 781 | |
| 782 | # test overflow |
| 783 | ns = self.OVERFLOW_SECONDS * SEC_TO_NS |
| 784 | ns_timestamps = (-ns, ns) |
| 785 | overflow_values = convert_values(ns_timestamps) |
| 786 | for time_rnd, _ in ROUNDING_MODES : |
| 787 | for value in overflow_values: |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 788 | debug_info = {'value': value, 'rounding': time_rnd} |
| 789 | with self.assertRaises(OverflowError, msg=debug_info): |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 790 | pytime_converter(value, time_rnd) |
| 791 | |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 792 | def check_int_rounding(self, pytime_converter, expected_func, |
| 793 | unit_to_sec=1, value_filter=None): |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 794 | self._check_rounding(pytime_converter, expected_func, |
| 795 | False, unit_to_sec, value_filter) |
| 796 | |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 797 | def check_float_rounding(self, pytime_converter, expected_func, |
| 798 | unit_to_sec=1, value_filter=None): |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 799 | self._check_rounding(pytime_converter, expected_func, |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 800 | True, unit_to_sec, value_filter) |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 801 | |
| 802 | def decimal_round(self, x): |
| 803 | d = decimal.Decimal(x) |
| 804 | d = d.quantize(1) |
| 805 | return int(d) |
| 806 | |
| 807 | |
| 808 | class TestCPyTime(CPyTimeTestCase, unittest.TestCase): |
| 809 | """ |
| 810 | Test the C _PyTime_t API. |
| 811 | """ |
| 812 | # _PyTime_t is a 64-bit signed integer |
| 813 | OVERFLOW_SECONDS = math.ceil((2**63 + 1) / SEC_TO_NS) |
| 814 | |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 815 | def test_FromSeconds(self): |
| 816 | from _testcapi import PyTime_FromSeconds |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 817 | |
| 818 | # PyTime_FromSeconds() expects a C int, reject values out of range |
| 819 | def c_int_filter(secs): |
| 820 | return (_testcapi.INT_MIN <= secs <= _testcapi.INT_MAX) |
| 821 | |
| 822 | self.check_int_rounding(lambda secs, rnd: PyTime_FromSeconds(secs), |
| 823 | lambda secs: secs * SEC_TO_NS, |
| 824 | value_filter=c_int_filter) |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 825 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 826 | # test nan |
| 827 | for time_rnd, _ in ROUNDING_MODES: |
| 828 | with self.assertRaises(TypeError): |
| 829 | PyTime_FromSeconds(float('nan')) |
| 830 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 831 | def test_FromSecondsObject(self): |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 832 | from _testcapi import PyTime_FromSecondsObject |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 833 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 834 | self.check_int_rounding( |
| 835 | PyTime_FromSecondsObject, |
| 836 | lambda secs: secs * SEC_TO_NS) |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 837 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 838 | self.check_float_rounding( |
| 839 | PyTime_FromSecondsObject, |
| 840 | lambda ns: self.decimal_round(ns * SEC_TO_NS)) |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 841 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 842 | # test nan |
| 843 | for time_rnd, _ in ROUNDING_MODES: |
| 844 | with self.assertRaises(ValueError): |
| 845 | PyTime_FromSecondsObject(float('nan'), time_rnd) |
| 846 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 847 | def test_AsSecondsDouble(self): |
| 848 | from _testcapi import PyTime_AsSecondsDouble |
| 849 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 850 | def float_converter(ns): |
| 851 | if abs(ns) % SEC_TO_NS == 0: |
| 852 | return float(ns // SEC_TO_NS) |
| 853 | else: |
| 854 | return float(ns) / SEC_TO_NS |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 855 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 856 | self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns), |
| 857 | float_converter, |
| 858 | NS_TO_SEC) |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 859 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 860 | # test nan |
| 861 | for time_rnd, _ in ROUNDING_MODES: |
| 862 | with self.assertRaises(TypeError): |
| 863 | PyTime_AsSecondsDouble(float('nan')) |
| 864 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 865 | def create_decimal_converter(self, denominator): |
| 866 | denom = decimal.Decimal(denominator) |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 867 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 868 | def converter(value): |
| 869 | d = decimal.Decimal(value) / denom |
| 870 | return self.decimal_round(d) |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 871 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 872 | return converter |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 873 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 874 | def test_AsTimeval(self): |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 875 | from _testcapi import PyTime_AsTimeval |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 876 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 877 | us_converter = self.create_decimal_converter(US_TO_NS) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 878 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 879 | def timeval_converter(ns): |
| 880 | us = us_converter(ns) |
| 881 | return divmod(us, SEC_TO_US) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 882 | |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 883 | if sys.platform == 'win32': |
| 884 | from _testcapi import LONG_MIN, LONG_MAX |
| 885 | |
| 886 | # On Windows, timeval.tv_sec type is a C long |
| 887 | def seconds_filter(secs): |
| 888 | return LONG_MIN <= secs <= LONG_MAX |
| 889 | else: |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 890 | seconds_filter = self.time_t_filter |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 891 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 892 | self.check_int_rounding(PyTime_AsTimeval, |
| 893 | timeval_converter, |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 894 | NS_TO_SEC, |
| 895 | value_filter=seconds_filter) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 896 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 897 | @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'), |
| 898 | 'need _testcapi.PyTime_AsTimespec') |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 899 | def test_AsTimespec(self): |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 900 | from _testcapi import PyTime_AsTimespec |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 901 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 902 | def timespec_converter(ns): |
| 903 | return divmod(ns, SEC_TO_NS) |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 904 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 905 | self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns), |
| 906 | timespec_converter, |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 907 | NS_TO_SEC, |
| 908 | value_filter=self.time_t_filter) |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 909 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 910 | def test_AsMilliseconds(self): |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 911 | from _testcapi import PyTime_AsMilliseconds |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 912 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 913 | self.check_int_rounding(PyTime_AsMilliseconds, |
| 914 | self.create_decimal_converter(MS_TO_NS), |
| 915 | NS_TO_SEC) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 916 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 917 | def test_AsMicroseconds(self): |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 918 | from _testcapi import PyTime_AsMicroseconds |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 919 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 920 | self.check_int_rounding(PyTime_AsMicroseconds, |
| 921 | self.create_decimal_converter(US_TO_NS), |
| 922 | NS_TO_SEC) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 923 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 924 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 925 | class TestOldPyTime(CPyTimeTestCase, unittest.TestCase): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 926 | """ |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 927 | Test the old C _PyTime_t API: _PyTime_ObjectToXXX() functions. |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 928 | """ |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 929 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 930 | # time_t is a 32-bit or 64-bit signed integer |
| 931 | OVERFLOW_SECONDS = 2 ** 64 |
| 932 | |
| 933 | def test_object_to_time_t(self): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 934 | from _testcapi import pytime_object_to_time_t |
| 935 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 936 | self.check_int_rounding(pytime_object_to_time_t, |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 937 | lambda secs: secs, |
| 938 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 939 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 940 | self.check_float_rounding(pytime_object_to_time_t, |
Victor Stinner | 350b518 | 2015-09-10 11:45:06 +0200 | [diff] [blame] | 941 | self.decimal_round, |
| 942 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 943 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 944 | def create_converter(self, sec_to_unit): |
| 945 | def converter(secs): |
| 946 | floatpart, intpart = math.modf(secs) |
| 947 | intpart = int(intpart) |
| 948 | floatpart *= sec_to_unit |
| 949 | floatpart = self.decimal_round(floatpart) |
| 950 | if floatpart < 0: |
| 951 | floatpart += sec_to_unit |
| 952 | intpart -= 1 |
| 953 | elif floatpart >= sec_to_unit: |
| 954 | floatpart -= sec_to_unit |
| 955 | intpart += 1 |
| 956 | return (intpart, floatpart) |
| 957 | return converter |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 958 | |
Victor Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame] | 959 | def test_object_to_timeval(self): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 960 | from _testcapi import pytime_object_to_timeval |
| 961 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 962 | self.check_int_rounding(pytime_object_to_timeval, |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 963 | lambda secs: (secs, 0), |
| 964 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 965 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 966 | self.check_float_rounding(pytime_object_to_timeval, |
Victor Stinner | 350b518 | 2015-09-10 11:45:06 +0200 | [diff] [blame] | 967 | self.create_converter(SEC_TO_US), |
| 968 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 969 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 970 | # test nan |
| 971 | for time_rnd, _ in ROUNDING_MODES: |
| 972 | with self.assertRaises(ValueError): |
| 973 | pytime_object_to_timeval(float('nan'), time_rnd) |
| 974 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 975 | def test_object_to_timespec(self): |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 976 | from _testcapi import pytime_object_to_timespec |
| 977 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 978 | self.check_int_rounding(pytime_object_to_timespec, |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 979 | lambda secs: (secs, 0), |
| 980 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 981 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 982 | self.check_float_rounding(pytime_object_to_timespec, |
Victor Stinner | 350b518 | 2015-09-10 11:45:06 +0200 | [diff] [blame] | 983 | self.create_converter(SEC_TO_NS), |
| 984 | value_filter=self.time_t_filter) |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 985 | |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 986 | # test nan |
| 987 | for time_rnd, _ in ROUNDING_MODES: |
| 988 | with self.assertRaises(ValueError): |
| 989 | pytime_object_to_timespec(float('nan'), time_rnd) |
| 990 | |
Victor Stinner | acea9f6 | 2015-09-02 10:39:40 +0200 | [diff] [blame] | 991 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 992 | if __name__ == "__main__": |
Ezio Melotti | 3836d70 | 2013-04-11 20:29:42 +0300 | [diff] [blame] | 993 | unittest.main() |