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