diff options
Diffstat (limited to 'Lib/test/test_time.py')
-rw-r--r-- | Lib/test/test_time.py | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 460c989..4459f3e 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -48,11 +48,92 @@ class TimeTestCase(unittest.TestCase): except ValueError: self.fail('conversion specifier: %r failed.' % format) - def test_asctime(self): time.asctime(time.gmtime(self.t)) self.assertRaises(TypeError, time.asctime, 0) + def test_tzset(self): + from os import environ + + # Epoch time of midnight Dec 25th 2002. Never DST in northern + # hemisphere. + xmas2002 = 1040774400.0 + + org_TZ = environ.get('TZ',None) + try: + + # Make sure we can switch to UTC time and results are correct + # Note that unknown timezones default to UTC. + for tz in ('UTC','GMT','Luna/Tycho'): + environ['TZ'] = 'US/Eastern' + time.tzset() + environ['TZ'] = tz + time.tzset() + self.failUnlessEqual( + time.gmtime(xmas2002),time.localtime(xmas2002) + ) + self.failUnlessEqual(time.timezone,time.altzone) + self.failUnlessEqual(time.daylight,0) + self.failUnlessEqual(time.timezone,0) + self.failUnlessEqual(time.altzone,0) + self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,0) + + # Make sure we can switch to US/Eastern + environ['TZ'] = 'US/Eastern' + time.tzset() + self.failIfEqual(time.gmtime(xmas2002),time.localtime(xmas2002)) + self.failUnlessEqual(time.tzname,('EST','EDT')) + self.failUnlessEqual(len(time.tzname),2) + self.failUnlessEqual(time.daylight,1) + self.failUnlessEqual(time.timezone,18000) + self.failUnlessEqual(time.altzone,14400) + self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,0) + self.failUnlessEqual(len(time.tzname),2) + + # Now go to the southern hemisphere. We want somewhere all OS's + # know about that has DST. + environ['TZ'] = 'Australia/Melbourne' + time.tzset() + self.failIfEqual(time.gmtime(xmas2002),time.localtime(xmas2002)) + self.failUnless(time.tzname[0] in ('EST','AEST')) + self.failUnless(time.tzname[1] in ('EST','EDT','AEDT')) + self.failUnlessEqual(len(time.tzname),2) + self.failUnlessEqual(time.daylight,1) + self.failUnlessEqual(time.timezone,-36000) + self.failUnlessEqual(time.altzone,-39600) + self.failUnlessEqual(time.localtime(xmas2002).tm_isdst,1) + + # Get some times from a timezone that isn't wallclock timezone + del environ['TZ'] + time.tzset() + if time.timezone == 0: + environ['TZ'] = 'US/Eastern' + else: + environ['TZ'] = 'UTC' + time.tzset() + nonlocal = time.localtime(xmas2002) + + # Then the same time in wallclock timezone + del environ['TZ'] + time.tzset() + local = time.localtime(xmas2002) + + # And make sure they arn't the same + self.failIfEqual(local,nonlocal) + + # Do some basic sanity checking after wallclock time set + self.failUnlessEqual(len(time.tzname),2) + time.daylight + time.timezone + time.altzone + finally: + # Repair TZ environment variable in case any other tests + # rely on it. + if org_TZ is not None: + environ['TZ'] = org_TZ + elif environ.has_key('TZ'): + del environ['TZ'] + def test_main(): test_support.run_unittest(TimeTestCase) |