8000 Compute birth date from Belgian National Number · unho/python-stdnum@bda2a9c · GitHub
[go: up one dir, main page]

Skip to content

Commit bda2a9c

Browse files
cedkarthurdejong
authored andcommitted
Compute birth date from Belgian National Number
Closes arthurdejong#288
1 parent e2a2774 commit bda2a9c

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

stdnum/be/nn.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf-8
22
# nn.py - function for handling Belgian national numbers
33
#
4-
# Copyright (C) 2021 Cédric Krier
4+
# Copyright (C) 2021-2022 Cédric Krier
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -39,6 +39,8 @@
3939
InvalidChecksum: ...
4040
>>> format('85073003328')
4141
'85.07.30-033.28'
42+
>>> get_birth_date('85.07.30-033 28')
43+
datetime.date(1985, 7, 30)
4244
"""
4345

4446
import datetime
@@ -55,13 +57,13 @@ def compact(number):
5557

5658

5759
def _checksum(number):
58-
"""Calculate the checksum."""
60+
"""Calculate the checksum and return the detected century."""
5961
numbers = [number]
6062
if int(number[:2]) + 2000 <= datetime.date.today().year:
6163
numbers.append('2' + number)
62-
for n in numbers:
64+
for century, n in zip((19, 20), numbers):
6365
if 97 - (int(n[:-2]) % 97) == int(n[-2:]):
64-
return True
66+
return century
6567
return False
6668

6769

@@ -91,3 +93,16 @@ def format(number):
9193
return (
9294
'.'.join(number[i:i + 2] for i in range(0, 6, 2)) +
9395
'-' + '.'.join([number[6:9], number[9:11]]))
96+
97+
98+
def get_birth_date(number):
99+
"""Return the date of birth"""
100+
number = compact(number)
101+
century = _checksum(number)
102+
if not century:
103+
raise InvalidChecksum()
104+
try:
105+
return datetime.datetime.strptime(
106+
str(century) + number[:6], '%Y%m%d').date()
107+
except ValueError:
108+
raise InvalidComponent()

tests/test_be_nn.doctest

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
test_be_nn.doctest - more detailed doctests for stdnum.be.nn module
2+
3+
Copyright (C) 2022 Arthur de Jong
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.be.nn module. It
22+
tries to test more corner cases and detailed functionality that is not
23+
really useful as module documentation.
24+
25+
>>> from stdnum.be import nn
26+
27+
28+
Extra tests for getting birth date
29+
30+
31+
>>> nn.get_birth_date('85.07.30-033 28')
32+
datetime.date(1985, 7, 30)
33+
>>> nn.get_birth_date('17 07 30 033 84')
34+
datetime.date(2017, 7, 30)
35+
>>> nn.get_birth_date('12345678901')
36+
Traceback (most recent call last):
37+
...
38+
InvalidChecksum: ...
39+
>>> nn.get_birth_date('00 00 01 003-64') # 2000-00-00 is not a valid date
40+
Traceback (most recent call last):
41+
...
42+
InvalidComponent: ...

0 commit comments

Comments
 (0)
0