Fixed #18728 -- Made colon optional in tzinfo
Made two-digit hours and minutes mandatory in tzinfo (the code used to crash if a one-digit representation was provided). Added standalone tests for django.utils.dateparse.
This commit is contained in:
parent
a43ecc0444
commit
2f59e94a41
|
@ -15,16 +15,16 @@ date_re = re.compile(
|
|||
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$'
|
||||
)
|
||||
|
||||
time_re = re.compile(
|
||||
r'(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
|
||||
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
|
||||
)
|
||||
|
||||
datetime_re = re.compile(
|
||||
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'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
|
||||
r'(?P<tzinfo>Z|[+-]\d{1,2}:\d{1,2})?$'
|
||||
)
|
||||
|
||||
time_re = re.compile(
|
||||
r'(?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<tzinfo>Z|[+-]\d{2}:?\d{2})?$'
|
||||
)
|
||||
|
||||
def parse_date(value):
|
||||
|
@ -73,7 +73,7 @@ def parse_datetime(value):
|
|||
if tzinfo == 'Z':
|
||||
tzinfo = utc
|
||||
elif tzinfo is not None:
|
||||
offset = 60 * int(tzinfo[1:3]) + int(tzinfo[4:6])
|
||||
offset = 60 * int(tzinfo[1:3]) + int(tzinfo[-2:])
|
||||
if tzinfo[0] == '-':
|
||||
offset = -offset
|
||||
tzinfo = FixedOffset(offset)
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import date, time, datetime
|
||||
|
||||
from django.utils.dateparse import parse_date, parse_time, parse_datetime
|
||||
from django.utils import unittest
|
||||
from django.utils.tzinfo import FixedOffset
|
||||
|
||||
|
||||
class DateParseTests(unittest.TestCase):
|
||||
|
||||
def test_parse_date(self):
|
||||
# Valid inputs
|
||||
self.assertEqual(parse_date('2012-04-23'), date(2012, 4, 23))
|
||||
self.assertEqual(parse_date('2012-4-9'), date(2012, 4, 9))
|
||||
# Invalid inputs
|
||||
self.assertEqual(parse_date('20120423'), None)
|
||||
self.assertRaises(ValueError, parse_date, '2012-04-56')
|
||||
|
||||
def test_parse_time(self):
|
||||
# Valid inputs
|
||||
self.assertEqual(parse_time('09:15:00'), time(9, 15))
|
||||
self.assertEqual(parse_time('10:10'), time(10, 10))
|
||||
self.assertEqual(parse_time('10:20:30.400'), time(10, 20, 30, 400000))
|
||||
self.assertEqual(parse_time('4:8:16'), time(4, 8, 16))
|
||||
# Invalid inputs
|
||||
self.assertEqual(parse_time('091500'), None)
|
||||
self.assertRaises(ValueError, parse_time, '09:15:90')
|
||||
|
||||
def test_parse_datetime(self):
|
||||
# Valid inputs
|
||||
self.assertEqual(parse_datetime('2012-04-23T09:15:00'),
|
||||
datetime(2012, 4, 23, 9, 15))
|
||||
self.assertEqual(parse_datetime('2012-4-9 4:8:16'),
|
||||
datetime(2012, 4, 9, 4, 8, 16))
|
||||
self.assertEqual(parse_datetime('2012-04-23T09:15:00Z'),
|
||||
datetime(2012, 4, 23, 9, 15, 0, 0, FixedOffset(0)))
|
||||
self.assertEqual(parse_datetime('2012-4-9 4:8:16-0320'),
|
||||
datetime(2012, 4, 9, 4, 8, 16, 0, FixedOffset(-200)))
|
||||
self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02:30'),
|
||||
datetime(2012, 4, 23, 10, 20, 30, 400000, FixedOffset(150)))
|
||||
# Invalid inputs
|
||||
self.assertEqual(parse_datetime('20120423091500'), None)
|
||||
self.assertRaises(ValueError, parse_datetime, '2012-04-56T09:15:90')
|
|
@ -26,3 +26,4 @@ from .timezone import TimezoneTests
|
|||
from .crypto import TestUtilsCryptoPBKDF2
|
||||
from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar
|
||||
from .regex_helper import NormalizeTests
|
||||
from .dateparse import DateParseTests
|
||||
|
|
Loading…
Reference in New Issue