parser

dateutil.parser.parse(timestr, parserinfo=None, **kwargs)[source]

Parse a string in one of the supported formats, using the parserinfo parameters.

Parameters:
  • timestr – A string containing a date/time stamp.
  • parserinfo – A parserinfo object containing parameters for the parser. If None, the default arguments to the parserinfo constructor are used.

The **kwargs parameter takes the following keyword arguments:

Parameters:
  • default – The default datetime object, if this is a datetime object and not None, elements specified in timestr replace elements in the default object.
  • ignoretz – If set True, time zones in parsed strings are ignored and a naive datetime object is returned.
  • tzinfos

    Additional time zone names / aliases which may be present in the string. This argument maps time zone names (and optionally offsets from those time zones) to time zones. This parameter can be a dictionary with timezone aliases mapping time zone names to time zones or a function taking two parameters (tzname and tzoffset) and returning a time zone.

    The timezones to which the names are mapped can be an integer offset from UTC in seconds or a tzinfo object.

     >>> from dateutil.parser import parse
     >>> from dateutil.tz import gettz
     >>> tzinfos = {"BRST": -7200, "CST": gettz("America/Chicago")}
     >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
     datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -7200))
     >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
     datetime.datetime(2012, 1, 19, 17, 21,
                       tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
    

    This parameter is ignored if ignoretz is set.

  • dayfirst – Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (True) or month (False). If yearfirst is set to True, this distinguishes between YDM and YMD. If set to None, this value is retrieved from the current parserinfo object (which itself defaults to False).
  • yearfirst – Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the year. If True, the first number is taken to be the year, otherwise the last number is taken to be the year. If this is set to None, the value is retrieved from the current parserinfo object (which itself defaults to False).
  • fuzzy – Whether to allow fuzzy parsing, allowing for string like “Today is January 1, 2047 at 8:21:00AM”.
  • fuzzy_with_tokens

    If True, fuzzy is automatically set to True, and the parser will return a tuple where the first element is the parsed datetime.datetime datetimestamp and the second element is a tuple containing the portions of the string which were ignored:

    >>> from dateutil.parser import parse
    >>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
    (datetime.datetime(2047, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
    
Returns:

Returns a datetime.datetime object or, if the fuzzy_with_tokens option is True, returns a tuple, the first element being a datetime.datetime object, the second a tuple containing the fuzzy tokens.

Raises:
  • ValueError – Raised for invalid or unknown string format, if the provided tzinfo is not in a valid format, or if an invalid date would be created.
  • OverflowError – Raised if the parsed date exceeds the largest valid C integer on your system.
class dateutil.parser.parser(info=None)[source]
parse(timestr, default=None, ignoretz=False, tzinfos=None, **kwargs)[source]

Parse the date/time string into a datetime.datetime object.

Parameters:
  • timestr – Any date/time string using the supported formats.
  • default – The default datetime object, if this is a datetime object and not None, elements specified in timestr replace elements in the default object.
  • ignoretz – If set True, time zones in parsed strings are ignored and a naive datetime.datetime object is returned.
  • tzinfos

    Additional time zone names / aliases which may be present in the string. This argument maps time zone names (and optionally offsets from those time zones) to time zones. This parameter can be a dictionary with timezone aliases mapping time zone names to time zones or a function taking two parameters (tzname and tzoffset) and returning a time zone.

    The timezones to which the names are mapped can be an integer offset from UTC in seconds or a tzinfo object.

     >>> from dateutil.parser import parse
     >>> from dateutil.tz import gettz
     >>> tzinfos = {"BRST": -7200, "CST": gettz("America/Chicago")}
     >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
     datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -7200))
     >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
     datetime.datetime(2012, 1, 19, 17, 21,
                       tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
    

    This parameter is ignored if ignoretz is set.

  • **kwargs

    Keyword arguments as passed to _parse().

Returns:

Returns a datetime.datetime object or, if the fuzzy_with_tokens option is True, returns a tuple, the first element being a datetime.datetime object, the second a tuple containing the fuzzy tokens.

Raises:
  • ValueError – Raised for invalid or unknown string format, if the provided tzinfo is not in a valid format, or if an invalid date would be created.
  • TypeError – Raised for non-string or character stream input.
  • OverflowError – Raised if the parsed date exceeds the largest valid C integer on your system.
class dateutil.parser.parserinfo(dayfirst=False, yearfirst=False)[source]

Class which handles what inputs are accepted. Subclass this to customize the language and acceptable values for each parameter.

Parameters:
  • dayfirst – Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (True) or month (False). If yearfirst is set to True, this distinguishes between YDM and YMD. Default is False.
  • yearfirst – Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the year. If True, the first number is taken to be the year, otherwise the last number is taken to be the year. Default is False.
AMPM = [('am', 'a'), ('pm', 'p')]
HMS = [('h', 'hour', 'hours'), ('m', 'minute', 'minutes'), ('s', 'second', 'seconds')]
JUMP = [' ', '.', ',', ';', '-', '/', "'", 'at', 'on', 'and', 'ad', 'm', 't', 'of', 'st', 'nd', 'rd', 'th']
MONTHS = [('Jan', 'January'), ('Feb', 'February'), ('Mar', 'March'), ('Apr', 'April'), ('May', 'May'), ('Jun', 'June'), ('Jul', 'July'), ('Aug', 'August'), ('Sep', 'Sept', 'September'), ('Oct', 'October'), ('Nov', 'November'), ('Dec', 'December')]
PERTAIN = ['of']
TZOFFSET = {}
UTCZONE = ['UTC', 'GMT', 'Z']
WEEKDAYS = [('Mon', 'Monday'), ('Tue', 'Tuesday'), ('Wed', 'Wednesday'), ('Thu', 'Thursday'), ('Fri', 'Friday'), ('Sat', 'Saturday'), ('Sun', 'Sunday')]
ampm(name)[source]
convertyear(year, century_specified=False)[source]
hms(name)[source]
jump(name)[source]
month(name)[source]
pertain(name)[source]
tzoffset(name)[source]
utczone(name)[source]
validate(res)[source]
weekday(name)[source]
exception dateutil.parser.InvalidDatetimeError[source]
exception dateutil.parser.InvalidDateError[source]
exception dateutil.parser.InvalidTimeError[source]