Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/id/nik.py
blob: 18cbb0d8411d73e8caa22b7846cf6ff22b4a8e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# nik.py - functions for handling Indonesian NIK numbers
# coding: utf-8
#
# Copyright (C) 2024 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""NIK (Nomor Induk Kependudukan, Indonesian identity number).


The Nomor Induk Kependudukan (NIK, Population Identification Number,
sometimes known as Nomor Kartu Tanda Penduduk or Nomor KTP) is issued to
Indonesian citizens.

The number consists of 16 digits in the format PPRRSSDDMMYYXXXX where PPRRSS
(province, city/district, sub-district) indicates the place of residence when
the number was issued. It is followed by a DDMMYY date of birth (for female
40 is added to the day). The last 4 digits are used to make the number
unique.

More information:

* https://id.wikipedia.org/wiki/Nomor_Induk_Kependudukan

>>> validate('3171011708450001')
'3171011708450001'
>>> validate('31710117084500')
Traceback (most recent call last):
    ...
InvalidLength: ...
>>> validate('9971011708450001')  # invalid province
Traceback (most recent call last):
    ...
InvalidComponent: ...
>>> get_birth_date('3171015708450001')
datetime.date(1945, 8, 17)
>>> get_birth_date('3171012902001234')  # 1900-02-29 doesn't exist
datetime.date(2000, 2, 29)
>>> get_birth_date('3171013002001234')  # 1900-20-30 doesn't exist
Traceback (most recent call last):
    ...
InvalidComponent: ...
"""

import datetime

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


def compact(number):
    """Convert the number to the minimal representation.

    This strips the number of any valid separators and removes
    surrounding whitespace.
    """
    return clean(number, ' -.').strip()


def get_birth_date(number, minyear=1920):
    """Get the birth date from the person's NIK.

    Note that the number only encodes the last two digits of the year so
    this may be a century off.
    """
    number = compact(number)
    day = int(number[6:8]) % 40
    month = int(number[8:10])
    year = int(number[10:12])
    try:
        return datetime.date(year + 1900, month, day)
    except ValueError:
        pass
    try:
        return datetime.date(year + 2000, month, day)
    except ValueError:
        raise InvalidComponent()


def _check_registration_place(number):
    """Use the number to look up the place of registration of the person."""
    from stdnum import numdb
    results = numdb.get('id/loc').info(number[:4])[0][1]
    if not results:
        raise InvalidComponent()
    return results


def validate(number):
    """Check if the number is a valid Indonesian NIK."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 16:
        raise InvalidLength()
    get_birth_date(number)
    _check_registration_place(number)
    return number


def is_valid(number):
    """Check if the number is a valid Indonesian NIK."""
    try:
        return bool(validate(number))
    except ValidationError:
        return False