Skip to content

Commit e0e3873

Browse files
committed
Add support for Z on %z directive
1 parent af68297 commit e0e3873

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

Lib/_strptime.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def __init__(self, locale_time=None):
210210
#XXX: Does 'Y' need to worry about having less or more than
211211
# 4 digits?
212212
'Y': r"(?P<Y>\d\d\d\d)",
213-
'z': r"(?P<z>[+-]\d\d:?[0-5]\d)",
213+
'z': r"(?P<z>[+-]\d\d:?[0-5]\d|Z)",
214214
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
215215
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
216216
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
@@ -455,13 +455,16 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
455455
iso_week = int(found_dict['V'])
456456
elif group_key == 'z':
457457
z = found_dict['z']
458-
if z[3] == ':':
459-
minute_start = 4
458+
if z == 'Z':
459+
tzoffset = 0
460460
else:
461-
minute_start = 3
462-
tzoffset = int(z[1:3]) * 60 + int(z[minute_start:minute_start+2])
463-
if z.startswith("-"):
464-
tzoffset = -tzoffset
461+
if z[3] == ':':
462+
minute_start = 4
463+
else:
464+
minute_start = 3
465+
tzoffset = int(z[1:3]) * 60 + int(z[minute_start:minute_start+2])
466+
if z.startswith("-"):
467+
tzoffset = -tzoffset
465468
elif group_key == 'Z':
466469
# Since -1 is default value only need to worry about setting tz if
467470
# it can be something other than -1.

Lib/test/test_strptime.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,18 @@ def test_julian(self):
318318
self.helper('j', 7)
319319

320320
def test_offset(self):
321-
(*_, offset), _ = _strptime._strptime("+0100", "%z")
322-
self.assertEqual(offset, 60 * 60)
321+
one_hour = 60 * 60
322+
half_hour = 30 * 60
323+
(*_, offset), _ = _strptime._strptime("+0130", "%z")
324+
self.assertEqual(offset, one_hour + half_hour)
323325
(*_, offset), _ = _strptime._strptime("-0100", "%z")
324-
self.assertEqual(offset, -60 * 60)
326+
self.assertEqual(offset, -one_hour)
325327
(*_, offset), _ = _strptime._strptime("+01:00", "%z")
326-
self.assertEqual(offset, 60 * 60)
327-
(*_, offset), _ = _strptime._strptime("-01:00", "%z")
328-
self.assertEqual(offset, -60 * 60)
328+
self.assertEqual(offset, one_hour)
329+
(*_, offset), _ = _strptime._strptime("-01:30", "%z")
330+
self.assertEqual(offset, -(one_hour + half_hour))
331+
(*_, offset), _ = _strptime._strptime("Z", "%z")
332+
self.assertEqual(offset, 0)
329333

330334
def test_timezone(self):
331335
# Test timezone directives.

0 commit comments

Comments
 (0)