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