View Source Personnummer (Personnummer v3.0.1)

Validate Swedish personal identity numbers Personnummer.

Link to this section Summary

Functions

Formats a personal identity number in short format.

Formats a personal identity number in long format.

Get the age of the person holding the personal identity number.

Returns true if the parsed personal identity number is a coordination number.

Returns true if the person behind the personal identity number is a female.

Returns true if the person behind the personal identity number is a male.

Calculate luhn checksum according to spec https://en.wikipedia.org/wiki/Luhn_algorithm.

Construct a new Personnummer struct.

Checks if the personal identity number is valid. Requres a valid date and a valid last four digits.

Link to this section Functions

Formats a personal identity number in short format.

examples

Examples

iex> {_, p} = Personnummer.new("199001011234")
iex> Personnummer.format(p)
"900101-1234"
iex> {_, p} = Personnummer.new("199001010001")
iex> Personnummer.format(p)
"900101-0001"
iex> {_, p} = Personnummer.new("199001610001")
iex> Personnummer.format(p)
"900161-0001"

Formats a personal identity number in long format.

examples

Examples

iex> {_, p} = Personnummer.new("9001011234")
iex> Personnummer.format(p, true)
"19900101-1234"
iex> Personnummer.format(p, false)
"900101-1234"

Get the age of the person holding the personal identity number.

examples

Examples

iex> now = DateTime.utc_now()
iex> {_, x} = Date.new(now.year - 20, now.month, now.day)
iex> pnr = "#{x.year}0101-1234"
iex> {_, p} = Personnummer.new(pnr)
iex> Personnummer.get_age(p)
20
Link to this function

is_coordination_number(pnr)

View Source

Returns true if the parsed personal identity number is a coordination number.

examples

Examples

iex> {_, p} = Personnummer.new("800161-3294")
iex> Personnummer.is_coordination_number(p)
true

Returns true if the person behind the personal identity number is a female.

examples

Examples

iex> {_, p} = Personnummer.new("19090903-6600")
iex> Personnummer.is_female?(p)
true

Returns true if the person behind the personal identity number is a male.

examples

Examples

iex> {_, p} = Personnummer.new("19900101-0017")
iex> Personnummer.is_male?(p)
true

Calculate luhn checksum according to spec https://en.wikipedia.org/wiki/Luhn_algorithm.

examples

Examples

iex> Personnummer.luhn("900101001")
7

Construct a new Personnummer struct.

Examples

iex> Personnummer.new("19900101-0017")
{:ok,
 %Personnummer{
   control: 7,
   coordination: false,
   date: ~D[1990-01-01],
   separator: "-",
   serial: 1
 }}

Checks if the personal identity number is valid. Requres a valid date and a valid last four digits.

examples-for-persomnnummer-type

Examples (for Persomnnummer type)

iex> p = %Personnummer{}
iex> Personnummer.valid?(p)
false
iex> {_, p} = Personnummer.new("19900101-0017")
iex> Personnummer.valid?(p)
true
iex> {_, p} = Personnummer.new("19900101-0018")
iex> Personnummer.valid?(p)
false

examples-for-string

Examples (for string)

iex> Personnummer.valid?("19900101-0017")
true
iex> Personnummer.valid?("19900101-0019")
false
iex> Personnummer.valid?("bogus")
false
iex> Personnummer.valid?("903030-0017")
false