From 172d9ef47eafe6982c2c885353af761256e7b409 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 11 May 2003 06:23:36 +0000 Subject: [PATCH] Beefed up timezone support. UTC and GMT are now always recognized timezones with values of 0. Also now check time.daylight to see if time.tzname[1] should be used in timezone checking. --- Lib/_strptime.py | 22 +++++++++++++++------- Lib/test/test_strptime.py | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Lib/_strptime.py b/Lib/_strptime.py index 4b7a7dd6151..a08a426916a 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -283,9 +283,13 @@ class LocaleTime(object): def __calc_timezone(self): # Set self.__timezone by using time.tzname. # - # Empty string used for matching when timezone is not used/needed such - # as with UTC. - self.__timezone = self.__pad(time.tzname, 0) + # Empty string used for matching when timezone is not used/needed. + time_zones = ["UTC", "GMT"] + if time.daylight: + time_zones.extend(time.tzname) + else: + time_zones.append(time.tzname[0]) + self.__timezone = self.__pad(time_zones, 0) def __calc_lang(self): # Set self.__lang by using __getlang(). @@ -490,16 +494,20 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"): elif group_key == 'j': julian = int(found_dict['j']) elif group_key == 'Z': + # Since -1 is default value only need to worry about setting tz if + # it can be something other than -1. found_zone = found_dict['Z'].lower() if locale_time.timezone[0] == locale_time.timezone[1]: pass #Deals with bad locale setup where timezone info is # the same; first found on FreeBSD 4.4. - elif locale_time.timezone[0].lower() == found_zone: + elif found_zone in ("utc", "gmt"): tz = 0 - elif locale_time.timezone[1].lower() == found_zone: - tz = 1 elif locale_time.timezone[2].lower() == found_zone: - tz = -1 + tz = 0 + elif time.daylight: + if locale_time.timezone[3].lower() == found_zone: + tz = 1 + # Cannot pre-calculate datetime_date() since can change in Julian #calculation and thus could have different value for the day of the week #calculation diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py index 140b226a632..c9d705b3ccd 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -58,9 +58,11 @@ class LocaleTime_Tests(unittest.TestCase): def test_timezone(self): # Make sure timezone is correct - if time.strftime("%Z", self.time_tuple): - self.compare_against_time(self.LT_ins.timezone, '%Z', 8, - "Testing against timezone failed") + timezone = time.strftime("%Z", self.time_tuple) + if timezone: + self.failUnless(timezone in self.LT_ins.timezone, + "timezone %s not found in %s" % + (timezone, self.LT_ins.timezone)) def test_date_time(self): # Check that LC_date_time, LC_date, and LC_time are correct @@ -293,18 +295,25 @@ class StrptimeTests(unittest.TestCase): # When gmtime() is used with %Z, entire result of strftime() is empty. # Check for equal timezone names deals with bad locale info when this # occurs; first found in FreeBSD 4.4. + strp_output = _strptime.strptime("UTC", "%Z") + self.failUnlessEqual(strp_output.tm_isdst, 0) + strp_output = _strptime.strptime("GMT", "%Z") + self.failUnlessEqual(strp_output.tm_isdst, 0) + if time.daylight: + strp_output = _strptime.strptime(time.tzname[1], "%Z") + self.failUnlessEqual(strp_output[8], 1) time_tuple = time.localtime() strf_output = time.strftime("%Z") #UTC does not have a timezone strp_output = _strptime.strptime(strf_output, "%Z") locale_time = _strptime.LocaleTime() - if locale_time.timezone[0] != locale_time.timezone[1]: + if time.tzname[0] != time.tzname[1]: self.failUnless(strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8])) else: self.failUnless(strp_output[8] == -1, "LocaleTime().timezone has duplicate values but " - "timzone value not set to 0") + "timzone value not set to -1") def test_date_time(self): # Test %c directive