Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | from test import test_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 |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | b0c2232 | 1996-12-06 23:30:07 +0000 | [diff] [blame] | 5 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 6 | class TimeTestCase(unittest.TestCase): |
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 | def setUp(self): |
| 9 | self.t = time.time() |
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 test_data_attributes(self): |
| 12 | time.altzone |
| 13 | time.daylight |
| 14 | time.timezone |
| 15 | time.tzname |
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 test_clock(self): |
| 18 | time.clock() |
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_conversions(self): |
| 21 | self.assert_(time.ctime(self.t) |
| 22 | == time.asctime(time.localtime(self.t))) |
| 23 | self.assert_(long(time.mktime(time.localtime(self.t))) |
| 24 | == long(self.t)) |
| 25 | |
| 26 | def test_sleep(self): |
| 27 | time.sleep(1.2) |
| 28 | |
| 29 | def test_strftime(self): |
| 30 | tt = time.gmtime(self.t) |
| 31 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 32 | 'j', 'm', 'M', 'p', 'S', |
| 33 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 34 | format = ' %' + directive |
| 35 | try: |
| 36 | time.strftime(format, tt) |
| 37 | except ValueError: |
| 38 | self.fail('conversion specifier: %r failed.' % format) |
| 39 | |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 40 | def test_strftime_bounds_checking(self): |
| 41 | # Make sure that strftime() checks the bounds of the various parts |
| 42 | #of the time tuple. |
| 43 | |
| 44 | # Check year |
| 45 | self.assertRaises(ValueError, time.strftime, '', |
| 46 | (1899, 1, 1, 0, 0, 0, 0, 1, -1)) |
| 47 | if time.accept2dyear: |
| 48 | self.assertRaises(ValueError, time.strftime, '', |
| 49 | (-1, 1, 1, 0, 0, 0, 0, 1, -1)) |
| 50 | self.assertRaises(ValueError, time.strftime, '', |
| 51 | (100, 1, 1, 0, 0, 0, 0, 1, -1)) |
| 52 | # Check month |
| 53 | self.assertRaises(ValueError, time.strftime, '', |
| 54 | (1900, 0, 1, 0, 0, 0, 0, 1, -1)) |
| 55 | self.assertRaises(ValueError, time.strftime, '', |
| 56 | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
| 57 | # Check day of month |
| 58 | self.assertRaises(ValueError, time.strftime, '', |
| 59 | (1900, 1, 0, 0, 0, 0, 0, 1, -1)) |
| 60 | self.assertRaises(ValueError, time.strftime, '', |
| 61 | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
| 62 | # Check hour |
| 63 | self.assertRaises(ValueError, time.strftime, '', |
| 64 | (1900, 1, 1, -1, 0, 0, 0, 1, -1)) |
| 65 | self.assertRaises(ValueError, time.strftime, '', |
| 66 | (1900, 1, 1, 24, 0, 0, 0, 1, -1)) |
| 67 | # Check minute |
| 68 | self.assertRaises(ValueError, time.strftime, '', |
| 69 | (1900, 1, 1, 0, -1, 0, 0, 1, -1)) |
| 70 | self.assertRaises(ValueError, time.strftime, '', |
| 71 | (1900, 1, 1, 0, 60, 0, 0, 1, -1)) |
| 72 | # Check second |
| 73 | self.assertRaises(ValueError, time.strftime, '', |
| 74 | (1900, 1, 1, 0, 0, -1, 0, 1, -1)) |
| 75 | # C99 only requires allowing for one leap second, but Python's docs say |
| 76 | # allow two leap seconds (0..61) |
| 77 | self.assertRaises(ValueError, time.strftime, '', |
| 78 | (1900, 1, 1, 0, 0, 62, 0, 1, -1)) |
| 79 | # No check for upper-bound day of week; |
| 80 | # value forced into range by a ``% 7`` calculation. |
| 81 | # Start check at -2 since gettmarg() increments value before taking |
| 82 | # modulo. |
| 83 | self.assertRaises(ValueError, time.strftime, '', |
| 84 | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
| 85 | # Check day of the year |
| 86 | self.assertRaises(ValueError, time.strftime, '', |
| 87 | (1900, 1, 1, 0, 0, 0, 0, 0, -1)) |
| 88 | self.assertRaises(ValueError, time.strftime, '', |
| 89 | (1900, 1, 1, 0, 0, 0, 0, 367, -1)) |
| 90 | # Check daylight savings flag |
| 91 | self.assertRaises(ValueError, time.strftime, '', |
| 92 | (1900, 1, 1, 0, 0, 0, 0, 1, -2)) |
| 93 | self.assertRaises(ValueError, time.strftime, '', |
| 94 | (1900, 1, 1, 0, 0, 0, 0, 1, 2)) |
| 95 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 96 | def test_strptime(self): |
| 97 | tt = time.gmtime(self.t) |
| 98 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 99 | 'j', 'm', 'M', 'p', 'S', |
| 100 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 101 | format = ' %' + directive |
| 102 | try: |
| 103 | time.strptime(time.strftime(format, tt), format) |
| 104 | except ValueError: |
| 105 | self.fail('conversion specifier: %r failed.' % format) |
| 106 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 107 | def test_asctime(self): |
| 108 | time.asctime(time.gmtime(self.t)) |
| 109 | self.assertRaises(TypeError, time.asctime, 0) |
| 110 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 111 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame] | 112 | if not hasattr(time, "tzset"): |
| 113 | return # Can't test this; don't want the test suite to fail |
| 114 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 115 | from os import environ |
| 116 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 117 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 118 | # hemisphere. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 119 | xmas2002 = 1040774400.0 |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 120 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 121 | # These formats are correct for 2002, and possibly future years |
| 122 | # This format is the 'standard' as documented at: |
| 123 | # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html |
| 124 | # They are also documented in the tzset(3) man page on most Unix |
| 125 | # systems. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 126 | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 127 | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
| 128 | utc='UTC+0' |
| 129 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 130 | org_TZ = environ.get('TZ',None) |
| 131 | try: |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 132 | # Make sure we can switch to UTC time and results are correct |
| 133 | # Note that unknown timezones default to UTC. |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 134 | # Note that altzone is undefined in UTC, as there is no DST |
| 135 | environ['TZ'] = eastern |
| 136 | time.tzset() |
| 137 | environ['TZ'] = utc |
| 138 | time.tzset() |
| 139 | self.failUnlessEqual( |
| 140 | time.gmtime(xmas2002), time.localtime(xmas2002) |
| 141 | ) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 142 | self.failUnlessEqual(time.daylight, 0) |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 143 | self.failUnlessEqual(time.timezone, 0) |
| 144 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 145 | |
| 146 | # Make sure we can switch to US/Eastern |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 147 | environ['TZ'] = eastern |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 148 | time.tzset() |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 149 | self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 150 | self.failUnlessEqual(time.tzname, ('EST', 'EDT')) |
| 151 | self.failUnlessEqual(len(time.tzname), 2) |
| 152 | self.failUnlessEqual(time.daylight, 1) |
| 153 | self.failUnlessEqual(time.timezone, 18000) |
| 154 | self.failUnlessEqual(time.altzone, 14400) |
| 155 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0) |
| 156 | self.failUnlessEqual(len(time.tzname), 2) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 157 | |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 158 | # Now go to the southern hemisphere. |
| 159 | environ['TZ'] = victoria |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 160 | time.tzset() |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 161 | self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
| 162 | self.failUnless(time.tzname[0] == 'AEST', str(time.tzname[0])) |
| 163 | self.failUnless(time.tzname[1] == 'AEDT', str(time.tzname[1])) |
| 164 | self.failUnlessEqual(len(time.tzname), 2) |
| 165 | self.failUnlessEqual(time.daylight, 1) |
| 166 | self.failUnlessEqual(time.timezone, -36000) |
| 167 | self.failUnlessEqual(time.altzone, -39600) |
| 168 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 1) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 170 | finally: |
| 171 | # Repair TZ environment variable in case any other tests |
| 172 | # rely on it. |
| 173 | if org_TZ is not None: |
| 174 | environ['TZ'] = org_TZ |
| 175 | elif environ.has_key('TZ'): |
| 176 | del environ['TZ'] |
Neal Norwitz | 7f2588c | 2003-04-11 15:35:53 +0000 | [diff] [blame] | 177 | time.tzset() |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 178 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame^] | 179 | def test_insane_timestamps(self): |
| 180 | # It's possible that some platform maps time_t to double, |
| 181 | # and that this test will fail there. This test should |
| 182 | # exempt such platforms (provided they return reasonable |
| 183 | # results!). |
| 184 | for func in time.ctime, time.gmtime, time.localtime: |
| 185 | for unreasonable in -1e200, 1e200: |
| 186 | self.assertRaises(ValueError, func, unreasonable) |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 187 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 188 | def test_main(): |
| 189 | test_support.run_unittest(TimeTestCase) |
| 190 | |
| 191 | |
| 192 | if __name__ == "__main__": |
| 193 | test_main() |