Debian Codegraph @

docs/introduction.rst

raw
1 ==========================
2 Introduction to dateparser
3 ==========================
4
5
6 Features
7 ========
8
9 * Generic parsing of dates in over 200 language locales plus numerous formats in a language agnostic fashion.
10 * Generic parsing of relative dates like: ``'1 min ago'``, ``'2 weeks ago'``, ``'3 months, 1 week and 1 day ago'``, ``'in 2 days'``, ``'tomorrow'``.
11 * Generic parsing of dates with time zones abbreviations or UTC offsets like: ``'August 14, 2015 EST'``, ``'July 4, 2013 PST'``, ``'21 July 2013 10:15 pm +0500'``.
12 * Date lookup in longer texts.
13 * Support for non-Gregorian calendar systems. See `Supported Calendars`_.
14 * Extensive test coverage.
15
16
17 Basic Usage
18 ===========
19
20 The most straightforward way is to use the `dateparser.parse <#dateparser.parse>`_ function,
21 that wraps around most of the functionality in the module.
22
23 .. automodule:: dateparser
24 :members: parse
25 :noindex:
26
27
28 Popular Formats
29 ---------------
30
31 >>> import dateparser
32 >>> dateparser.parse('12/12/12')
33 datetime.datetime(2012, 12, 12, 0, 0)
34 >>> dateparser.parse('Fri, 12 Dec 2014 10:55:50')
35 datetime.datetime(2014, 12, 12, 10, 55, 50)
36 >>> dateparser.parse('Martes 21 de Octubre de 2014') # Spanish (Tuesday 21 October 2014)
37 datetime.datetime(2014, 10, 21, 0, 0)
38 >>> dateparser.parse('Le 11 Décembre 2014 à 09:00') # French (11 December 2014 at 09:00)
39 datetime.datetime(2014, 12, 11, 9, 0)
40 >>> dateparser.parse('13 января 2015 г. в 13:34') # Russian (13 January 2015 at 13:34)
41 datetime.datetime(2015, 1, 13, 13, 34)
42 >>> dateparser.parse('1 เดือนตุลาคม 2005, 1:00 AM') # Thai (1 October 2005, 1:00 AM)
43 datetime.datetime(2005, 10, 1, 1, 0)
44
45 This will try to parse a date from the given string, attempting to
46 detect the language each time.
47
48 You can specify the language(s), if known, using ``languages`` argument. In this case, given languages are used and language detection is skipped:
49
50 >>> dateparser.parse('2015, Ago 15, 1:08 pm', languages=['pt', 'es'])
51 datetime.datetime(2015, 8, 15, 13, 8)
52
53 If you know the possible formats of the dates, you can
54 use the ``date_formats`` argument:
55
56 >>> dateparser.parse('22 Décembre 2010', date_formats=['%d %B %Y'])
57 datetime.datetime(2010, 12, 22, 0, 0)
58
59
60 Relative Dates
61 --------------
62
63 >>> parse('1 hour ago')
64 datetime.datetime(2015, 5, 31, 23, 0)
65 >>> parse('Il ya 2 heures') # French (2 hours ago)
66 datetime.datetime(2015, 5, 31, 22, 0)
67 >>> parse('1 anno 2 mesi') # Italian (1 year 2 months)
68 datetime.datetime(2014, 4, 1, 0, 0)
69 >>> parse('yaklaşık 23 saat önce') # Turkish (23 hours ago)
70 datetime.datetime(2015, 5, 31, 1, 0)
71 >>> parse('Hace una semana') # Spanish (a week ago)
72 datetime.datetime(2015, 5, 25, 0, 0)
73 >>> parse('2小时前') # Chinese (2 hours ago)
74 datetime.datetime(2015, 5, 31, 22, 0)
75
76 .. note:: Testing above code might return different values for you depending on your environment's current date and time.
77
78 .. note:: For `Finnish` language, please specify ``settings={'SKIP_TOKENS': []}`` to correctly parse relative dates.
79
80 OOTB Language Based Date Order Preference
81 -----------------------------------------
82
83 >>> # parsing ambiguous date
84 >>> parse('02-03-2016') # assumes english language, uses MDY date order
85 datetime.datetime(2016, 2, 3, 0, 0)
86 >>> parse('le 02-03-2016') # detects french, uses DMY date order
87 datetime.datetime(2016, 3, 2, 0, 0)
88
89 .. note:: Ordering is not locale based, that's why do not expect `DMY` order for UK/Australia English. You can specify date order in that case as follows using :ref:`settings`:
90
91 >>> parse('18-12-15 06:00', settings={'DATE_ORDER': 'DMY'})
92 datetime.datetime(2015, 12, 18, 6, 0)
93
94 For more on date order, please look at :ref:`settings`.
95
96
97 Timezone and UTC Offset
98 -----------------------
99
100 By default, `dateparser` returns tzaware `datetime` if timezone is present in date string. Otherwise, it returns a naive `datetime` object.
101
102 >>> parse('January 12, 2012 10:00 PM EST')
103 datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'EST'>)
104
105 >>> parse('January 12, 2012 10:00 PM -0500')
106 datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'UTC\-05:00'>)
107
108 >>> parse('2 hours ago EST')
109 datetime.datetime(2017, 3, 10, 15, 55, 39, 579667, tzinfo=<StaticTzInfo 'EST'>)
110
111 >>> parse('2 hours ago -0500')
112 datetime.datetime(2017, 3, 10, 15, 59, 30, 193431, tzinfo=<StaticTzInfo 'UTC\-05:00'>)
113
114 If date has no timezone name/abbreviation or offset, you can specify it using `TIMEZONE` setting.
115
116 >>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': 'US/Eastern'})
117 datetime.datetime(2012, 1, 12, 22, 0)
118
119 >>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': '+0500'})
120 datetime.datetime(2012, 1, 12, 22, 0)
121
122 ``TIMEZONE`` option may not be useful alone as it only attaches given timezone to
123 resultant ``datetime`` object. But can be useful in cases where you want conversions from and to different
124 timezones or when simply want a tzaware date with given timezone info attached.
125
126 >>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': 'US/Eastern', 'RETURN_AS_TIMEZONE_AWARE': True})
127 datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
128
129
130 >>> parse('10:00 am', settings={'TIMEZONE': 'EST', 'TO_TIMEZONE': 'EDT'})
131 datetime.datetime(2016, 9, 25, 11, 0)
132
133 Some more use cases for conversion of timezones.
134
135 >>> parse('10:00 am EST', settings={'TO_TIMEZONE': 'EDT'}) # date string has timezone info
136 datetime.datetime(2017, 3, 12, 11, 0, tzinfo=<StaticTzInfo 'EDT'>)
137
138 >>> parse('now EST', settings={'TO_TIMEZONE': 'UTC'}) # relative dates
139 datetime.datetime(2017, 3, 10, 23, 24, 47, 371823, tzinfo=<StaticTzInfo 'UTC'>)
140
141 In case, no timezone is present in date string or defined in :ref:`settings`. You can still
142 return tzaware ``datetime``. It is especially useful in case of relative dates when uncertain
143 what timezone is relative base.
144
145 >>> parse('2 minutes ago', settings={'RETURN_AS_TIMEZONE_AWARE': True})
146 datetime.datetime(2017, 3, 11, 4, 25, 24, 152670, tzinfo=<DstTzInfo 'Asia/Karachi' PKT+5:00:00 STD>)
147
148 In case, you want to compute relative dates in UTC instead of default system's local timezone, you can use `TIMEZONE` setting.
149
150 >>> parse('4 minutes ago', settings={'TIMEZONE': 'UTC'})
151 datetime.datetime(2017, 3, 10, 23, 27, 59, 647248, tzinfo=<StaticTzInfo 'UTC'>)
152
153 .. note:: In case, when timezone is present both in string and also specified using :ref:`settings`, string is parsed into tzaware representation and then converted to timezone specified in :ref:`settings`.
154
155 >>> parse('10:40 pm PKT', settings={'TIMEZONE': 'UTC'})
156 datetime.datetime(2017, 3, 12, 17, 40, tzinfo=<StaticTzInfo 'UTC'>)
157
158 >>> parse('20 mins ago EST', settings={'TIMEZONE': 'UTC'})
159 datetime.datetime(2017, 3, 12, 21, 16, 0, 885091, tzinfo=<StaticTzInfo 'UTC'>)
160
161 For more on timezones, please look at :ref:`settings`.
162
163
164 Incomplete Dates
165 ----------------
166
167 >>> from dateparser import parse
168 >>> parse('December 2015') # default behavior
169 datetime.datetime(2015, 12, 16, 0, 0)
170 >>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'last'})
171 datetime.datetime(2015, 12, 31, 0, 0)
172 >>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'first'})
173 datetime.datetime(2015, 12, 1, 0, 0)
174
175 >>> parse('March')
176 datetime.datetime(2015, 3, 16, 0, 0)
177 >>> parse('March', settings={'PREFER_DATES_FROM': 'future'})
178 datetime.datetime(2016, 3, 16, 0, 0)
179 >>> # parsing with preference set for 'past'
180 >>> parse('August', settings={'PREFER_DATES_FROM': 'past'})
181 datetime.datetime(2015, 8, 15, 0, 0)
182
183 >>> import dateparser
184 >>> dateparser.parse("2015") # default behavior
185 datetime.datetime(2015, 3, 27, 0, 0)
186 >>> dateparser.parse("2015", settings={"PREFER_MONTH_OF_YEAR": "last"})
187 datetime.datetime(2015, 12, 27, 0, 0)
188 >>> dateparser.parse("2015", settings={"PREFER_MONTH_OF_YEAR": "first"})
189 datetime.datetime(2015, 1, 27, 0, 0)
190 >>> dateparser.parse("2015", settings={"PREFER_MONTH_OF_YEAR": "current"})
191 datetime.datetime(2015, 3, 27, 0, 0)
192
193 You can also ignore parsing incomplete dates altogether by setting `STRICT_PARSING` flag as follows:
194
195 >>> parse('December 2015', settings={'STRICT_PARSING': True})
196 None
197
198 For more on handling incomplete dates, please look at :ref:`settings`.
199
200
201 Search for Dates in Longer Chunks of Text
202 -----------------------------------------
203
204 .. warning:: Support for searching dates is really limited and needs a lot of improvement, we look forward to community's contribution to get better on that part. See ":ref:`contributing`".
205
206
207 You can extract dates from longer strings of text. They are returned as list of tuples with text chunk containing the date and parsed datetime object.
208
209
210 .. automodule:: dateparser.search
211 :members: search_dates
212 :noindex:
213
214 Advanced Usage
215 ==============
216 If you need more control over what is being parser check the :ref:`settings` section as well as the :ref:`using-datedataparser` section.
217
218
219 Dependencies
220 ============
221
222 `dateparser` relies on following libraries in some ways:
223
224 * dateutil_'s module ``relativedelta`` for its freshness parser.
225 * convertdate_ to convert *Jalali* dates to *Gregorian*.
226 * hijridate_ to convert *Hijri* dates to *Gregorian*.
227 * tzlocal_ to reliably get local timezone.
228 * ruamel.yaml_ (optional) for operations on language files.
229
230 .. _dateutil: https://pypi.python.org/pypi/python-dateutil
231 .. _convertdate: https://pypi.python.org/pypi/convertdate
232 .. _hijridate: https://pypi.python.org/pypi/hijridate
233 .. _tzlocal: https://pypi.python.org/pypi/tzlocal
234 .. _ruamel.yaml: https://pypi.python.org/pypi/ruamel.yaml
235
236 Supported languages and locales
237 ===============================
238 You can check the supported locales by visiting the ":ref:`supported-locales`" section.
239
240
241 Supported Calendars
242 ===================
243
244 Apart from the Georgian calendar, `dateparser` supports the `Persian Jalali calendar` and the `Hijri/Islami calendar`
245
246 To be able to use them you need to install the `calendar` extra by typing:
247
248 pip install dateparser[calendars]
249
250
251 * Example using the `Persian Jalali calendar`. For more information, refer to `Persian Jalali Calendar <https://en.wikipedia.org/wiki/Iranian_calendars#Zoroastrian_calendar>`_.
252
253 >>> from dateparser.calendars.jalali import JalaliCalendar
254 >>> JalaliCalendar('جمعه سی ام اسفند ۱۳۸۷').get_date()
255 DateData(date_obj=datetime.datetime(2009, 3, 20, 0, 0), period='day', locale=None)
256
257
258 * Example using the `Hijri/Islamic Calendar`. For more information, refer to `Hijri Calendar <https://en.wikipedia.org/wiki/Islamic_calendar>`_.
259
260 >>> from dateparser.calendars.hijri import HijriCalendar
261 >>> HijriCalendar('17-01-1437 هـ 08:30 مساءً').get_date()
262 DateData(date_obj=datetime.datetime(2015, 10, 30, 20, 30), period='day', locale=None)
263
264 .. note:: `HijriCalendar` only works with Python ≥ 3.7.