Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble in parsing date using dateutil

I am using python-dateutil for parsing a date from a string:

import dateutil.parser
print dateutil.parser.parse('some null string', fuzzy=True).date()
2012-10-18
print dateutil.parser.parse('some 31 Oct 2012 string', fuzzy=True).date()
2012-10-31

What I am expecting is for dateutil.parser.parse('some null string', fuzzy=True).date() to throw an exception, but it's returning the current date. Can someone show me how I can avoid getting the current date, if no date is found in the provided string?

Thanks in advance.

like image 589
akhter wahab Avatar asked Oct 18 '12 17:10

akhter wahab


People also ask

How do you parse a date in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.

Is dateutil included in Python 3?

Python 2. x has an extraordinary function called dateutil. parser which transforms an ISO8601 arranged date into a python DateTime value. It's absent in Python 3.

What is Python dateutil package?

The dateutil module supports the parsing of dates in any string format. This module provides internal up-to-date world time zone details. This module helps in computing the relative deltas. This module also helps in computing the dates based on pretty flexible rules of recurrence.

Is dateutil part of Python standard library?

The dateutil module specializes in providing an extension of features to the existing datetime module, and as such, the installation of the datetime module is a prerequisite. However, since it's a part of the Python standard library, there's nothing to worry about.


1 Answers

See the dateutil docs, specifically the parse function (emphasizes mine):

Additionally, the following keyword arguments are available:

default If given, this must be a datetime instance. Any fields missing in the parsed date will be copied from this instance. The default value is the current date, at 00:00:00am.

... (snip) ...

fuzzy If fuzzy is set to True, unknown tokens in the string will be ignored.

Given that you've set fuzzy to True, no exception will be thrown as it will simply ignore all unknown tokens. And, as the default argument is not passed, the current date will be returned.

So the solution will be to either keep fuzzy set to False, so that invalid format strings will throw an exception; or check if the returned datetime is equal to the current date at 00:00:00am as an indication of a failed conversion.

like image 161
jro Avatar answered Sep 22 '22 06:09

jro