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.
This commit is contained in:
Brett Cannon 2003-05-11 06:23:36 +00:00
parent c2409b4f5a
commit 172d9ef47e
2 changed files with 29 additions and 12 deletions

View File

@ -283,9 +283,13 @@ class LocaleTime(object):
def __calc_timezone(self): def __calc_timezone(self):
# Set self.__timezone by using time.tzname. # Set self.__timezone by using time.tzname.
# #
# Empty string used for matching when timezone is not used/needed such # Empty string used for matching when timezone is not used/needed.
# as with UTC. time_zones = ["UTC", "GMT"]
self.__timezone = self.__pad(time.tzname, 0) 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): def __calc_lang(self):
# Set self.__lang by using __getlang(). # 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': elif group_key == 'j':
julian = int(found_dict['j']) julian = int(found_dict['j'])
elif group_key == 'Z': 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() found_zone = found_dict['Z'].lower()
if locale_time.timezone[0] == locale_time.timezone[1]: if locale_time.timezone[0] == locale_time.timezone[1]:
pass #Deals with bad locale setup where timezone info is pass #Deals with bad locale setup where timezone info is
# the same; first found on FreeBSD 4.4. # the same; first found on FreeBSD 4.4.
elif locale_time.timezone[0].lower() == found_zone: elif found_zone in ("utc", "gmt"):
tz = 0 tz = 0
elif locale_time.timezone[1].lower() == found_zone:
tz = 1
elif locale_time.timezone[2].lower() == found_zone: 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 # Cannot pre-calculate datetime_date() since can change in Julian
#calculation and thus could have different value for the day of the week #calculation and thus could have different value for the day of the week
#calculation #calculation

View File

@ -58,9 +58,11 @@ class LocaleTime_Tests(unittest.TestCase):
def test_timezone(self): def test_timezone(self):
# Make sure timezone is correct # Make sure timezone is correct
if time.strftime("%Z", self.time_tuple): timezone = time.strftime("%Z", self.time_tuple)
self.compare_against_time(self.LT_ins.timezone, '%Z', 8, if timezone:
"Testing against timezone failed") self.failUnless(timezone in self.LT_ins.timezone,
"timezone %s not found in %s" %
(timezone, self.LT_ins.timezone))
def test_date_time(self): def test_date_time(self):
# Check that LC_date_time, LC_date, and LC_time are correct # 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. # When gmtime() is used with %Z, entire result of strftime() is empty.
# Check for equal timezone names deals with bad locale info when this # Check for equal timezone names deals with bad locale info when this
# occurs; first found in FreeBSD 4.4. # 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() time_tuple = time.localtime()
strf_output = time.strftime("%Z") #UTC does not have a timezone strf_output = time.strftime("%Z") #UTC does not have a timezone
strp_output = _strptime.strptime(strf_output, "%Z") strp_output = _strptime.strptime(strf_output, "%Z")
locale_time = _strptime.LocaleTime() 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], self.failUnless(strp_output[8] == time_tuple[8],
"timezone check failed; '%s' -> %s != %s" % "timezone check failed; '%s' -> %s != %s" %
(strf_output, strp_output[8], time_tuple[8])) (strf_output, strp_output[8], time_tuple[8]))
else: else:
self.failUnless(strp_output[8] == -1, self.failUnless(strp_output[8] == -1,
"LocaleTime().timezone has duplicate values but " "LocaleTime().timezone has duplicate values but "
"timzone value not set to 0") "timzone value not set to -1")
def test_date_time(self): def test_date_time(self):
# Test %c directive # Test %c directive