Skip to content

Commit 8344a05

Browse files
dannyhajjJohannestegner
authored andcommitted
Validate leap year dates correctly
1 parent 85154b7 commit 8344a05

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

personnummer/personnummer.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, ssn, options=None):
2323
options = {}
2424

2525
self.options = options
26+
self._ssn = ssn
2627
self.parts = self.get_parts(ssn)
2728

2829
if self.valid() is False:
@@ -80,7 +81,11 @@ def is_male(self):
8081
return int(gender_digit) % 2 != 0
8182

8283
def is_coordination_number(self):
83-
return test_date(int(self.parts['year']), int(self.parts['month']), int(self.parts['day']) - 60)
84+
return test_date(
85+
int(self.parts['century'] + self.parts['year']),
86+
int(self.parts['month']),
87+
int(self.parts['day']) - 60,
88+
)
8489

8590
@staticmethod
8691
def get_parts(ssn):
@@ -133,6 +138,7 @@ def valid(self):
133138
:return:
134139
"""
135140

141+
century = self.parts['century']
136142
year = self.parts['year']
137143
month = self.parts['month']
138144
day = self.parts['day']
@@ -144,10 +150,10 @@ def valid(self):
144150

145151
is_valid = luhn(year + month + day + num) == int(check)
146152

147-
if is_valid and test_date(int(year), int(month), int(day)):
153+
if is_valid and test_date(int(century + year), int(month), int(day)):
148154
return True
149155

150-
return is_valid and test_date(int(year), int(month), int(day) - 60)
156+
return is_valid and test_date(int(century + year), int(month), int(day) - 60)
151157

152158

153159
def luhn(data):
@@ -210,14 +216,8 @@ def test_date(year, month, day):
210216
"""
211217
Test if the input parameters are a valid date or not
212218
"""
213-
for x in ['19', '20']:
214-
new_y = x.__str__() + year.__str__()
215-
new_y = int(new_y)
216-
try:
217-
date = datetime.date(new_y, month, day)
218-
if date.year == new_y and date.month == month and date.day == day:
219-
return True
220-
except ValueError:
221-
continue
222-
223-
return False
219+
try:
220+
date = datetime.date(year, month, day)
221+
return date.year == year and date.month == month and date.day == day
222+
except ValueError:
223+
return False

0 commit comments

Comments
 (0)