From 9c19aff7c7561e3a82978a272ecdaad40dda5c00 Mon Sep 17 00:00:00 2001 From: django-bot Date: Thu, 3 Feb 2022 20:24:19 +0100 Subject: Refs #33476 -- Reformatted code with Black. --- django/utils/dateparse.py | 92 +++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 47 deletions(-) (limited to 'django/utils/dateparse.py') diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py index a137031b3f..2e6a260a4f 100644 --- a/django/utils/dateparse.py +++ b/django/utils/dateparse.py @@ -10,59 +10,57 @@ import datetime from django.utils.regex_helper import _lazy_re_compile from django.utils.timezone import get_fixed_timezone, utc -date_re = _lazy_re_compile( - r'(?P\d{4})-(?P\d{1,2})-(?P\d{1,2})$' -) +date_re = _lazy_re_compile(r"(?P\d{4})-(?P\d{1,2})-(?P\d{1,2})$") time_re = _lazy_re_compile( - r'(?P\d{1,2}):(?P\d{1,2})' - r'(?::(?P\d{1,2})(?:[\.,](?P\d{1,6})\d{0,6})?)?$' + r"(?P\d{1,2}):(?P\d{1,2})" + r"(?::(?P\d{1,2})(?:[\.,](?P\d{1,6})\d{0,6})?)?$" ) datetime_re = _lazy_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'\s*(?PZ|[+-]\d{2}(?::?\d{2})?)?$' + 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"\s*(?PZ|[+-]\d{2}(?::?\d{2})?)?$" ) standard_duration_re = _lazy_re_compile( - r'^' - r'(?:(?P-?\d+) (days?, )?)?' - r'(?P-?)' - r'((?:(?P\d+):)(?=\d+:\d+))?' - r'(?:(?P\d+):)?' - r'(?P\d+)' - r'(?:[\.,](?P\d{1,6})\d{0,6})?' - r'$' + r"^" + r"(?:(?P-?\d+) (days?, )?)?" + r"(?P-?)" + r"((?:(?P\d+):)(?=\d+:\d+))?" + r"(?:(?P\d+):)?" + r"(?P\d+)" + r"(?:[\.,](?P\d{1,6})\d{0,6})?" + r"$" ) # Support the sections of ISO 8601 date representation that are accepted by # timedelta iso8601_duration_re = _lazy_re_compile( - r'^(?P[-+]?)' - r'P' - r'(?:(?P\d+([\.,]\d+)?)D)?' - r'(?:T' - r'(?:(?P\d+([\.,]\d+)?)H)?' - r'(?:(?P\d+([\.,]\d+)?)M)?' - r'(?:(?P\d+([\.,]\d+)?)S)?' - r')?' - r'$' + r"^(?P[-+]?)" + r"P" + r"(?:(?P\d+([\.,]\d+)?)D)?" + r"(?:T" + r"(?:(?P\d+([\.,]\d+)?)H)?" + r"(?:(?P\d+([\.,]\d+)?)M)?" + r"(?:(?P\d+([\.,]\d+)?)S)?" + r")?" + r"$" ) # Support PostgreSQL's day-time interval format, e.g. "3 days 04:05:06". The # year-month and mixed intervals cannot be converted to a timedelta and thus # aren't accepted. postgres_interval_re = _lazy_re_compile( - r'^' - r'(?:(?P-?\d+) (days? ?))?' - r'(?:(?P[-+])?' - r'(?P\d+):' - r'(?P\d\d):' - r'(?P\d\d)' - r'(?:\.(?P\d{1,6}))?' - r')?$' + r"^" + r"(?:(?P-?\d+) (days? ?))?" + r"(?:(?P[-+])?" + r"(?P\d+):" + r"(?P\d\d):" + r"(?P\d\d)" + r"(?:\.(?P\d{1,6}))?" + r")?$" ) @@ -98,7 +96,7 @@ def parse_time(value): except ValueError: if match := time_re.match(value): kw = match.groupdict() - kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0') + kw["microsecond"] = kw["microsecond"] and kw["microsecond"].ljust(6, "0") kw = {k: int(v) for k, v in kw.items() if v is not None} return datetime.time(**kw) @@ -117,14 +115,14 @@ def parse_datetime(value): except ValueError: if match := datetime_re.match(value): kw = match.groupdict() - kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0') - tzinfo = kw.pop('tzinfo') - if tzinfo == 'Z': + kw["microsecond"] = kw["microsecond"] and kw["microsecond"].ljust(6, "0") + tzinfo = kw.pop("tzinfo") + if tzinfo == "Z": tzinfo = utc elif tzinfo is not None: 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 tzinfo = get_fixed_timezone(offset) kw = {k: int(v) for k, v in kw.items() if v is not None} @@ -140,17 +138,17 @@ def parse_duration(value): format. """ match = ( - standard_duration_re.match(value) or - iso8601_duration_re.match(value) or - postgres_interval_re.match(value) + standard_duration_re.match(value) + or iso8601_duration_re.match(value) + or postgres_interval_re.match(value) ) if match: kw = match.groupdict() - sign = -1 if kw.pop('sign', '+') == '-' else 1 - if kw.get('microseconds'): - kw['microseconds'] = kw['microseconds'].ljust(6, '0') - kw = {k: float(v.replace(',', '.')) for k, v in kw.items() if v is not None} - days = datetime.timedelta(kw.pop('days', .0) or .0) + sign = -1 if kw.pop("sign", "+") == "-" else 1 + if kw.get("microseconds"): + kw["microseconds"] = kw["microseconds"].ljust(6, "0") + kw = {k: float(v.replace(",", ".")) for k, v in kw.items() if v is not None} + days = datetime.timedelta(kw.pop("days", 0.0) or 0.0) if match.re == iso8601_duration_re: days *= sign return days + sign * datetime.timedelta(**kw) -- cgit v1.3