Fixed #22814 -- Allowed ISO-8601 [+-]hh timezone format in parse_datetime

This commit is contained in:
Richard Eames 2014-06-12 10:58:50 -06:00
parent bcc3d2b978
commit 7beaeeed2b
2 changed files with 7 additions and 2 deletions

View File

@ -24,7 +24,7 @@ datetime_re = re.compile(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})' r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})' r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?' r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
r'(?P<tzinfo>Z|[+-]\d{2}:?\d{2})?$' r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
) )
@ -76,7 +76,8 @@ def parse_datetime(value):
if tzinfo == 'Z': if tzinfo == 'Z':
tzinfo = utc tzinfo = utc
elif tzinfo is not None: elif tzinfo is not None:
offset = 60 * int(tzinfo[1:3]) + int(tzinfo[-2:]) offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-': if tzinfo[0] == '-':
offset = -offset offset = -offset
tzinfo = get_fixed_timezone(offset) tzinfo = get_fixed_timezone(offset)

View File

@ -39,6 +39,10 @@ class DateParseTests(unittest.TestCase):
datetime(2012, 4, 9, 4, 8, 16, 0, get_fixed_timezone(-200))) datetime(2012, 4, 9, 4, 8, 16, 0, get_fixed_timezone(-200)))
self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02:30'), self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02:30'),
datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(150))) datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(150)))
self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02'),
datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(120)))
self.assertEqual(parse_datetime('2012-04-23T10:20:30.400-02'),
datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(-120)))
# Invalid inputs # Invalid inputs
self.assertEqual(parse_datetime('20120423091500'), None) self.assertEqual(parse_datetime('20120423091500'), None)
self.assertRaises(ValueError, parse_datetime, '2012-04-56T09:15:90') self.assertRaises(ValueError, parse_datetime, '2012-04-56T09:15:90')