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