Skip to content

Commit 77e4340

Browse files
authored
Merge pull request #84 from itk-dev-rpa/feature/cpr-birth-date
Added get birth date function
2 parents f6ce798 + 582d7b5 commit 77e4340

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- misc.cpr_util: Function to get birth date from cpr number.
13+
1014
## [2.7.1] - 2024-09-24
1115

1216
### Fixed

itk_dev_shared_components/misc/cpr_util.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from datetime import date
44

55

6-
def get_age(cpr: str, current_date: date = date.today()) -> int:
7-
"""Get the age of a person based on their cpr number
6+
def get_birth_date(cpr: str) -> date:
7+
"""Get the birth date of a person based on their cpr number
88
using the 7th digit to infer the century.
99
1010
Args:
1111
cpr: The cpr number in the format 'ddmmyyxxxx'.
12-
current_date: The date where the age is calculated from. Defaults to the current date.
1312
1413
Returns:
15-
The age in integer years based on the cpr number.
14+
The birth date based on cpr number.
1615
1716
Raises:
1817
ValueError: If the given cpr is not in 'ddmmyyxxxx' format.
@@ -51,7 +50,24 @@ def get_age(cpr: str, current_date: date = date.today()) -> int:
5150
else:
5251
year += t[2]
5352

54-
birthdate = date(year, month, day)
53+
return date(year, month, day)
54+
55+
56+
def get_age(cpr: str, current_date: date = date.today()) -> int:
57+
"""Get the age of a person based on their cpr number
58+
using the 7th digit to infer the century.
59+
60+
Args:
61+
cpr: The cpr number in the format 'ddmmyyxxxx'.
62+
current_date: The date where the age is calculated from. Defaults to the current date.
63+
64+
Returns:
65+
The age in integer years based on the cpr number.
66+
67+
Raises:
68+
ValueError: If the given cpr is not in 'ddmmyyxxxx' format.
69+
"""
70+
birthdate = get_birth_date(cpr)
5571

5672
age = current_date.year - birthdate.year - ((current_date.month, current_date.day) < (birthdate.month, birthdate.day))
5773

tests/test_misc/test_cpr_util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ def test_get_age_from_cpr(self):
4343
with self.assertRaises(ValueError):
4444
cpr_util.get_age("010190xxxx")
4545

46+
def test_get_birth_date_from_cpr(self):
47+
"""Test getting the age from a cpr number."""
48+
# A table of fictional cpr-numbers and the expected birth dates.
49+
test_data = (
50+
("0101920000", date(1992, 1, 1)),
51+
("0101921000", date(1992, 1, 1)),
52+
("0101922000", date(1992, 1, 1)),
53+
("0101923000", date(1992, 1, 1)),
54+
("0101154000", date(2015, 1, 1)),
55+
("0101924000", date(1992, 1, 1)),
56+
("0101155000", date(2015, 1, 1)),
57+
("0101925000", date(1892, 1, 1)),
58+
("0101156000", date(2015, 1, 1)),
59+
("0101926000", date(1892, 1, 1)),
60+
("0101157000", date(2015, 1, 1)),
61+
("0101927000", date(1892, 1, 1)),
62+
("0101158000", date(2015, 1, 1)),
63+
("0101928000", date(1892, 1, 1)),
64+
("0101159000", date(2015, 1, 1)),
65+
("0101929000", date(1992, 1, 1)),
66+
)
67+
68+
for cpr, birth_date in test_data:
69+
with self.subTest(cpr=cpr, birth_date=birth_date):
70+
self.assertEqual(cpr_util.get_birth_date(cpr), birth_date)
71+
72+
with self.assertRaises(ValueError):
73+
cpr_util.get_birth_date("010190-1234")
74+
75+
with self.assertRaises(ValueError):
76+
cpr_util.get_birth_date("010190xxxx")
77+
4678

4779
if __name__ == '__main__':
4880
unittest.main()

0 commit comments

Comments
 (0)