Skip to main content

slnic

Parse, validate, convert, and generate Sri Lankan NIC (National Identity Card) numbers in Python.

PyPI version Python versions License: MIT

A Sri Lankan NIC number encodes the holder's birth date and gender. slnic understands both the old 10-character format (853400123V) and the new 12-digit format (198534000123), and gives you a clean, typed API to work with them — zero dependencies.

Installation

pip install slnic

Quick start

import slnic

nic = slnic.parse("853400123V")

nic.birth_date      # datetime.date(1985, 12, 5)
nic.gender          # <Gender.MALE: 'male'>
nic.age()           # 40  (as of today)
nic.format          # <NICFormat.OLD: 'old'>
nic.voter_eligible  # True  ('V' suffix; 'X' → False; None for new format)
nic.serial          # '012'
nic.check_digit     # '3'

str(nic.to_new())   # '198534000123'

Validate

slnic.is_valid("853400123V")    # True
slnic.is_valid("858400123v")    # True  (case-insensitive, female)
slnic.is_valid("859990123V")    # False (invalid day-of-year segment)

# parse() raises InvalidNICError with a helpful message:
slnic.parse("870600012V")
# InvalidNICError: day of year 60 means 29 February, but 1987 is not a leap year

Convert between formats

old = slnic.parse("853400123V")
new = old.to_new()              # NIC('198534000123')

# New → old works when the birth year is 1900-1999 and the serial fits.
# The V/X letter was dropped in the new format, so supply it (default 'V'):
back = new.to_old(voter_eligible=True)   # NIC('853400123V')

Generate test data

from datetime import date
from slnic import generate

nic = generate(date(1992, 7, 14), gender="female", nic_format="new")
str(nic)   # e.g. '199269604823'

Note: the NIC checksum algorithm has never been published, so generated numbers are structurally valid (great for test fixtures and demos) but may not correspond to any real, issued NIC. For the same reason, parse() and is_valid() do not verify the check digit — no library can.

How NIC numbers work

Segment Old format (853400123V) New format (198534000123)
Birth year 85 → 1985 (always 19xx) 1985
Day of year 340 340
Serial 012 (3 digits) 0012 (4 digits)
Check digit 3 3
Voter letter V eligible / X not (dropped)
  • Gender: if the day-of-year segment is greater than 500, the holder is female — subtract 500 to get the actual day (e.g. 840 → day 340).
  • February always counts as 29 days in the NIC calendar, regardless of leap year. So 1 March is always day 61, and day 60 (29 Feb) is only valid for leap-year births.
  • Old → new conversion: prepend 19 to the year and left-pad the serial with 0.

API

Function / attribute Description
slnic.parse(s) -> NIC Parse either format; raises InvalidNICError
slnic.is_valid(s) -> bool Structural validity check
slnic.generate(birth_date, ...) -> NIC Build a structurally valid NIC (test data)
NIC.birth_date, .gender, .serial, .check_digit, .voter_eligible, .format, .number Parsed fields
NIC.birth_year, .day_of_year, .age(on=None) Derived values
NIC.to_new(), NIC.to_old(voter_eligible=True) Format conversion (raises ConversionError when impossible)

NIC is a frozen (immutable, hashable) dataclass, and the package ships type hints (py.typed).

Development

git clone https://github.com/RavinduPabasara/slnic
cd slnic
make install-dev
make test

Privacy note

NIC numbers are personal data. This library only decodes what the number format itself encodes; take care when logging or storing NICs in your own applications.

License

MIT — see LICENSE.

Author

Ravindu Pabasara Karunarathna — also the author of sinhaladate.

Release files for slnic 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slnic 0.1.0
File Size Uploaded
slnic-0.1.0.tar.gz 13.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slnic 0.1.0
File Interpreter ABI Platform
slnic-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.0 kB

Release files / slnic-0.1.0.tar.gz

Download URL slnic-0.1.0.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
efdfe25183c9828bb4ed8c76d5fdc4aceda5dac7f503e6cb24766e079d895009
BLAKE2b-256 checksum
How to use checksums
a625dec4d24f4af43c12ca32d53c8d0b8c1e71435d501edbb37e57af1c05b227
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release files / slnic-0.1.0-py3-none-any.whl

Download URL slnic-0.1.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8c2aa50a0994ce5b1dfe3ce577654a98dbb312115ec15a9fffbf56e07de55e8
BLAKE2b-256 checksum
How to use checksums
687c8ca4abbec1e21ae98f9e54a5a44d757fdf7d6210b9c89ab7feee09720da5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page