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 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 40 | def test_strptime(self): |
| 41 | tt = time.gmtime(self.t) |
| 42 | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
| 43 | 'j', 'm', 'M', 'p', 'S', |
| 44 | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
| 45 | format = ' %' + directive |
| 46 | try: |
| 47 | time.strptime(time.strftime(format, tt), format) |
| 48 | except ValueError: |
| 49 | self.fail('conversion specifier: %r failed.' % format) |
| 50 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 51 | def test_asctime(self): |
| 52 | time.asctime(time.gmtime(self.t)) |
| 53 | self.assertRaises(TypeError, time.asctime, 0) |
| 54 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 55 | def test_tzset(self): |
Guido van Rossum | d2b738e | 2003-03-15 12:01:52 +0000 | [diff] [blame^] | 56 | if not hasattr(time, "tzset"): |
| 57 | return # Can't test this; don't want the test suite to fail |
| 58 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 59 | from os import environ |
| 60 | |
| 61 | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
| 62 | # hemisphere. |
| 63 | xmas2002 = 1040774400.0 |
| 64 | |
| 65 | org_TZ = environ.get('TZ',None) |
| 66 | try: |
| 67 | |
| 68 | # Make sure we can switch to UTC time and results are correct |
| 69 | # Note that unknown timezones default to UTC. |
| 70 | for tz in ('UTC','GMT','Luna/Tycho'): |
| 71 | environ['TZ'] = 'US/Eastern' |
| 72 | time.tzset() |
| 73 | environ['TZ'] = tz |
| 74 | time.tzset() |
| 75 | self.failUnlessEqual( |
| 76 | time.gmtime(xmas2002),time.localtime(xmas2002) |
| 77 | ) |
| 78 | self.failUnlessEqual(time.timezone,time.altzone) |
| 79 | self.failUnlessEqual(time.daylight,0) |
| 80 | self.failUnlessEqual(time.timezone,0) |
| 81 | self.failUnlessEqual(time.altzone,0) |
| 82 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,0) |
| 83 | |
| 84 | # Make sure we can switch to US/Eastern |
| 85 | environ['TZ'] = 'US/Eastern' |
| 86 | time.tzset() |
| 87 | self.failIfEqual(time.gmtime(xmas2002),time.localtime(xmas2002)) |
| 88 | self.failUnlessEqual(time.tzname,('EST','EDT')) |
| 89 | self.failUnlessEqual(len(time.tzname),2) |
| 90 | self.failUnlessEqual(time.daylight,1) |
| 91 | self.failUnlessEqual(time.timezone,18000) |
| 92 | self.failUnlessEqual(time.altzone,14400) |
| 93 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,0) |
| 94 | self.failUnlessEqual(len(time.tzname),2) |
| 95 | |
| 96 | # Now go to the southern hemisphere. We want somewhere all OS's |
| 97 | # know about that has DST. |
| 98 | environ['TZ'] = 'Australia/Melbourne' |
| 99 | time.tzset() |
| 100 | self.failIfEqual(time.gmtime(xmas2002),time.localtime(xmas2002)) |
| 101 | self.failUnless(time.tzname[0] in ('EST','AEST')) |
| 102 | self.failUnless(time.tzname[1] in ('EST','EDT','AEDT')) |
| 103 | self.failUnlessEqual(len(time.tzname),2) |
| 104 | self.failUnlessEqual(time.daylight,1) |
| 105 | self.failUnlessEqual(time.timezone,-36000) |
| 106 | self.failUnlessEqual(time.altzone,-39600) |
| 107 | self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,1) |
| 108 | |
| 109 | # Get some times from a timezone that isn't wallclock timezone |
| 110 | del environ['TZ'] |
| 111 | time.tzset() |
| 112 | if time.timezone == 0: |
| 113 | environ['TZ'] = 'US/Eastern' |
| 114 | else: |
| 115 | environ['TZ'] = 'UTC' |
| 116 | time.tzset() |
| 117 | nonlocal = time.localtime(xmas2002) |
| 118 | |
| 119 | # Then the same time in wallclock timezone |
| 120 | del environ['TZ'] |
| 121 | time.tzset() |
| 122 | local = time.localtime(xmas2002) |
| 123 | |
| 124 | # And make sure they arn't the same |
| 125 | self.failIfEqual(local,nonlocal) |
| 126 | |
| 127 | # Do some basic sanity checking after wallclock time set |
| 128 | self.failUnlessEqual(len(time.tzname),2) |
| 129 | time.daylight |
| 130 | time.timezone |
| 131 | time.altzone |
| 132 | finally: |
| 133 | # Repair TZ environment variable in case any other tests |
| 134 | # rely on it. |
| 135 | if org_TZ is not None: |
| 136 | environ['TZ'] = org_TZ |
| 137 | elif environ.has_key('TZ'): |
| 138 | del environ['TZ'] |
| 139 | |
Fred Drake | bc56198 | 2001-05-22 17:02:02 +0000 | [diff] [blame] | 140 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 141 | def test_main(): |
| 142 | test_support.run_unittest(TimeTestCase) |
| 143 | |
| 144 | |
| 145 | if __name__ == "__main__": |
| 146 | test_main() |