Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 172d9ef

Browse files
committed
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.
1 parent c2409b4 commit 172d9ef

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

Lib/_strptime.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,13 @@ def __calc_date_time(self):
283283
def __calc_timezone(self):
284284
# Set self.__timezone by using time.tzname.
285285
#
286-
# Empty string used for matching when timezone is not used/needed such
287-
# as with UTC.
288-
self.__timezone = self.__pad(time.tzname, 0)
286+
# Empty string used for matching when timezone is not used/needed.
287+
time_zones = ["UTC", "GMT"]
288+
if time.daylight:
289+
time_zones.extend(time.tzname)
290+
else:
291+
time_zones.append(time.tzname[0])
292+
self.__timezone = self.__pad(time_zones, 0)
289293

290294
def __calc_lang(self):
291295
# Set self.__lang by using __getlang().
@@ -490,16 +494,20 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
490494
elif group_key == 'j':
491495
julian = int(found_dict['j'])
492496
elif group_key == 'Z':
497+
# Since -1 is default value only need to worry about setting tz if
498+
# it can be something other than -1.
493499
found_zone = found_dict['Z'].lower()
494500
if locale_time.timezone[0] == locale_time.timezone[1]:
495501
pass #Deals with bad locale setup where timezone info is
496502
# the same; first found on FreeBSD 4.4.
497-
elif locale_time.timezone[0].lower() == found_zone:
503+
elif found_zone in ("utc", "gmt"):
498504
tz = 0
499-
elif locale_time.timezone[1].lower() == found_zone:
500-
tz = 1
501505
elif locale_time.timezone[2].lower() == found_zone:
502-
tz = -1
506+
tz = 0
507+
elif time.daylight:
508+
if locale_time.timezone[3].lower() == found_zone:
509+
tz = 1
510+
503511
# Cannot pre-calculate datetime_date() since can change in Julian
504512
#calculation and thus could have different value for the day of the week
505513
#calculation

Lib/test/test_strptime.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ def test_am_pm(self):
5858

5959
def test_timezone(self):
6060
# Make sure timezone is correct
61-
if time.strftime("%Z", self.time_tuple):
62-
self.compare_against_time(self.LT_ins.timezone, '%Z', 8,
63-
"Testing against timezone failed")
61+
timezone = time.strftime("%Z", self.time_tuple)
62+
if timezone:
63+
self.failUnless(timezone in self.LT_ins.timezone,
64+
"timezone %s not found in %s" %
65+
(timezone, self.LT_ins.timezone))
6466

6567
def test_date_time(self):
6668
# Check that LC_date_time, LC_date, and LC_time are correct
@@ -293,18 +295,25 @@ def test_timezone(self):
293295
# When gmtime() is used with %Z, entire result of strftime() is empty.
294296
# Check for equal timezone names deals with bad locale info when this
295297
# occurs; first found in FreeBSD 4.4.
298+
strp_output = _strptime.strptime("UTC", "%Z")
299+
self.failUnlessEqual(strp_output.tm_isdst, 0)
300+
strp_output = _strptime.strptime("GMT", "%Z")
301+
self.failUnlessEqual(strp_output.tm_isdst, 0)
302+
if time.daylight:
303+
strp_output = _strptime.strptime(time.tzname[1], "%Z")
304+
self.failUnlessEqual(strp_output[8], 1)
296305
time_tuple = time.localtime()
297306
strf_output = time.strftime("%Z") #UTC does not have a timezone
298307
strp_output = _strptime.strptime(strf_output, "%Z")
299308
locale_time = _strptime.LocaleTime()
300-
if locale_time.timezone[0] != locale_time.timezone[1]:
309+
if time.tzname[0] != time.tzname[1]:
301310
self.failUnless(strp_output[8] == time_tuple[8],
302311
"timezone check failed; '%s' -> %s != %s" %
303312
(strf_output, strp_output[8], time_tuple[8]))
304313
else:
305314
self.failUnless(strp_output[8] == -1,
306315
"LocaleTime().timezone has duplicate values but "
307-
"timzone value not set to 0")
316+
"timzone value not set to -1")
308317

309318
def test_date_time(self):
310319
# Test %c directive

0 commit comments

Comments
 (0)