2011-11-18 21:01:06 +08:00
|
|
|
"""Functions to parse datetime objects."""
|
|
|
|
|
|
|
|
# We're using regular expressions rather than time.strptime because:
|
2011-12-17 10:31:34 +08:00
|
|
|
# - They provide both validation and parsing.
|
|
|
|
# - They're more flexible for datetimes.
|
|
|
|
# - The date/datetime/time constructors produce friendlier error messages.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import re
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2013-09-08 16:43:33 +08:00
|
|
|
from django.utils.timezone import utc, get_fixed_timezone
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
date_re = re.compile(
|
2011-12-17 10:31:34 +08:00
|
|
|
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$'
|
2011-11-18 21:01:06 +08:00
|
|
|
)
|
|
|
|
|
2012-08-20 03:47:41 +08:00
|
|
|
time_re = re.compile(
|
|
|
|
r'(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
|
2011-12-17 10:31:34 +08:00
|
|
|
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
|
2011-11-18 21:01:06 +08:00
|
|
|
)
|
|
|
|
|
2012-08-20 03:47:41 +08:00
|
|
|
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})'
|
2011-12-17 10:31:34 +08:00
|
|
|
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
|
2012-08-20 03:47:41 +08:00
|
|
|
r'(?P<tzinfo>Z|[+-]\d{2}:?\d{2})?$'
|
2011-11-18 21:01:06 +08:00
|
|
|
)
|
|
|
|
|
2013-09-08 16:43:33 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def parse_date(value):
|
2011-12-17 10:31:34 +08:00
|
|
|
"""Parses a string and return a datetime.date.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2011-12-17 10:31:34 +08:00
|
|
|
Raises ValueError if the input is well formatted but not a valid date.
|
|
|
|
Returns None if the input isn't well formatted.
|
2011-11-18 21:01:06 +08:00
|
|
|
"""
|
|
|
|
match = date_re.match(value)
|
|
|
|
if match:
|
2012-07-21 03:14:27 +08:00
|
|
|
kw = dict((k, int(v)) for k, v in six.iteritems(match.groupdict()))
|
2011-11-18 21:01:06 +08:00
|
|
|
return datetime.date(**kw)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def parse_time(value):
|
2011-12-17 10:31:34 +08:00
|
|
|
"""Parses a string and return a datetime.time.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
This function doesn't support time zone offsets.
|
|
|
|
|
2011-12-17 10:31:34 +08:00
|
|
|
Raises ValueError if the input is well formatted but not a valid time.
|
|
|
|
Returns None if the input isn't well formatted, in particular if it
|
2011-11-18 21:01:06 +08:00
|
|
|
contains an offset.
|
|
|
|
"""
|
|
|
|
match = time_re.match(value)
|
|
|
|
if match:
|
|
|
|
kw = match.groupdict()
|
|
|
|
if kw['microsecond']:
|
|
|
|
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
|
2012-07-21 03:14:27 +08:00
|
|
|
kw = dict((k, int(v)) for k, v in six.iteritems(kw) if v is not None)
|
2011-11-18 21:01:06 +08:00
|
|
|
return datetime.time(**kw)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def parse_datetime(value):
|
2011-12-17 10:31:34 +08:00
|
|
|
"""Parses a string and return a datetime.datetime.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
This function supports time zone offsets. When the input contains one,
|
2013-09-08 16:43:33 +08:00
|
|
|
the output uses a timezone with a fixed offset from UTC.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2011-12-17 10:31:34 +08:00
|
|
|
Raises ValueError if the input is well formatted but not a valid datetime.
|
|
|
|
Returns None if the input isn't well formatted.
|
2011-11-18 21:01:06 +08:00
|
|
|
"""
|
|
|
|
match = datetime_re.match(value)
|
|
|
|
if match:
|
|
|
|
kw = match.groupdict()
|
|
|
|
if kw['microsecond']:
|
|
|
|
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
|
|
|
|
tzinfo = kw.pop('tzinfo')
|
|
|
|
if tzinfo == 'Z':
|
|
|
|
tzinfo = utc
|
|
|
|
elif tzinfo is not None:
|
2012-08-20 03:47:41 +08:00
|
|
|
offset = 60 * int(tzinfo[1:3]) + int(tzinfo[-2:])
|
2011-11-18 21:01:06 +08:00
|
|
|
if tzinfo[0] == '-':
|
|
|
|
offset = -offset
|
2013-09-08 16:43:33 +08:00
|
|
|
tzinfo = get_fixed_timezone(offset)
|
2012-07-21 03:14:27 +08:00
|
|
|
kw = dict((k, int(v)) for k, v in six.iteritems(kw) if v is not None)
|
2011-11-18 21:01:06 +08:00
|
|
|
kw['tzinfo'] = tzinfo
|
|
|
|
return datetime.datetime(**kw)
|