Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 2 | import time |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 3 | import unittest |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 4 | import locale |
| 5 | import sysconfig |
| 6 | import sys |
| 7 | import platform |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 8 | try: |
| 9 | import threading |
| 10 | except ImportError: |
| 11 | threading = None |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 12 | |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 13 | # Max year is only limited by the size of C int. |
| 14 | SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 |
| 15 | TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1 |
| 16 | TIME_MINYEAR = -TIME_MAXYEAR - 1 |
| 17 | |
| 18 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 19 | class TimeTestCase(unittest.TestCase): |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 20 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 21 | def setUp(self): |
| 22 | self.t = time.time() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 23 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 24 | def test_data_attributes(self): |
| 25 | time.altzone |
| 26 | time.daylight |
| 27 | time.timezone |
| 28 | time.tzname |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 29 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 30 | def test_time(self): |
| 31 | time.time() |
| 32 | info = time.get_clock_info('time') |
| 33 | self.assertEqual(info.is_monotonic, False) |
| 34 | if sys.platform != 'win32': |
| 35 | self.assertEqual(info.is_adjusted, True) |
| 36 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 37 | def test_clock(self): |
| 38 | time.clock() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 39 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 40 | info = time.get_clock_info('clock') |
| 41 | self.assertEqual(info.is_monotonic, True) |
| 42 | self.assertEqual(info.is_adjusted, False) |
| 43 | |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 44 | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
| 45 | 'need time.clock_gettime()') |
| 46 | def test_clock_realtime(self): |
| 47 | time.clock_gettime(time.CLOCK_REALTIME) |
| 48 | |
| 49 | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
| 50 | 'need time.clock_gettime()') |
| 51 | @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'), |
| 52 | 'need time.CLOCK_MONOTONIC') |
| 53 | def test_clock_monotonic(self): |
| 54 | a = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 55 | b = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 56 | self.assertLessEqual(a, b) |
| 57 | |
| 58 | @unittest.skipUnless(hasattr(time, 'clock_getres'), |
| 59 | 'need time.clock_getres()') |
| 60 | def test_clock_getres(self): |
| 61 | res = time.clock_getres(time.CLOCK_REALTIME) |
| 62 | self.assertGreater(res, 0.0) |
| 63 | self.assertLessEqual(res, 1.0) |
| 64 | |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 65 | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
| 66 | 'need time.clock_settime()') |
| 67 | def test_clock_settime(self): |
| 68 | t = time.clock_gettime(time.CLOCK_REALTIME) |
| 69 | try: |
| 70 | time.clock_settime(time.CLOCK_REALTIME, t) |
| 71 | except PermissionError: |
| 72 | pass |
| 73 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 74 | if hasattr(time, 'CLOCK_MONOTONIC'): |
| 75 | self.assertRaises(OSError, |
| 76 | time.clock_settime, time.CLOCK_MONOTONIC, 0) |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 77 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 78 | def test_conversions(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 79 | self.assertEqual(time.ctime(self.t), |
| 80 | time.asctime(time.localtime(self.t))) |
| 81 | self.assertEqual(int(time.mktime(time.localtime(self.t))), |
| 82 | int(self.t)) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 83 | |
| 84 | def test_sleep(self): |
Victor Stinner | 7f53a50 | 2011-07-05 22:00:25 +0200 | [diff] [blame] | 85 | self.assertRaises(ValueError, time.sleep, -2) |
| 86 | self.assertRaises(ValueError, time.sleep, -1) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 87 | time.sleep(1.2) |
| 88 | |
| 89 | def test_strftime(self): |
| 90 | tt = time.gmtime(self.t) |
| 91 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 92 | 'j', 'm', 'M', 'p', 'S', |
| 93 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 94 | format = ' %' + directive |
| 95 | try: |
| 96 | time.strftime(format, tt) |
| 97 | except ValueError: |
| 98 | self.fail('conversion specifier: %r failed.' % format) |
| 99 | |
Senthil Kumaran | 8f377a3 | 2011-04-06 12:54:06 +0800 | [diff] [blame] | 100 | # Issue #10762: Guard against invalid/non-supported format string |
| 101 | # so that Python don't crash (Windows crashes when the format string |
| 102 | # input to [w]strftime is not kosher. |
| 103 | if sys.platform.startswith('win'): |
| 104 | with self.assertRaises(ValueError): |
| 105 | time.strftime('%f') |
| 106 | |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 107 | def _bounds_checking(self, func): |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 108 | # Make sure that strftime() checks the bounds of the various parts |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 109 | # of the time tuple (0 is valid for *all* values). |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 110 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 111 | # The year field is tested by other test cases above |
| 112 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | # Check month [1, 12] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 114 | func((1900, 0, 1, 0, 0, 0, 0, 1, -1)) |
| 115 | func((1900, 12, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 116 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 117 | (1900, -1, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 118 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 119 | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | # Check day of month [1, 31] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 121 | func((1900, 1, 0, 0, 0, 0, 0, 1, -1)) |
| 122 | func((1900, 1, 31, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 123 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 124 | (1900, 1, -1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 125 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 126 | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | # Check hour [0, 23] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 128 | func((1900, 1, 1, 23, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 129 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 130 | (1900, 1, 1, -1, 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, 1, 1, 24, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 133 | # Check minute [0, 59] |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 134 | func((1900, 1, 1, 0, 59, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 135 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 136 | (1900, 1, 1, 0, -1, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 137 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 138 | (1900, 1, 1, 0, 60, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 139 | # Check second [0, 61] |
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, 1, 1, 0, 0, -1, 0, 1, -1)) |
| 142 | # C99 only requires allowing for one leap second, but Python's docs say |
| 143 | # allow two leap seconds (0..61) |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 144 | func((1900, 1, 1, 0, 0, 60, 0, 1, -1)) |
| 145 | func((1900, 1, 1, 0, 0, 61, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 146 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 147 | (1900, 1, 1, 0, 0, 62, 0, 1, -1)) |
| 148 | # No check for upper-bound day of week; |
| 149 | # value forced into range by a ``% 7`` calculation. |
| 150 | # Start check at -2 since gettmarg() increments value before taking |
| 151 | # modulo. |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 152 | self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)), |
| 153 | func((1900, 1, 1, 0, 0, 0, +6, 1, -1))) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 154 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 155 | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 156 | # Check day of the year [1, 366] + zero support |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 157 | func((1900, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 158 | func((1900, 1, 1, 0, 0, 0, 0, 366, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 159 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 160 | (1900, 1, 1, 0, 0, 0, 0, -1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 161 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 162 | (1900, 1, 1, 0, 0, 0, 0, 367, -1)) |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 163 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 164 | def test_strftime_bounding_check(self): |
| 165 | self._bounds_checking(lambda tup: time.strftime('', tup)) |
| 166 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 167 | def test_default_values_for_zero(self): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 168 | # Make sure that using all zeros uses the proper default |
| 169 | # values. No test for daylight savings since strftime() does |
| 170 | # not change output based on its value and no test for year |
| 171 | # because systems vary in their support for year 0. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | expected = "2000 01 01 00 00 00 1 001" |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 173 | with support.check_warnings(): |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 174 | 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] | 175 | self.assertEqual(expected, result) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 177 | def test_strptime(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 178 | # Should be able to go round-trip from strftime to strptime without |
| 179 | # throwing an exception. |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 180 | tt = time.gmtime(self.t) |
| 181 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 182 | 'j', 'm', 'M', 'p', 'S', |
| 183 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 184 | format = '%' + directive |
| 185 | strf_output = time.strftime(format, tt) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 186 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 187 | time.strptime(strf_output, format) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 188 | except ValueError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 189 | self.fail("conversion specifier %r failed with '%s' input." % |
| 190 | (format, strf_output)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 191 | |
Brett Cannon | 7f6b4f8 | 2009-03-30 21:30:26 +0000 | [diff] [blame] | 192 | def test_strptime_bytes(self): |
| 193 | # Make sure only strings are accepted as arguments to strptime. |
| 194 | self.assertRaises(TypeError, time.strptime, b'2009', "%Y") |
| 195 | self.assertRaises(TypeError, time.strptime, '2009', b'%Y') |
| 196 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 197 | def test_asctime(self): |
| 198 | time.asctime(time.gmtime(self.t)) |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 199 | |
| 200 | # Max year is only limited by the size of C int. |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 201 | for bigyear in TIME_MAXYEAR, TIME_MINYEAR: |
| 202 | asc = time.asctime((bigyear, 6, 1) + (0,) * 6) |
| 203 | self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) |
| 204 | self.assertRaises(OverflowError, time.asctime, |
| 205 | (TIME_MAXYEAR + 1,) + (0,) * 8) |
| 206 | self.assertRaises(OverflowError, time.asctime, |
| 207 | (TIME_MINYEAR - 1,) + (0,) * 8) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 208 | self.assertRaises(TypeError, time.asctime, 0) |
Alexander Belopolsky | e2dc082 | 2011-01-02 20:48:22 +0000 | [diff] [blame] | 209 | self.assertRaises(TypeError, time.asctime, ()) |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 210 | self.assertRaises(TypeError, time.asctime, (0,) * 10) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 211 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 212 | def test_asctime_bounding_check(self): |
| 213 | self._bounds_checking(time.asctime) |
| 214 | |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 215 | def test_ctime(self): |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 216 | t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1)) |
| 217 | self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973') |
| 218 | t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 219 | self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000') |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 220 | for year in [-100, 100, 1000, 2000, 10000]: |
| 221 | try: |
| 222 | testval = time.mktime((year, 1, 10) + (0,)*6) |
| 223 | except (ValueError, OverflowError): |
| 224 | # If mktime fails, ctime will fail too. This may happen |
| 225 | # on some platforms. |
| 226 | pass |
| 227 | else: |
| 228 | self.assertEqual(time.ctime(testval)[20:], str(year)) |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 229 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 230 | @unittest.skipUnless(hasattr(time, "tzset"), |
| 231 | "time module has no attribute tzset") |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 232 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 234 | from os import environ |
| 235 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 236 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 237 | # hemisphere. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 238 | xmas2002 = 1040774400.0 |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 239 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 240 | # These formats are correct for 2002, and possibly future years |
| 241 | # This format is the 'standard' as documented at: |
| 242 | # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html |
| 243 | # They are also documented in the tzset(3) man page on most Unix |
| 244 | # systems. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 245 | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 246 | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
| 247 | utc='UTC+0' |
| 248 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 249 | org_TZ = environ.get('TZ',None) |
| 250 | try: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 251 | # Make sure we can switch to UTC time and results are correct |
| 252 | # Note that unknown timezones default to UTC. |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 253 | # Note that altzone is undefined in UTC, as there is no DST |
| 254 | environ['TZ'] = eastern |
| 255 | time.tzset() |
| 256 | environ['TZ'] = utc |
| 257 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertEqual( |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 259 | time.gmtime(xmas2002), time.localtime(xmas2002) |
| 260 | ) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 261 | self.assertEqual(time.daylight, 0) |
| 262 | self.assertEqual(time.timezone, 0) |
| 263 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 264 | |
| 265 | # Make sure we can switch to US/Eastern |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 266 | environ['TZ'] = eastern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 267 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 268 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 269 | self.assertEqual(time.tzname, ('EST', 'EDT')) |
| 270 | self.assertEqual(len(time.tzname), 2) |
| 271 | self.assertEqual(time.daylight, 1) |
| 272 | self.assertEqual(time.timezone, 18000) |
| 273 | self.assertEqual(time.altzone, 14400) |
| 274 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
| 275 | self.assertEqual(len(time.tzname), 2) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 276 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 277 | # Now go to the southern hemisphere. |
| 278 | environ['TZ'] = victoria |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 279 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 280 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
Victor Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 281 | |
| 282 | # Issue #11886: Australian Eastern Standard Time (UTC+10) is called |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 283 | # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" |
| 284 | # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone), |
| 285 | # on some operating systems (e.g. FreeBSD), which is wrong. See for |
| 286 | # example this bug: |
| 287 | # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810 |
Victor Stinner | 0cd4790 | 2011-12-08 00:32:51 +0100 | [diff] [blame] | 288 | self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) |
Victor Stinner | 10a6ddb | 2011-12-10 14:37:53 +0100 | [diff] [blame] | 289 | self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 290 | self.assertEqual(len(time.tzname), 2) |
| 291 | self.assertEqual(time.daylight, 1) |
| 292 | self.assertEqual(time.timezone, -36000) |
| 293 | self.assertEqual(time.altzone, -39600) |
| 294 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 1) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 296 | finally: |
| 297 | # Repair TZ environment variable in case any other tests |
| 298 | # rely on it. |
| 299 | if org_TZ is not None: |
| 300 | environ['TZ'] = org_TZ |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 301 | elif 'TZ' in environ: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 302 | del environ['TZ'] |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 303 | time.tzset() |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 304 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 305 | def test_insane_timestamps(self): |
| 306 | # It's possible that some platform maps time_t to double, |
| 307 | # and that this test will fail there. This test should |
| 308 | # exempt such platforms (provided they return reasonable |
| 309 | # results!). |
| 310 | for func in time.ctime, time.gmtime, time.localtime: |
| 311 | for unreasonable in -1e200, 1e200: |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 312 | self.assertRaises(OverflowError, func, unreasonable) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 313 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 314 | def test_ctime_without_arg(self): |
| 315 | # Not sure how to check the values, since the clock could tick |
| 316 | # at any time. Make sure these are at least accepted and |
| 317 | # don't raise errors. |
| 318 | time.ctime() |
| 319 | time.ctime(None) |
| 320 | |
| 321 | def test_gmtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 322 | gt0 = time.gmtime() |
| 323 | gt1 = time.gmtime(None) |
| 324 | t0 = time.mktime(gt0) |
| 325 | t1 = time.mktime(gt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 326 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 327 | |
| 328 | def test_localtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 329 | lt0 = time.localtime() |
| 330 | lt1 = time.localtime(None) |
| 331 | t0 = time.mktime(lt0) |
| 332 | t1 = time.mktime(lt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 333 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 334 | |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 335 | def test_mktime(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 336 | # Issue #1726687 |
| 337 | for t in (-2, -1, 0, 1): |
| 338 | try: |
| 339 | tt = time.localtime(t) |
Victor Stinner | 2cbae98 | 2012-01-27 00:50:33 +0100 | [diff] [blame] | 340 | except (OverflowError, OSError): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 341 | pass |
| 342 | else: |
| 343 | self.assertEqual(time.mktime(tt), t) |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 344 | |
| 345 | # Issue #13309: passing extreme values to mktime() or localtime() |
| 346 | # borks the glibc's internal timezone data. |
| 347 | @unittest.skipUnless(platform.libc_ver()[0] != 'glibc', |
| 348 | "disabled because of a bug in glibc. Issue #13309") |
| 349 | def test_mktime_error(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 350 | # It may not be possible to reliably make mktime return error |
| 351 | # on all platfom. This will make sure that no other exception |
| 352 | # than OverflowError is raised for an extreme value. |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 353 | tt = time.gmtime(self.t) |
| 354 | tzname = time.strftime('%Z', tt) |
| 355 | self.assertNotEqual(tzname, 'LMT') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 356 | try: |
| 357 | time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) |
| 358 | except OverflowError: |
| 359 | pass |
Florent Xicluna | e54371e | 2011-11-11 18:59:30 +0100 | [diff] [blame] | 360 | self.assertEqual(time.strftime('%Z', tt), tzname) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 361 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 362 | @unittest.skipUnless(hasattr(time, 'monotonic'), |
| 363 | 'need time.monotonic') |
| 364 | def test_monotonic(self): |
| 365 | t1 = time.monotonic() |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 366 | time.sleep(0.1) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 367 | t2 = time.monotonic() |
Victor Stinner | 2dd254d | 2012-01-20 02:24:18 +0100 | [diff] [blame] | 368 | dt = t2 - t1 |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 369 | self.assertGreater(t2, t1) |
Victor Stinner | 2dd254d | 2012-01-20 02:24:18 +0100 | [diff] [blame] | 370 | self.assertAlmostEqual(dt, 0.1, delta=0.2) |
Antoine Pitrou | 391166f | 2012-01-18 22:35:21 +0100 | [diff] [blame] | 371 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 372 | info = time.get_clock_info('monotonic') |
| 373 | self.assertEqual(info.is_monotonic, True) |
| 374 | if sys.platform == 'linux': |
| 375 | self.assertEqual(info.is_adjusted, True) |
| 376 | else: |
| 377 | self.assertEqual(info.is_adjusted, False) |
| 378 | |
| 379 | def test_perf_counter(self): |
| 380 | time.perf_counter() |
| 381 | |
| 382 | def test_process_time(self): |
| 383 | start = time.process_time() |
| 384 | time.sleep(0.1) |
| 385 | stop = time.process_time() |
| 386 | self.assertLess(stop - start, 0.01) |
| 387 | |
| 388 | info = time.get_clock_info('process_time') |
| 389 | self.assertEqual(info.is_monotonic, True) |
| 390 | self.assertEqual(info.is_adjusted, False) |
| 391 | |
| 392 | @unittest.skipUnless(threading, |
| 393 | 'need threading') |
| 394 | def test_process_time_threads(self): |
| 395 | class BusyThread(threading.Thread): |
| 396 | def run(self): |
| 397 | while not self.stop: |
| 398 | pass |
| 399 | |
| 400 | thread = BusyThread() |
| 401 | thread.stop = False |
| 402 | t1 = time.process_time() |
| 403 | thread.start() |
| 404 | time.sleep(0.2) |
| 405 | t2 = time.process_time() |
| 406 | thread.stop = True |
| 407 | thread.join() |
| 408 | self.assertGreater(t2 - t1, 0.1) |
| 409 | |
| 410 | @unittest.skipUnless(hasattr(time, 'monotonic'), |
| 411 | 'need time.monotonic') |
| 412 | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
| 413 | 'need time.clock_settime') |
| 414 | def test_monotonic_settime(self): |
| 415 | t1 = time.monotonic() |
| 416 | realtime = time.clock_gettime(time.CLOCK_REALTIME) |
| 417 | # jump backward with an offset of 1 hour |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 418 | try: |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 419 | time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) |
| 420 | except PermissionError as err: |
| 421 | self.skipTest(err) |
| 422 | t2 = time.monotonic() |
| 423 | time.clock_settime(time.CLOCK_REALTIME, realtime) |
| 424 | # monotonic must not be affected by system clock updates |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 425 | self.assertGreaterEqual(t2, t1) |
| 426 | |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 427 | def test_localtime_failure(self): |
| 428 | # Issue #13847: check for localtime() failure |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 429 | invalid_time_t = None |
| 430 | for time_t in (-1, 2**30, 2**33, 2**60): |
| 431 | try: |
| 432 | time.localtime(time_t) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 433 | except OverflowError: |
| 434 | self.skipTest("need 64-bit time_t") |
Victor Stinner | 53d3645 | 2012-01-27 01:03:25 +0100 | [diff] [blame] | 435 | except OSError: |
| 436 | invalid_time_t = time_t |
| 437 | break |
| 438 | if invalid_time_t is None: |
| 439 | self.skipTest("unable to find an invalid time_t value") |
| 440 | |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 441 | self.assertRaises(OSError, time.localtime, invalid_time_t) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 442 | self.assertRaises(OSError, time.ctime, invalid_time_t) |
Victor Stinner | b94b266 | 2012-01-18 01:50:21 +0100 | [diff] [blame] | 443 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame^] | 444 | def test_get_clock_info(self): |
| 445 | clocks = ['clock', 'perf_counter', 'process_time', 'time'] |
| 446 | if hasattr(time, 'monotonic'): |
| 447 | clocks.append('monotonic') |
| 448 | |
| 449 | for name in clocks: |
| 450 | info = time.get_clock_info(name) |
| 451 | #self.assertIsInstance(info, dict) |
| 452 | self.assertIsInstance(info.implementation, str) |
| 453 | self.assertNotEqual(info.implementation, '') |
| 454 | self.assertIsInstance(info.is_monotonic, bool) |
| 455 | self.assertIsInstance(info.resolution, float) |
| 456 | # 0.0 < resolution <= 1.0 |
| 457 | self.assertGreater(info.resolution, 0.0) |
| 458 | self.assertLessEqual(info.resolution, 1.0) |
| 459 | self.assertIsInstance(info.is_adjusted, bool) |
| 460 | |
| 461 | self.assertRaises(ValueError, time.get_clock_info, 'xxx') |
| 462 | |
| 463 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 464 | class TestLocale(unittest.TestCase): |
| 465 | def setUp(self): |
| 466 | self.oldloc = locale.setlocale(locale.LC_ALL) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 467 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 468 | def tearDown(self): |
| 469 | locale.setlocale(locale.LC_ALL, self.oldloc) |
| 470 | |
Martin v. Löwis | a6a9c4d | 2009-05-30 06:15:30 +0000 | [diff] [blame] | 471 | def test_bug_3061(self): |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 472 | try: |
| 473 | tmp = locale.setlocale(locale.LC_ALL, "fr_FR") |
| 474 | except locale.Error: |
| 475 | # skip this test |
| 476 | return |
| 477 | # This should not cause an exception |
| 478 | time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) |
| 479 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 480 | |
| 481 | class _BaseYearTest(unittest.TestCase): |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 482 | def yearstr(self, y): |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 483 | raise NotImplementedError() |
| 484 | |
| 485 | class _TestAsctimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 486 | _format = '%d' |
| 487 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 488 | def yearstr(self, y): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 489 | return time.asctime((y,) + (0,) * 8).split()[-1] |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 490 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 491 | def test_large_year(self): |
Victor Stinner | 7369132 | 2011-01-08 02:00:24 +0000 | [diff] [blame] | 492 | # Check that it doesn't crash for year > 9999 |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 493 | self.assertEqual(self.yearstr(12345), '12345') |
| 494 | self.assertEqual(self.yearstr(123456789), '123456789') |
| 495 | |
| 496 | class _TestStrftimeYear: |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 497 | |
| 498 | # Issue 13305: For years < 1000, the value is not always |
| 499 | # padded to 4 digits across platforms. The C standard |
| 500 | # assumes year >= 1900, so it does not specify the number |
| 501 | # of digits. |
| 502 | |
| 503 | if time.strftime('%Y', (1,) + (0,) * 8) == '0001': |
| 504 | _format = '%04d' |
| 505 | else: |
| 506 | _format = '%d' |
| 507 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 508 | def yearstr(self, y): |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 509 | return time.strftime('%Y', (y,) + (0,) * 8) |
| 510 | |
| 511 | def test_4dyear(self): |
| 512 | # Check that we can return the zero padded value. |
| 513 | if self._format == '%04d': |
| 514 | self.test_year('%04d') |
| 515 | else: |
| 516 | def year4d(y): |
| 517 | return time.strftime('%4Y', (y,) + (0,) * 8) |
| 518 | self.test_year('%04d', func=year4d) |
| 519 | |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 520 | def skip_if_not_supported(y): |
| 521 | msg = "strftime() is limited to [1; 9999] with Visual Studio" |
| 522 | # Check that it doesn't crash for year > 9999 |
| 523 | try: |
| 524 | time.strftime('%Y', (y,) + (0,) * 8) |
| 525 | except ValueError: |
| 526 | cond = False |
| 527 | else: |
| 528 | cond = True |
| 529 | return unittest.skipUnless(cond, msg) |
| 530 | |
| 531 | @skip_if_not_supported(10000) |
| 532 | def test_large_year(self): |
| 533 | return super().test_large_year() |
| 534 | |
| 535 | @skip_if_not_supported(0) |
| 536 | def test_negative(self): |
| 537 | return super().test_negative() |
| 538 | |
| 539 | del skip_if_not_supported |
| 540 | |
| 541 | |
Florent Xicluna | 49ce068 | 2011-11-01 12:56:14 +0100 | [diff] [blame] | 542 | class _Test4dYear(_BaseYearTest): |
| 543 | _format = '%d' |
| 544 | |
| 545 | def test_year(self, fmt=None, func=None): |
| 546 | fmt = fmt or self._format |
| 547 | func = func or self.yearstr |
| 548 | self.assertEqual(func(1), fmt % 1) |
| 549 | self.assertEqual(func(68), fmt % 68) |
| 550 | self.assertEqual(func(69), fmt % 69) |
| 551 | self.assertEqual(func(99), fmt % 99) |
| 552 | self.assertEqual(func(999), fmt % 999) |
| 553 | self.assertEqual(func(9999), fmt % 9999) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 554 | |
| 555 | def test_large_year(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 556 | self.assertEqual(self.yearstr(12345), '12345') |
Victor Stinner | 13ed2ea | 2011-03-21 02:11:01 +0100 | [diff] [blame] | 557 | self.assertEqual(self.yearstr(123456789), '123456789') |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 558 | self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR)) |
| 559 | self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 560 | |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 561 | def test_negative(self): |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 562 | self.assertEqual(self.yearstr(-1), self._format % -1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 563 | self.assertEqual(self.yearstr(-1234), '-1234') |
| 564 | self.assertEqual(self.yearstr(-123456), '-123456') |
Florent Xicluna | d1bd7f7 | 2011-11-01 23:42:05 +0100 | [diff] [blame] | 565 | self.assertEqual(self.yearstr(-123456789), str(-123456789)) |
| 566 | self.assertEqual(self.yearstr(-1234567890), str(-1234567890)) |
Florent Xicluna | 2fbc185 | 2011-11-02 08:13:43 +0100 | [diff] [blame] | 567 | self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900)) |
| 568 | # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900 |
| 569 | # Skip the value test, but check that no error is raised |
| 570 | self.yearstr(TIME_MINYEAR) |
Florent Xicluna | e2a732e | 2011-11-02 01:28:17 +0100 | [diff] [blame] | 571 | # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR)) |
Florent Xicluna | bceb528 | 2011-11-01 14:11:34 +0100 | [diff] [blame] | 572 | self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1) |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 573 | |
Alexander Belopolsky | b7d40d1 | 2011-01-11 01:21:25 +0000 | [diff] [blame] | 574 | |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 575 | class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear): |
| 576 | pass |
| 577 | |
| 578 | class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear): |
Victor Stinner | 301f121 | 2011-01-08 03:06:52 +0000 | [diff] [blame] | 579 | pass |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 580 | |
Alexander Belopolsky | 0dd06f4 | 2011-01-08 01:23:02 +0000 | [diff] [blame] | 581 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 582 | class TestPytime(unittest.TestCase): |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 583 | def setUp(self): |
| 584 | self.invalid_values = ( |
| 585 | -(2 ** 100), 2 ** 100, |
| 586 | -(2.0 ** 100.0), 2.0 ** 100.0, |
| 587 | ) |
| 588 | |
| 589 | def test_time_t(self): |
| 590 | from _testcapi import pytime_object_to_time_t |
| 591 | for obj, time_t in ( |
| 592 | (0, 0), |
| 593 | (-1, -1), |
| 594 | (-1.0, -1), |
| 595 | (-1.9, -1), |
| 596 | (1.0, 1), |
| 597 | (1.9, 1), |
| 598 | ): |
| 599 | self.assertEqual(pytime_object_to_time_t(obj), time_t) |
| 600 | |
| 601 | for invalid in self.invalid_values: |
| 602 | self.assertRaises(OverflowError, pytime_object_to_time_t, invalid) |
| 603 | |
| 604 | def test_timeval(self): |
| 605 | from _testcapi import pytime_object_to_timeval |
| 606 | for obj, timeval in ( |
| 607 | (0, (0, 0)), |
| 608 | (-1, (-1, 0)), |
| 609 | (-1.0, (-1, 0)), |
| 610 | (1e-6, (0, 1)), |
| 611 | (-1e-6, (-1, 999999)), |
| 612 | (-1.2, (-2, 800000)), |
| 613 | (1.1234560, (1, 123456)), |
| 614 | (1.1234569, (1, 123456)), |
| 615 | (-1.1234560, (-2, 876544)), |
| 616 | (-1.1234561, (-2, 876543)), |
| 617 | ): |
| 618 | self.assertEqual(pytime_object_to_timeval(obj), timeval) |
| 619 | |
| 620 | for invalid in self.invalid_values: |
| 621 | self.assertRaises(OverflowError, pytime_object_to_timeval, invalid) |
| 622 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 623 | def test_timespec(self): |
| 624 | from _testcapi import pytime_object_to_timespec |
| 625 | for obj, timespec in ( |
| 626 | (0, (0, 0)), |
| 627 | (-1, (-1, 0)), |
| 628 | (-1.0, (-1, 0)), |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 629 | (1e-9, (0, 1)), |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 630 | (-1e-9, (-1, 999999999)), |
| 631 | (-1.2, (-2, 800000000)), |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 632 | (1.1234567890, (1, 123456789)), |
| 633 | (1.1234567899, (1, 123456789)), |
| 634 | (-1.1234567890, (-2, 876543211)), |
| 635 | (-1.1234567891, (-2, 876543210)), |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 636 | ): |
| 637 | self.assertEqual(pytime_object_to_timespec(obj), timespec) |
| 638 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 639 | for invalid in self.invalid_values: |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 640 | self.assertRaises(OverflowError, pytime_object_to_timespec, invalid) |
| 641 | |
| 642 | |
| 643 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 644 | def test_main(): |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 645 | support.run_unittest( |
| 646 | TimeTestCase, |
| 647 | TestLocale, |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 648 | TestAsctime4dyear, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 649 | TestStrftime4dyear, |
| 650 | TestPytime) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 651 | |
| 652 | if __name__ == "__main__": |
| 653 | test_main() |