MCPcopy Create free account
hub / github.com/dateutil/dateutil / parse

Method parse

src/dateutil/parser/_parser.py:572–659  ·  view source on GitHub ↗

Parse the date/time string into a :class:`datetime.datetime` object. :param timestr: Any date/time string using the supported formats. :param default: The default datetime object, if this is a datetime object and not ``None``, elements s

(self, timestr, default=None,
              ignoretz=False, tzinfos=None, **kwargs)

Source from the content-addressed store, hash-verified

570 self.info = info or parserinfo()
571
572 def parse(self, timestr, default=None,
573 ignoretz=False, tzinfos=None, **kwargs):
574 """
575 Parse the date/time string into a :class:`datetime.datetime` object.
576
577 :param timestr:
578 Any date/time string using the supported formats.
579
580 :param default:
581 The default datetime object, if this is a datetime object and not
582 ``None``, elements specified in ``timestr`` replace elements in the
583 default object.
584
585 :param ignoretz:
586 If set ``True``, time zones in parsed strings are ignored and a
587 naive :class:`datetime.datetime` object is returned.
588
589 :param tzinfos:
590 Additional time zone names / aliases which may be present in the
591 string. This argument maps time zone names (and optionally offsets
592 from those time zones) to time zones. This parameter can be a
593 dictionary with timezone aliases mapping time zone names to time
594 zones or a function taking two parameters (``tzname`` and
595 ``tzoffset``) and returning a time zone.
596
597 The timezones to which the names are mapped can be an integer
598 offset from UTC in seconds or a :class:`tzinfo` object.
599
600 .. doctest::
601 :options: +NORMALIZE_WHITESPACE
602
603 >>> from dateutil.parser import parse
604 >>> from dateutil.tz import gettz
605 >>> tzinfos = {"BRST": -7200, "CST": gettz("America/Chicago")}
606 >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
607 datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -7200))
608 >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
609 datetime.datetime(2012, 1, 19, 17, 21,
610 tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
611
612 This parameter is ignored if ``ignoretz`` is set.
613
614 :param \\*\\*kwargs:
615 Keyword arguments as passed to ``_parse()``.
616
617 :return:
618 Returns a :class:`datetime.datetime` object or, if the
619 ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
620 first element being a :class:`datetime.datetime` object, the second
621 a tuple containing the fuzzy tokens.
622
623 :raises ParserError:
624 Raised for invalid or unknown string format, if the provided
625 :class:`tzinfo` is not in a valid format, or if an invalid date
626 would be created.
627
628 :raises TypeError:
629 Raised for non-string or character stream input.

Callers 8

_handle_UNTILMethod · 0.45
_parse_date_valueMethod · 0.45
_parse_rfcMethod · 0.45
parseFunction · 0.45
_parsetzFunction · 0.45
testParserParseStrMethod · 0.45
testCustomParserInfoMethod · 0.45

Calls 6

_parseMethod · 0.95
_build_naiveMethod · 0.95
_build_tzawareMethod · 0.95
ParserErrorClass · 0.85
replaceMethod · 0.45
getMethod · 0.45

Tested by 3

testParserParseStrMethod · 0.36
testCustomParserInfoMethod · 0.36