dateparser package¶
Subpackages¶
Submodules¶
dateparser.conf module¶
- class dateparser.conf.Settings(*args, **kwargs)[source]¶
Bases:
object
Control and configure default parsing behavior of dateparser. Currently, supported settings are:
DATE_ORDER
PREFER_LOCALE_DATE_ORDER
TIMEZONE
TO_TIMEZONE
RETURN_AS_TIMEZONE_AWARE
PREFER_DAY_OF_MONTH
PREFER_DATES_FROM
RELATIVE_BASE
STRICT_PARSING
REQUIRE_PARTS
SKIP_TOKENS
NORMALIZE
RETURN_TIME_AS_PERIOD
PARSERS
dateparser.date module¶
- class dateparser.date.DateData(*, date_obj=None, period=None, locale=None)[source]¶
Bases:
object
Class that represents the parsed data with useful information. It can be accessed with square brackets like a dict object.
- class dateparser.date.DateDataParser(languages=None, locales=None, region=None, try_previous_locales=False, use_given_order=False, settings=None)[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 only these locales to translate date string.
region (str) – 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 (bool) – If True, locales previously used to translate date are tried first.
use_given_order (bool) – 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,SettingValidationError
: A provided setting is not valid.
- 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) – 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
DateData
object.- Raises
ValueError - Unknown Language
Note
Period values can be a ‘day’ (default), ‘week’, ‘month’, ‘year’, ‘time’.
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 ismonth
:>>> DateDataParser().get_date_data('March 2015') DateData(date_obj=datetime.datetime(2015, 3, 16, 0, 0), period='month', locale='en')
Similarly, for date strings with no day and month information present, level of precision is
year
and day16
and month6
are from current_date.>>> DateDataParser().get_date_data('2014') DateData(date_obj=datetime.datetime(2014, 6, 16, 0, 0), period='year', locale='en')
Dates with time zone indications or UTC offsets are returned in UTC time unless specified using `Settings`_.
>>> DateDataParser().get_date_data('23 March 2000, 1:21 PM CET') DateData(date_obj=datetime.datetime(2000, 3, 23, 13, 21, tzinfo=<StaticTzInfo 'CET'>), period='day', locale='en')
- locale_loader = None¶
dateparser.date_parser module¶
dateparser.freshness_date_parser module¶
dateparser.timezone_parser module¶
dateparser.timezones module¶
dateparser.utils module¶
Module contents¶
- dateparser.parse(date_string, date_formats=None, languages=None, locales=None, region=None, settings=None)[source]¶
Parse date and time from given date string.
- Parameters
date_string (str) – 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 only these locales to translate date string.
region (str) – 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,TypeError
: Languages argument must be a list,SettingValidationError
: A provided setting is not valid.