Skip to content

bpo-31800: Support for colon when parsing time offsets #4015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for parsing seconds
  • Loading branch information
mariocj89 committed Oct 23, 2017
commit d184e7be0db49de19ae6fc7d093c5b0b207dc1f6
25 changes: 13 additions & 12 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __init__(self, locale_time=None):
#XXX: Does 'Y' need to worry about having less or more than
# 4 digits?
'Y': r"(?P<Y>\d\d\d\d)",
'z': r"(?P<z>[+-]\d\d:?[0-5]\d|Z)",
'z': r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d)?|Z)",
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
Expand Down Expand Up @@ -365,7 +365,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
month = day = 1
hour = minute = second = fraction = 0
tz = -1
tzoffset = None
gmtoff = None
# Default to -1 to signify that values not known; not critical to have,
# though
iso_week = week_of_year = None
Expand Down Expand Up @@ -456,15 +456,20 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
elif group_key == 'z':
z = found_dict['z']
if z == 'Z':
tzoffset = 0
gmtoff = 0
else:
if z[3] == ':':
minute_start = 4
else:
minute_start = 3
tzoffset = int(z[1:3]) * 60 + int(z[minute_start:minute_start+2])
z = z[:3] + z[4:]
if len(z) > 5:
if z[5] != ':':
raise ValueError(f"Unconsistent use of :")
z = z[:5] + z[6:]
hours = int(z[1:3])
minutes = int(z[3:5])
seconds = int(z[5:7] or 0)
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
if z.startswith("-"):
tzoffset = -tzoffset
gmtoff = -gmtoff
elif group_key == 'Z':
# Since -1 is default value only need to worry about setting tz if
# it can be something other than -1.
Expand Down Expand Up @@ -542,10 +547,6 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
weekday = datetime_date(year, month, day).weekday()
# Add timezone info
tzname = found_dict.get("Z")
if tzoffset is not None:
gmtoff = tzoffset * 60
else:
gmtoff = None

if leap_year_fix:
# the caller didn't supply a year but asked for Feb 29th. We couldn't
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,19 @@ def test_julian(self):
def test_offset(self):
one_hour = 60 * 60
half_hour = 30 * 60
half_minute = 30
(*_, offset), _ = _strptime._strptime("+0130", "%z")
self.assertEqual(offset, one_hour + half_hour)
(*_, offset), _ = _strptime._strptime("-0100", "%z")
self.assertEqual(offset, -one_hour)
(*_, offset), _ = _strptime._strptime("-013030", "%z")
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
(*_, offset), _ = _strptime._strptime("+01:00", "%z")
self.assertEqual(offset, one_hour)
(*_, offset), _ = _strptime._strptime("-01:30", "%z")
self.assertEqual(offset, -(one_hour + half_hour))
(*_, offset), _ = _strptime._strptime("-01:30:30", "%z")
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
(*_, offset), _ = _strptime._strptime("Z", "%z")
self.assertEqual(offset, 0)

Expand Down