summaryrefslogtreecommitdiff
path: root/dateutil/parser/_parser.py
diff options
context:
space:
mode:
authorCorey <corey.r.girard@gmail.com>2018-04-15 12:18:25 -0400
committerPaul Ganssle <paul@ganssle.io>2018-04-15 12:18:25 -0400
commit9e81954d43287d64abcb0e84fd5adbdb9e47477e (patch)
treeab2a579e45b3de6d9be0243f33b61a7dbfec3a84 /dateutil/parser/_parser.py
parent77122ac1131a55b9208b99bad7cc9f8e4033e99e (diff)
downloaddateutil-git-9e81954d43287d64abcb0e84fd5adbdb9e47477e.tar.gz
Refactor parserinfo.convertyear
Added assertion that year is always positive. Co-authored-by: Lauren Oldja <loldja@users.noreply.github.com>
Diffstat (limited to 'dateutil/parser/_parser.py')
-rw-r--r--dateutil/parser/_parser.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/dateutil/parser/_parser.py b/dateutil/parser/_parser.py
index 8c2c547..c9af04c 100644
--- a/dateutil/parser/_parser.py
+++ b/dateutil/parser/_parser.py
@@ -364,13 +364,25 @@ class parserinfo(object):
return self.TZOFFSET.get(name)
def convertyear(self, year, century_specified=False):
- if year < 100 and not century_specified:
- year += self._century
- if abs(year - self._year) >= 50:
- if year < self._year:
- year += 100
- else:
- year -= 100
+ """
+ Converts two-digit years to year within [-50, 49]
+ range of self._year (current local time)
+ """
+
+ # only called internally. Should never be negative
+ assert year >= 0
+
+ if century_specified or year >= 100:
+ return year
+
+ # assume current century to start
+ year += self._century
+
+ if year >= self._year + 50: # if too far in future
+ year -= 100
+ elif year < self._year - 50: # if too far in past
+ year += 100
+
return year
def validate(self, res):