dateparser package

Submodules

dateparser.conf module

class dateparser.conf.Settings(settings=None)[source]

Bases: object

Control and configure default parsing behavior of dateparser. Currently, supported settings are:

  • PREFER_DATES_FROM: defaults to current_period. Options are future or past.
  • SUPPORT_BEFORE_COMMON_ERA: defaults to False.
  • PREFER_DAY_OF_MONTH: defaults to current. Could be first and last day of month.
  • SKIP_TOKENS: defaults to [‘t’]. Can be any string.
  • TIMEZONE: defaults to UTC. Can be timezone abbreviation or any of tz database name as given here.
  • RETURN_AS_TIMEZONE_AWARE: return tz aware datetime objects in case timezone is detected in the date string.
  • RELATIVE_BASE: count relative date from this base date. Should be datetime object.
  • RETURN_TIME_AS_PERIOD: returns period as time in case time component is detected in the date string.

Default: False.

classmethod get_key(settings=None)[source]
replace(**kwds)[source]
dateparser.conf.apply_settings(f)[source]

dateparser.date module

class dateparser.date.DateDataParser(**kwargs)[source]

Bases: object

Class which handles language detection, translation and subsequent generic parsing of string representing date and/or time.

Parameters:
  • languages (list) – A list of language codes, e.g. [‘en’, ‘es’, ‘zh-Hant’]. If locales are not given, languages and region are used to construct locales for translation.
  • locales (list) – A list of locale codes, e.g. [‘fr-PF’, ‘qu-EC’, ‘af-NA’]. The parser uses locales to translate date string.
  • region (str|unicode) – A region code, e.g. ‘IN’, ‘001’, ‘NE’. If locales are not given, languages and region are used to construct locales for translation.
  • try_previous_locales – If True, locales previously used to translate date are tried first.
  • use_given_order – If True, locales are tried for translation of date string in the order in which they are given.
  • settings (dict) – Configure customized behavior using settings defined in dateparser.conf.Settings.
Returns:

A parser instance

Raises:

ValueError - Unknown Language, TypeError - Languages argument must be a list

get_date_data(date_string, date_formats=None)[source]

Parse string representing date and/or time in recognizable localized formats. Supports parsing multiple languages and timezones.

Parameters:
  • date_string (str|unicode) – A string representing date and/or time in a recognizably valid format.
  • date_formats (list) – A list of format strings using directives as given here. The parser applies formats one by one, taking into account the detected languages.
Returns:

a dict mapping keys to datetime.datetime object and period. For example: {‘date_obj’: datetime.datetime(2015, 6, 1, 0, 0), ‘period’: u’day’}

Raises:

ValueError - Unknown Language

Note

Period values can be a ‘day’ (default), ‘week’, ‘month’, ‘year’.

Period represents the granularity of date parsed from the given string.

In the example below, since no day information is present, the day is assumed to be current day 16 from current date (which is June 16, 2015, at the moment of writing this). Hence, the level of precision is month:

>>> DateDataParser().get_date_data(u'March 2015')
{'date_obj': datetime.datetime(2015, 3, 16, 0, 0), 'period': u'month'}

Similarly, for date strings with no day and month information present, level of precision is year and day 16 and month 6 are from current_date.

>>> DateDataParser().get_date_data(u'2014')
{'date_obj': datetime.datetime(2014, 6, 16, 0, 0), 'period': u'year'}

Dates with time zone indications or UTC offsets are returned in UTC time unless specified using `Settings`_.

>>> DateDataParser().get_date_data(u'23 March 2000, 1:21 PM CET')
{'date_obj': datetime.datetime(2000, 3, 23, 14, 21), 'period': 'day'}
get_date_tuple(*args, **kwargs)[source]
locale_loader = None
dateparser.date.date_range(begin, end, **kwargs)[source]
dateparser.date.get_date_from_timestamp(date_string, settings)[source]
dateparser.date.get_intersecting_periods(low, high, period=u'day')[source]
dateparser.date.get_last_day_of_month(year, month)[source]
dateparser.date.parse_with_formats(date_string, date_formats, settings)[source]

Parse with formats and return a dictionary with ‘period’ and ‘obj_date’.

Returns:datetime.datetime, dict or None
dateparser.date.sanitize_date(date_string)[source]
dateparser.date.sanitize_spaces(date_string)[source]

dateparser.date_parser module

class dateparser.date_parser.DateParser[source]

Bases: object

parse(**kwargs)[source]

dateparser.freshness_date_parser module

class dateparser.freshness_date_parser.FreshnessDateDataParser[source]

Bases: object

Parses date string like “1 year, 2 months ago” and “3 hours, 50 minutes ago”

get_date_data(date_string, settings=None)[source]
get_kwargs(date_string)[source]
get_local_tz()[source]
parse(date_string, settings)[source]

dateparser.timezone_parser module

class dateparser.timezone_parser.StaticTzInfo(name, offset)[source]

Bases: datetime.tzinfo

dst(dt)[source]

datetime -> DST offset in minutes east of UTC.

localize(dt, is_dst=False)[source]
tzname(dt)[source]

datetime -> string name of time zone.

utcoffset(dt)[source]

datetime -> minutes east of UTC (negative for west of UTC).

dateparser.timezone_parser.build_tz_offsets(search_regex_parts)[source]
dateparser.timezone_parser.convert_to_local_tz(datetime_obj, datetime_tz_offset)[source]
dateparser.timezone_parser.get_local_tz_offset()[source]
dateparser.timezone_parser.pop_tz_offset_from_string(date_string, as_offset=True)[source]
dateparser.timezone_parser.word_is_tz(word)[source]

dateparser.timezones module

dateparser.utils module

dateparser.utils.apply_dateparser_timezone(utc_datetime, offset_or_timezone_abb)[source]
dateparser.utils.apply_timezone(date_time, tz_string)[source]
dateparser.utils.apply_timezone_from_settings(date_obj, settings)[source]
dateparser.utils.apply_tzdatabase_timezone(date_time, pytz_string)[source]
dateparser.utils.combine_dicts(primary_dict, supplementary_dict)[source]
dateparser.utils.convert_to_unicode(info)[source]
dateparser.utils.find_date_separator(format)[source]
dateparser.utils.get_logger()[source]
dateparser.utils.localize_timezone(date_time, tz_string)[source]
dateparser.utils.normalize_unicode(string, form='NFKD')[source]
dateparser.utils.registry(cls)[source]
dateparser.utils.setup_logging()[source]
dateparser.utils.strip_braces(date_string)[source]

Module contents

dateparser.parse(*args, **kwargs)[source]

Parse date and time from given date string.

Parameters:
  • date_string (str|unicode) – A string representing date and/or time in a recognizably valid format.
  • date_formats (list) –

    A list of format strings using directives as given here. The parser applies formats one by one, taking into account the detected languages/locales.

  • languages (list) – A list of language codes, e.g. [‘en’, ‘es’, ‘zh-Hant’]. If locales are not given, languages and region are used to construct locales for translation.
  • locales (list) – A list of locale codes, e.g. [‘fr-PF’, ‘qu-EC’, ‘af-NA’]. The parser uses locales to translate date string.
  • region (str|unicode) – A region code, e.g. ‘IN’, ‘001’, ‘NE’. If locales are not given, languages and region are used to construct locales for translation.
  • settings (dict) – Configure customized behavior using settings defined in dateparser.conf.Settings.
Returns:

Returns datetime representing parsed date if successful, else returns None

Return type:

datetime.

Raises:

ValueError - Unknown Language