Im trying to use parseutil.parser to parse a date in a string.
When the date is complete there are no problems:
I was born October 11th 2002 -> date(2002, 10, 11)
But when it is an incomplete date, parseutil.parser will try to "autofill" it with todays date, like this:
I need documents from 2003 -> date(2003, 2, 23) (today's month and year)
What I want is something like this
I need documents from 2003 -> date(2003, None, None)
I tried to pass "default=date(None, None, None)" to the parser, but datetime fields cannot be None, so that solution is impossible. I could pass a default date, but that date will be valid 1 day of the year, so there is no way to tell later on if it was an incomplete parsing or a sucessful one.
"Why do you need to do this?"
Because the dates that are incomplete I need to turn them into intervals, so:
I need documents from 2003 -> date(2003, 1, 1), date(2003, 12, 31)
And the dates that are matched by the month and year are also turned into intervals but smaller:
I need documents from March 2003 -> date(2003, 3, 1), date(2003, 3, 31)
Right now I cannot really do this, because after I call parseutil.parser.parse() I do not know if it was a complete match or a partial match, nor do I know the fields that were matched.
There is a way to accomplish this by using
parse()twice but with different default dates.The trick is to compare the year, month, date of the resulting dates. If they all match - return any of the parsed dates, otherwise - determine and return the intervals.
Output:
And there is no need to worry about
since all significant values of the default dates differ from each other.
Even if those dates are changed to
it will still produce a correct output.