parse() handling of strings containing only integers is inconsistent
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
dateutil |
New
|
Undecided
|
Unassigned |
Bug Description
We have a set of strings from data and we're trying to test to see if they are dates or something else. Code looks something like
try:
dt = parser.
return dt
except:
# not a date, does something else
unfortunately finding a string such as "113" returns inconsistently. In this case I had string "113" and I got back
datetime(113, 12, 5, 0, 0)
(12/5 is today's date)
I can see how this would sort of be a feature but different integer/strings do different things
One and two digits affect the day only:
parser.parse("1") - returns datetime(2014, 12, 1, 0, 0)
parser.parse("10") - returns datetime(2014, 12, 10, 0, 0)
3 digits affects the year, as above. As does 4 digits:
parser.parse( "113" ) - returns datetime(113, 12, 5, 0, 0)
parser.
5 digits
parser.
6 digits
parser.
7 digits
parser.
8 digits
parser.
In my opinion 8 digits makes sense but everything else is extremely ambiguous and should probably throw an exception.
However there's another gotcha with even with an 8 digit date. The resulting datetime.strftime() throws a exception that years must be less than 1900
Alternatively I propose such strings be parsed consistently by looking for a year first, followed by a month, followed by days. Rather than falling back to today's date for missing values, fall back to the first day or month that would be valid. Any year less than 1900 should be discarded.
Examples:
parser.parse("0") - datetime(2000,1, 1, 0, 0)
parser.parse("10") - datetime(2010, 1, 1, 0, 0)
parser.parse("100") - ValueError - year out of range
parser.
parser.
parser.
# next 1 or 2 digits considered month
parser.
parser.
parser.
# Next 1 or 2 digits considered date
parser.
parser.
parser.
I could work up a patch if this seems sane.
A more 'strict' parsing behavior is very welcome, if you can help with a patch. Please note that the development activity officially moved to https:/ /github. com/dateutil/ dateutil