Skip to content

Instantly share code, notes, and snippets.

@silverdr
Last active November 27, 2016 20:43
Show Gist options
  • Save silverdr/4bdcb36abc4791a377d346584a638a4e to your computer and use it in GitHub Desktop.
Save silverdr/4bdcb36abc4791a377d346584a638a4e to your computer and use it in GitHub Desktop.
#!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