def parse_date(date_string): """Parse a date string as returned from now_str() into a datetime object. """ if _dateutil_parse: return _dateutil_parse(date_string) else: try: date_fmt = "%a, %d %b %Y %H:%M:%S %z" return datetime.datetime.strptime(date_string, date_fmt) except ValueError: date_fmt = "%a, %d %b %Y %H:%M:%S" return datetime.datetime.strptime(date_string, date_fmt)
def parse_date(s): """parse an ISO8601 date string If it is None or not a valid ISO8601 timestamp, it will be returned unmodified. Otherwise, it will return a datetime object. """ if s is None: return s m = ISO8601_PAT.match(s) if m: dt = _dateutil_parse(s) return _ensure_tzinfo(dt) return s
def parse_date(date_string): """Parse a date string into a datetime object. The function accepts strings as returned by datetime.isoformat() and date_str_rfc5322(). """ if _dateutil_parse: return _dateutil_parse(date_string) else: try: return _dt_fromisoformat(date_string) except ValueError: try: date_fmt = "%a, %d %b %Y %H:%M:%S %z" return datetime.datetime.strptime(date_string, date_fmt) except ValueError: try: date_fmt = "%a, %d %b %Y %H:%M:%S" return datetime.datetime.strptime(date_string, date_fmt) except ValueError: raise ValueError("Invalid date string: '%s'" % date_string) from None
def _dateutilparsefun(date): """dateutil parsing function""" if hasattr(date, 'year'): return date return _dateutil_parse(date)