Skip to content

Commit d184e7b

Browse files
committed
Add support for parsing seconds
1 parent 7c53307 commit d184e7b

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Lib/_strptime.py

Lines changed: 13 additions & 12 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|Z)",
213+
'z': r"(?P<z>[+-]\d\d:?[0-5]\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'),
@@ -365,7 +365,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
365365
month = day = 1
366366
hour = minute = second = fraction = 0
367367
tz = -1
368-
tzoffset = None
368+
gmtoff = None
369369
# Default to -1 to signify that values not known; not critical to have,
370370
# though
371371
iso_week = week_of_year = None
@@ -456,15 +456,20 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
456456
elif group_key == 'z':
457457
z = found_dict['z']
458458
if z == 'Z':
459-
tzoffset = 0
459+
gmtoff = 0
460460
else:
461461
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])
462+
z = z[:3] + z[4:]
463+
if len(z) > 5:
464+
if z[5] != ':':
465+
raise ValueError(f"Unconsistent use of :")
466+
z = z[:5] + z[6:]
467+
hours = int(z[1:3])
468+
minutes = int(z[3:5])
469+
seconds = int(z[5:7] or 0)
470+
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
466471
if z.startswith("-"):
467-
tzoffset = -tzoffset
472+
gmtoff = -gmtoff
468473
elif group_key == 'Z':
469474
# Since -1 is default value only need to worry about setting tz if
470475
# it can be something other than -1.
@@ -542,10 +547,6 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
542547
weekday = datetime_date(year, month, day).weekday()
543548
# Add timezone info
544549
tzname = found_dict.get("Z")
545-
if tzoffset is not None:
546-
gmtoff = tzoffset * 60
547-
else:
548-
gmtoff = None
549550

550551
if leap_year_fix:
551552
# the caller didn't supply a year but asked for Feb 29th. We couldn't

Lib/test/test_strptime.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,19 @@ def test_julian(self):
320320
def test_offset(self):
321321
one_hour = 60 * 60
322322
half_hour = 30 * 60
323+
half_minute = 30
323324
(*_, offset), _ = _strptime._strptime("+0130", "%z")
324325
self.assertEqual(offset, one_hour + half_hour)
325326
(*_, offset), _ = _strptime._strptime("-0100", "%z")
326327
self.assertEqual(offset, -one_hour)
328+
(*_, offset), _ = _strptime._strptime("-013030", "%z")
329+
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
327330
(*_, offset), _ = _strptime._strptime("+01:00", "%z")
328331
self.assertEqual(offset, one_hour)
329332
(*_, offset), _ = _strptime._strptime("-01:30", "%z")
330333
self.assertEqual(offset, -(one_hour + half_hour))
334+
(*_, offset), _ = _strptime._strptime("-01:30:30", "%z")
335+
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
331336
(*_, offset), _ = _strptime._strptime("Z", "%z")
332337
self.assertEqual(offset, 0)
333338

0 commit comments

Comments
 (0)