Negligible spacing cleanup in utils/dateparse.py
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17219 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
286c5c811b
commit
637a70fa7b
|
@ -1,58 +1,51 @@
|
||||||
"""Functions to parse datetime objects."""
|
"""Functions to parse datetime objects."""
|
||||||
|
|
||||||
# We're using regular expressions rather than time.strptime because:
|
# We're using regular expressions rather than time.strptime because:
|
||||||
# - they provide both validation and parsing,
|
# - They provide both validation and parsing.
|
||||||
# - they're more flexible for datetimes,
|
# - They're more flexible for datetimes.
|
||||||
# - the date/datetime/time constructors produce friendlier error messages.
|
# - The date/datetime/time constructors produce friendlier error messages.
|
||||||
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.utils.timezone import utc
|
from django.utils.timezone import utc
|
||||||
from django.utils.tzinfo import FixedOffset
|
from django.utils.tzinfo import FixedOffset
|
||||||
|
|
||||||
|
|
||||||
date_re = re.compile(
|
date_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})$'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
datetime_re = re.compile(
|
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{1,2}:\d{1,2})?$'
|
r'(?P<tzinfo>Z|[+-]\d{1,2}:\d{1,2})?$'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
time_re = re.compile(
|
time_re = re.compile(
|
||||||
r'(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
|
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<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def parse_date(value):
|
def parse_date(value):
|
||||||
"""Parse a string and return a datetime.date.
|
"""Parses a string and return a datetime.date.
|
||||||
|
|
||||||
Raise ValueError if the input is well formatted but not a valid date.
|
Raises ValueError if the input is well formatted but not a valid date.
|
||||||
Return None if the input isn't well formatted.
|
Returns None if the input isn't well formatted.
|
||||||
"""
|
"""
|
||||||
match = date_re.match(value)
|
match = date_re.match(value)
|
||||||
if match:
|
if match:
|
||||||
kw = dict((k, int(v)) for k, v in match.groupdict().iteritems())
|
kw = dict((k, int(v)) for k, v in match.groupdict().iteritems())
|
||||||
return datetime.date(**kw)
|
return datetime.date(**kw)
|
||||||
|
|
||||||
|
|
||||||
def parse_time(value):
|
def parse_time(value):
|
||||||
"""Parse a string and return a datetime.time.
|
"""Parses a string and return a datetime.time.
|
||||||
|
|
||||||
This function doesn't support time zone offsets.
|
This function doesn't support time zone offsets.
|
||||||
|
|
||||||
Sub-microsecond precision is accepted, but ignored.
|
Sub-microsecond precision is accepted, but ignored.
|
||||||
|
|
||||||
Raise ValueError if the input is well formatted but not a valid time.
|
Raises ValueError if the input is well formatted but not a valid time.
|
||||||
Return None if the input isn't well formatted, in particular if it
|
Returns None if the input isn't well formatted, in particular if it
|
||||||
contains an offset.
|
contains an offset.
|
||||||
"""
|
"""
|
||||||
match = time_re.match(value)
|
match = time_re.match(value)
|
||||||
|
@ -63,17 +56,16 @@ def parse_time(value):
|
||||||
kw = dict((k, int(v)) for k, v in kw.iteritems() if v is not None)
|
kw = dict((k, int(v)) for k, v in kw.iteritems() if v is not None)
|
||||||
return datetime.time(**kw)
|
return datetime.time(**kw)
|
||||||
|
|
||||||
|
|
||||||
def parse_datetime(value):
|
def parse_datetime(value):
|
||||||
"""Parse a string and return a datetime.datetime.
|
"""Parses a string and return a datetime.datetime.
|
||||||
|
|
||||||
This function supports time zone offsets. When the input contains one,
|
This function supports time zone offsets. When the input contains one,
|
||||||
the output uses an instance of FixedOffset as tzinfo.
|
the output uses an instance of FixedOffset as tzinfo.
|
||||||
|
|
||||||
Sub-microsecond precision is accepted, but ignored.
|
Sub-microsecond precision is accepted, but ignored.
|
||||||
|
|
||||||
Raise ValueError if the input is well formatted but not a valid datetime.
|
Raises ValueError if the input is well formatted but not a valid datetime.
|
||||||
Return None if the input isn't well formatted.
|
Returns None if the input isn't well formatted.
|
||||||
"""
|
"""
|
||||||
match = datetime_re.match(value)
|
match = datetime_re.match(value)
|
||||||
if match:
|
if match:
|
||||||
|
|
Loading…
Reference in New Issue