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 |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 6 | import warnings |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 7 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 8 | class TimeTestCase(unittest.TestCase): |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 9 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 10 | def setUp(self): |
| 11 | self.t = time.time() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 12 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 13 | def test_data_attributes(self): |
| 14 | time.altzone |
| 15 | time.daylight |
| 16 | time.timezone |
| 17 | time.tzname |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 18 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 19 | def test_clock(self): |
| 20 | time.clock() |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 21 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 22 | def test_conversions(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 23 | self.assertEqual(time.ctime(self.t), |
| 24 | time.asctime(time.localtime(self.t))) |
| 25 | self.assertEqual(int(time.mktime(time.localtime(self.t))), |
| 26 | int(self.t)) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 27 | |
| 28 | def test_sleep(self): |
| 29 | time.sleep(1.2) |
| 30 | |
| 31 | def test_strftime(self): |
| 32 | tt = time.gmtime(self.t) |
| 33 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 34 | 'j', 'm', 'M', 'p', 'S', |
| 35 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 36 | format = ' %' + directive |
| 37 | try: |
| 38 | time.strftime(format, tt) |
| 39 | except ValueError: |
| 40 | self.fail('conversion specifier: %r failed.' % format) |
| 41 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 42 | def _bounds_checking(self, func=time.strftime): |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 43 | # Make sure that strftime() checks the bounds of the various parts |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | #of the time tuple (0 is valid for *all* values). |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 45 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 46 | # Check year [1900, max(int)] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 47 | self.assertRaises(ValueError, func, |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 48 | (999, 1, 1, 0, 0, 0, 0, 1, -1)) |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 49 | if time.accept2dyear: |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 50 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 51 | (-1, 1, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 52 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 53 | (100, 1, 1, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | # Check month [1, 12] + zero support |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 55 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | (1900, -1, 1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 57 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 58 | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | # Check day of month [1, 31] + zero support |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 60 | self.assertRaises(ValueError, func, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 61 | (1900, 1, -1, 0, 0, 0, 0, 1, -1)) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 62 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 63 | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | # Check hour [0, 23] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 65 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 66 | (1900, 1, 1, -1, 0, 0, 0, 1, -1)) |
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, 24, 0, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | # Check minute [0, 59] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 70 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 71 | (1900, 1, 1, 0, -1, 0, 0, 1, -1)) |
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, 60, 0, 0, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | # Check second [0, 61] |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 75 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 76 | (1900, 1, 1, 0, 0, -1, 0, 1, -1)) |
| 77 | # C99 only requires allowing for one leap second, but Python's docs say |
| 78 | # allow two leap seconds (0..61) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 79 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 80 | (1900, 1, 1, 0, 0, 62, 0, 1, -1)) |
| 81 | # No check for upper-bound day of week; |
| 82 | # value forced into range by a ``% 7`` calculation. |
| 83 | # Start check at -2 since gettmarg() increments value before taking |
| 84 | # modulo. |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 85 | self.assertRaises(ValueError, func, |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 86 | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | # Check day of the year [1, 366] + zero support |
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, 1, 1, 0, 0, 0, 0, 367, -1)) |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 92 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 93 | def test_strftime_bounding_check(self): |
| 94 | self._bounds_checking(lambda tup: time.strftime('', tup)) |
| 95 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | def test_default_values_for_zero(self): |
| 97 | # Make sure that using all zeros uses the proper default values. |
| 98 | # No test for daylight savings since strftime() does not change output |
| 99 | # based on its value. |
| 100 | expected = "2000 01 01 00 00 00 1 001" |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 101 | with support.check_warnings(): |
| 102 | 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] | 103 | self.assertEqual(expected, result) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 105 | def test_strptime(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 106 | # Should be able to go round-trip from strftime to strptime without |
| 107 | # throwing an exception. |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 108 | tt = time.gmtime(self.t) |
| 109 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 110 | 'j', 'm', 'M', 'p', 'S', |
| 111 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | format = '%' + directive |
| 113 | strf_output = time.strftime(format, tt) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 114 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 115 | time.strptime(strf_output, format) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 116 | except ValueError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 117 | self.fail("conversion specifier %r failed with '%s' input." % |
| 118 | (format, strf_output)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 119 | |
Brett Cannon | 7f6b4f8 | 2009-03-30 21:30:26 +0000 | [diff] [blame] | 120 | def test_strptime_bytes(self): |
| 121 | # Make sure only strings are accepted as arguments to strptime. |
| 122 | self.assertRaises(TypeError, time.strptime, b'2009', "%Y") |
| 123 | self.assertRaises(TypeError, time.strptime, '2009', b'%Y') |
| 124 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 125 | def test_asctime(self): |
| 126 | time.asctime(time.gmtime(self.t)) |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 127 | |
| 128 | # Max year is only limited by the size of C int. |
Antoine Pitrou | 1ec121d | 2011-01-04 22:54:30 +0000 | [diff] [blame] | 129 | sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4 |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 130 | bigyear = (1 << 8 * sizeof_int - 1) - 1 |
| 131 | asc = time.asctime((bigyear, 6, 1) + (0,)*6) |
| 132 | self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) |
| 133 | self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 134 | self.assertRaises(TypeError, time.asctime, 0) |
Alexander Belopolsky | e2dc082 | 2011-01-02 20:48:22 +0000 | [diff] [blame] | 135 | self.assertRaises(TypeError, time.asctime, ()) |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, time.asctime, (0,) * 10) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 137 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 138 | def test_asctime_bounding_check(self): |
| 139 | self._bounds_checking(time.asctime) |
| 140 | |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 141 | def test_ctime(self): |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 142 | t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1)) |
| 143 | self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973') |
| 144 | t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 145 | self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000') |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 146 | for year in [-100, 100, 1000, 2000, 10000]: |
| 147 | try: |
| 148 | testval = time.mktime((year, 1, 10) + (0,)*6) |
| 149 | except (ValueError, OverflowError): |
| 150 | # If mktime fails, ctime will fail too. This may happen |
| 151 | # on some platforms. |
| 152 | pass |
| 153 | else: |
| 154 | self.assertEqual(time.ctime(testval)[20:], str(year)) |
Georg Brandl | e10608c | 2011-01-02 22:33:43 +0000 | [diff] [blame] | 155 | |
R. David Murray | 6ecf76e | 2010-12-14 01:22:50 +0000 | [diff] [blame] | 156 | @unittest.skipIf(not hasattr(time, "tzset"), |
| 157 | "time module has no attribute tzset") |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 158 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 160 | from os import environ |
| 161 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 162 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 163 | # hemisphere. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 164 | xmas2002 = 1040774400.0 |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 165 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 166 | # These formats are correct for 2002, and possibly future years |
| 167 | # This format is the 'standard' as documented at: |
| 168 | # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html |
| 169 | # They are also documented in the tzset(3) man page on most Unix |
| 170 | # systems. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 171 | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 172 | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
| 173 | utc='UTC+0' |
| 174 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 175 | org_TZ = environ.get('TZ',None) |
| 176 | try: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 177 | # Make sure we can switch to UTC time and results are correct |
| 178 | # Note that unknown timezones default to UTC. |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 179 | # Note that altzone is undefined in UTC, as there is no DST |
| 180 | environ['TZ'] = eastern |
| 181 | time.tzset() |
| 182 | environ['TZ'] = utc |
| 183 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 184 | self.assertEqual( |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 185 | time.gmtime(xmas2002), time.localtime(xmas2002) |
| 186 | ) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 187 | self.assertEqual(time.daylight, 0) |
| 188 | self.assertEqual(time.timezone, 0) |
| 189 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 190 | |
| 191 | # Make sure we can switch to US/Eastern |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 192 | environ['TZ'] = eastern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 193 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 194 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 195 | self.assertEqual(time.tzname, ('EST', 'EDT')) |
| 196 | self.assertEqual(len(time.tzname), 2) |
| 197 | self.assertEqual(time.daylight, 1) |
| 198 | self.assertEqual(time.timezone, 18000) |
| 199 | self.assertEqual(time.altzone, 14400) |
| 200 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
| 201 | self.assertEqual(len(time.tzname), 2) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 202 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 203 | # Now go to the southern hemisphere. |
| 204 | environ['TZ'] = victoria |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 205 | time.tzset() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 206 | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 207 | self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0])) |
| 208 | self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1])) |
| 209 | self.assertEqual(len(time.tzname), 2) |
| 210 | self.assertEqual(time.daylight, 1) |
| 211 | self.assertEqual(time.timezone, -36000) |
| 212 | self.assertEqual(time.altzone, -39600) |
| 213 | self.assertEqual(time.localtime(xmas2002).tm_isdst, 1) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 215 | finally: |
| 216 | # Repair TZ environment variable in case any other tests |
| 217 | # rely on it. |
| 218 | if org_TZ is not None: |
| 219 | environ['TZ'] = org_TZ |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 220 | elif 'TZ' in environ: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 221 | del environ['TZ'] |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 222 | time.tzset() |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 223 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 224 | def test_insane_timestamps(self): |
| 225 | # It's possible that some platform maps time_t to double, |
| 226 | # and that this test will fail there. This test should |
| 227 | # exempt such platforms (provided they return reasonable |
| 228 | # results!). |
| 229 | for func in time.ctime, time.gmtime, time.localtime: |
| 230 | for unreasonable in -1e200, 1e200: |
| 231 | self.assertRaises(ValueError, func, unreasonable) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 232 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 233 | def test_ctime_without_arg(self): |
| 234 | # Not sure how to check the values, since the clock could tick |
| 235 | # at any time. Make sure these are at least accepted and |
| 236 | # don't raise errors. |
| 237 | time.ctime() |
| 238 | time.ctime(None) |
| 239 | |
| 240 | def test_gmtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 241 | gt0 = time.gmtime() |
| 242 | gt1 = time.gmtime(None) |
| 243 | t0 = time.mktime(gt0) |
| 244 | t1 = time.mktime(gt1) |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 245 | self.assertAlmostEqual(t1, t0, delta=0.2) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 246 | |
| 247 | def test_localtime_without_arg(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 248 | lt0 = time.localtime() |
| 249 | lt1 = time.localtime(None) |
| 250 | t0 = time.mktime(lt0) |
| 251 | t1 = time.mktime(lt1) |
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 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 254 | class TestLocale(unittest.TestCase): |
| 255 | def setUp(self): |
| 256 | self.oldloc = locale.setlocale(locale.LC_ALL) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 257 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 258 | def tearDown(self): |
| 259 | locale.setlocale(locale.LC_ALL, self.oldloc) |
| 260 | |
Martin v. Löwis | a6a9c4d | 2009-05-30 06:15:30 +0000 | [diff] [blame] | 261 | def test_bug_3061(self): |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 262 | try: |
| 263 | tmp = locale.setlocale(locale.LC_ALL, "fr_FR") |
| 264 | except locale.Error: |
| 265 | # skip this test |
| 266 | return |
| 267 | # This should not cause an exception |
| 268 | time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) |
| 269 | |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 270 | class TestAccept2Year(unittest.TestCase): |
| 271 | accept2dyear = 1 |
| 272 | def setUp(self): |
| 273 | self.saved_accept2dyear = time.accept2dyear |
| 274 | time.accept2dyear = self.accept2dyear |
| 275 | |
| 276 | def tearDown(self): |
| 277 | time.accept2dyear = self.saved_accept2dyear |
| 278 | |
| 279 | def yearstr(self, y): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 280 | # return time.strftime('%Y', (y,) + (0,) * 8) |
| 281 | return time.asctime((y,) + (0,) * 8).split()[-1] |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 282 | |
| 283 | def test_2dyear(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 284 | with support.check_warnings(): |
| 285 | self.assertEqual(self.yearstr(0), '2000') |
| 286 | self.assertEqual(self.yearstr(69), '1969') |
| 287 | self.assertEqual(self.yearstr(68), '2068') |
| 288 | self.assertEqual(self.yearstr(99), '1999') |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 289 | |
| 290 | def test_invalid(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 291 | self.assertRaises(ValueError, self.yearstr, 999) |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 292 | self.assertRaises(ValueError, self.yearstr, 100) |
| 293 | self.assertRaises(ValueError, self.yearstr, -1) |
| 294 | |
| 295 | class TestAccept2YearBool(TestAccept2Year): |
| 296 | accept2dyear = True |
| 297 | |
| 298 | class TestDontAccept2Year(TestAccept2Year): |
| 299 | accept2dyear = 0 |
| 300 | def test_2dyear(self): |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame^] | 301 | self.assertEqual(self.yearstr(0), '0') |
| 302 | self.assertEqual(self.yearstr(69), '69') |
| 303 | self.assertEqual(self.yearstr(68), '68') |
| 304 | self.assertEqual(self.yearstr(99), '99') |
| 305 | self.assertEqual(self.yearstr(999), '999') |
| 306 | self.assertEqual(self.yearstr(9999), '9999') |
| 307 | |
| 308 | def test_invalid(self): |
| 309 | pass |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 310 | |
| 311 | class TestDontAccept2YearBool(TestDontAccept2Year): |
| 312 | accept2dyear = False |
| 313 | |
| 314 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 315 | def test_main(): |
Alexander Belopolsky | a686725 | 2011-01-05 23:00:47 +0000 | [diff] [blame] | 316 | support.run_unittest(TimeTestCase, TestLocale, |
| 317 | TestAccept2Year, TestAccept2YearBool, |
| 318 | TestDontAccept2Year, TestDontAccept2YearBool) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 319 | |
| 320 | if __name__ == "__main__": |
| 321 | test_main() |