Created
July 11, 2011 17:00
-
-
Save markusl/1076276 to your computer and use it in GitHub Desktop.
Finnish social security number calculator in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
/// Finnish social security number calculator in F# | |
/// Suomalainen henkilötunnuslaskuri F#:lla | |
module SSN = | |
let getSeparator year = | |
if year < 1900 then '+' | |
else if year < 2000 then '-' | |
else 'A' | |
let getNumber female = | |
let number = (new System.Random()).Next(2, 899) | |
if (number % 2 = 0 && not female) then number+1 | |
else if female && not (number % 2 = 0) then number+1 | |
else number | |
let getMark date number = | |
let marks = [|'0';'1';'2';'3';'4';'5';'6';'7';'8';'9';'A';'B';'C';'D';'E';'F'; | |
'H';'J';'K';'L';'M';'N';'P';'R';'S';'T';'U';'V';'W';'X';'Y'|] | |
let dateAndNumber = Int32.Parse(sprintf "%s%03i" date number) | |
marks.[dateAndNumber%31] | |
/// Get finnish social security number | |
/// date = birth date of a person | |
/// female = true if calculating number for a female | |
let getFinnishSSN (date:DateTime) female = | |
let separator = getSeparator date.Year | |
let number = getNumber female | |
let dateString = date.ToString("ddMMyy") | |
let mark = getMark dateString number | |
sprintf "%s%c%03i%c" dateString separator number mark | |
// Examples: | |
SSN.getFinnishSSN (DateTime.Parse("6.6.1992")) false;; | |
SSN.getFinnishSSN (DateTime.Parse("3.12.2002")) true;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment