Skip to content

Instantly share code, notes, and snippets.

@silverdr silverdr/decode_pesel.rb
Last active Nov 27, 2016

Embed
What would you like to do?
#!ruby
pesel = ARGV[0]
puts pesel
puts pesel[2]
puts pesel[0..1]
if /^[0-9]{11}/.match(pesel)
case pesel[2].to_i # first digit of month number encodes century
when 8, 9
century = 1800
month = pesel[2..3].to_i - 80
when 0, 1
century = 1900
month = pesel[2..3].to_i
when 2, 3
century = 2000
month = pesel[2..3].to_i - 20
when 4, 5
century = 2100
month = pesel[2..3].to_i - 40
when 6, 7
century = 2200
month = pesel[2..3].to_i - 60
end
year = century + pesel[0..1].to_i
day = pesel[4..5].to_i
gender = pesel[9].to_i.even? ? 0 : 1
control_digit_given = pesel[10].to_i
factors_array = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7] # for control digit calculation
control_digit_calculated = 0
pesel[0..9].split('').map.with_index{|digit, index| control_digit_calculated += digit.to_i * factors_array[index]}
control_digit_calculated %= 10
puts year
puts month
puts day
puts gender
puts control_digit_given
puts control_digit_calculated
else
puts 'error!'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.