parser — dateutil 0.1.dev1+gc981f9c documentation (original) (raw)

This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time.

This module attempts to be forgiving with regards to unlikely input formats, returning a datetime object even for dates which are ambiguous. If an element of a date/time stamp is omitted, the following rules are applied:

If any other elements are missing, they are taken from thedatetime.datetime object passed to the parameter default. If this results in a day number exceeding the valid number of days per month, the value falls back to the end of the month.

Additional resources about date/time string formats can be found below:

Functions

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

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

Parameters:

The **kwargs parameter takes the following keyword arguments:

Parameters:

This parameter is ignored if ignoretz is set.

Returns:

Returns a datetime.datetime object or, if thefuzzy_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:

parser.isoparse(dt_str)

Parse an ISO-8601 datetime string into a datetime.datetime.

An ISO-8601 datetime string consists of a date portion, followed optionally by a time portion - the date and time portions are separated by a single character separator, which is T in the official standard. Incomplete date formats (such as YYYY-MM) may not be combined with a time portion.

Supported date formats are:

Common:

Uncommon:

The ISO week and day numbering follows the same logic asdatetime.date.isocalendar().

Supported time formats are:

Midnight is a special case for hh, as the standard supports both 00:00 and 24:00 as a representation. The decimal separator can be either a dot or a comma.

Caution

Support for fractional components other than seconds is part of the ISO-8601 standard, but is not currently implemented in this parser.

Supported time zone offset formats are:

Offsets will be represented as dateutil.tz.tzoffset objects, with the exception of UTC, which will be represented asdateutil.tz.tzutc. Time zone offsets equivalent to UTC (such as +00:00) will also be represented as dateutil.tz.tzutc.

Parameters:

dt_str – A string or stream containing only an ISO-8601 datetime string

Returns:

Returns a datetime.datetime representing the string. Unspecified components default to their lowest value.

Warning

As of version 2.7.0, the strictness of the parser should not be considered a stable part of the contract. Any valid ISO-8601 string that parses correctly with the default settings will continue to parse correctly in future versions, but invalid strings that currently fail (e.g. 2017-01-01T00:00+00:00:00) are not guaranteed to continue failing in future versions if they encode a valid date.

Added in version 2.7.0.

Classes

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:

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', '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]

Converts two-digit years to year within [-50, 49] range of self._year (current local time)

hms(name)[source]

jump(name)[source]

month(name)[source]

pertain(name)[source]

tzoffset(name)[source]

utczone(name)[source]

validate(res)[source]

weekday(name)[source]

Warnings and Exceptions

class dateutil.parser.ParserError[source]

Exception subclass used for any failure to parse a datetime string.

This is a subclass of ValueError, and should be raised any time earlier versions of dateutil would have raised ValueError.

Added in version 2.8.1.

class dateutil.parser.UnknownTimezoneWarning[source]

Raised when the parser finds a timezone it cannot parse into a tzinfo.

Added in version 2.7.0.