Skip to content
Snippets Groups Projects
Commit 171626906013 authored by Brock Mendel's avatar Brock Mendel
Browse files

Fix for B.Y.d format corner case #687

Add changelog file
parent 5e875cf83bef
No related branches found
No related tags found
No related merge requests found
Fixed incorrect parsing of certain dates earlier than 100 AD when repesented in the form "%B.%Y.%d", e.g. "December.0031.30". (gh issue #687, pr #700)
...@@ -458,7 +458,29 @@ ...@@ -458,7 +458,29 @@
raise ValueError('Year is already set') raise ValueError('Year is already set')
self.ystridx = len(self) - 1 self.ystridx = len(self) - 1
def _resolve_from_stridxs(self):
"""
Try to resolve the identities of year/month/day elements using
ystridx, mstridx, and dstridx, if enough of these are specified.
"""
strids = {'y': self.ystridx, 'm': self.mstridx, 'd': self.dstridx}
strids = {key: strids[key] for key in strids if strids[key] is not None}
if len(self) == 3 and len(strids) == 2:
# we can back out the remaining stridx value
missing = [x for x in range(3) if x not in strids.values()]
key = [x for x in ['y', 'm', 'd'] if x not in strids]
assert len(missing) == len(key) == 1
key = key[0]
val = missing[0]
strids[key] = val
assert len(self) == len(strids) # otherwise this should not be called
out = {'y': None, 'm': None, 'd': None}
out.update({key: self[strids[key]] for key in strids})
return (out['y'], out['m'], out['d'])
def resolve_ymd(self, yearfirst, dayfirst): def resolve_ymd(self, yearfirst, dayfirst):
len_ymd = len(self) len_ymd = len(self)
year, month, day = (None, None, None) year, month, day = (None, None, None)
...@@ -461,7 +483,13 @@ ...@@ -461,7 +483,13 @@
def resolve_ymd(self, yearfirst, dayfirst): def resolve_ymd(self, yearfirst, dayfirst):
len_ymd = len(self) len_ymd = len(self)
year, month, day = (None, None, None) year, month, day = (None, None, None)
strids = {'y': self.ystridx, 'm': self.mstridx, 'd': self.dstridx}
strids = {key: strids[key] for key in strids if strids[key] is not None}
if (len(self) == len(strids) > 0 or
(len(self) == 3 and len(strids) == 2)):
return self._resolve_from_stridxs()
mstridx = self.mstridx mstridx = self.mstridx
if len_ymd > 3: if len_ymd > 3:
...@@ -470,6 +498,10 @@ ...@@ -470,6 +498,10 @@
# One member, or two members with a month string # One member, or two members with a month string
if mstridx is not None: if mstridx is not None:
month = self[mstridx] month = self[mstridx]
del self[mstridx] # since mstridx is 0 or 1, self[mstridx-1] always
# looks up the other element
other = self[mstridx - 1]
else:
other = self[0]
if len_ymd > 1 or mstridx is None: if len_ymd > 1 or mstridx is None:
...@@ -474,5 +506,5 @@ ...@@ -474,5 +506,5 @@
if len_ymd > 1 or mstridx is None: if len_ymd > 1 or mstridx is None:
if self[0] > 31: if other > 31:
year = self[0] year = other
else: else:
...@@ -478,5 +510,5 @@ ...@@ -478,5 +510,5 @@
else: else:
day = self[0] day = other
elif len_ymd == 2: elif len_ymd == 2:
# Two members with numbers # Two members with numbers
......
...@@ -1106,3 +1106,9 @@ ...@@ -1106,3 +1106,9 @@
# constructed with an invalid value # constructed with an invalid value
with pytest.raises(ValueError): with pytest.raises(ValueError):
parse(value) parse(value)
def test_BYd_corner_case():
# GH#687
res = parse('December.0031.30')
assert res == datetime(31, 12, 30)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment