Skip to content

Validate, parse, and generate Swedish Personal Identity Numbers (PINs, "Personnummer")

License

Notifications You must be signed in to change notification settings

Mange/swedish-pin-ruby

Repository files navigation

swedish-pin

Inline docs

Validate, parse, and generate Swedish Personal Identity Numbers ("PINs", or Personnummer).

API documentation

Installation

Add this to your Gemfile

gem 'swedish-pin'

Then run bundle install.

Usage

require "swedish_pin"

# Validate strings
SwedishPIN.valid?("8507099805") # => true
SwedishPIN.valid?("8507099804") # => false

# Parse numbers to get more information about them, or to normalize display of
# them.
pin = SwedishPIN.parse("8507099805") # => #<SwedishPIN::Personnummer …>
pin.year # => 1985
pin.birthdate # => #<Date: 1985-07-09>

# The 10-digit variant also knows about century separators.
pin.to_s     # => "850709-9805"
pin.to_s(10) # => "850709-9805"
pin.format_short(Date.civil(2025, 12, 1)) # => "850709-9805"
pin.format_short(Date.civil(2085, 12, 1)) # => "850709+9805"

# Use unofficial 12-digit format for a stable string that doesn't change
# depending on today's date when storing it.
pin.to_s(12)    # => "19850709-9805"
pin.format_long # => "19850709-9805"

# You can also generate numbers to use as example data
fake1 = SwedishPIN.generate
fake2 = SwedishPIN.generate(user.birthday)

License

MIT. See LICENSE file for more details.

This project started out as a fork of personnummer/ruby, but has since been almost completely rewritten. Despite this, the original authors retains most of the copyright since this is derivative work.

About

Validate, parse, and generate Swedish Personal Identity Numbers (PINs, "Personnummer")

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages