From 7beaeeed2b99ec120486ee53e1eb7f1fe2515b9c Mon Sep 17 00:00:00 2001 From: Richard Eames Date: Thu, 12 Jun 2014 10:58:50 -0600 Subject: [PATCH] Fixed #22814 -- Allowed ISO-8601 [+-]hh timezone format in parse_datetime --- django/utils/dateparse.py | 5 +++-- tests/utils_tests/test_dateparse.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py index f2c079023f..cb45a2760b 100644 --- a/django/utils/dateparse.py +++ b/django/utils/dateparse.py @@ -24,7 +24,7 @@ datetime_re = re.compile( r'(?P\d{4})-(?P\d{1,2})-(?P\d{1,2})' r'[T ](?P\d{1,2}):(?P\d{1,2})' r'(?::(?P\d{1,2})(?:\.(?P\d{1,6})\d{0,6})?)?' - r'(?PZ|[+-]\d{2}:?\d{2})?$' + r'(?PZ|[+-]\d{2}(?::?\d{2})?)?$' ) @@ -76,7 +76,8 @@ def parse_datetime(value): if tzinfo == 'Z': tzinfo = utc 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] == '-': offset = -offset tzinfo = get_fixed_timezone(offset) diff --git a/tests/utils_tests/test_dateparse.py b/tests/utils_tests/test_dateparse.py index 61afff9a9c..cdf91c039e 100644 --- a/tests/utils_tests/test_dateparse.py +++ b/tests/utils_tests/test_dateparse.py @@ -39,6 +39,10 @@ class DateParseTests(unittest.TestCase): datetime(2012, 4, 9, 4, 8, 16, 0, get_fixed_timezone(-200))) self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02:30'), 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 self.assertEqual(parse_datetime('20120423091500'), None) self.assertRaises(ValueError, parse_datetime, '2012-04-56T09:15:90')