Testing Finnish National identification number (SOTU)
Here’s a sample function to test the validity of Finnish National identification number (henkilötunnus (HETU)), formerly known as sosiaaliturvatunnus (SOTU)).
function IsValidSOTU(const SOTU: string): Boolean;
const
chars = '0123456789ABCDEFHJKLMNPRSTUVWXY';
var
n: integer;
c: char;
d: TDate;
century, year, month, day: word;
begin
Result := False;
try
// check length
if Length(SOTU) <> 11 then
Exit;
// check century
case SOTU[7] of
'+': century := 1800;
'-': century := 1900;
'A': century := 2000;
else
Exit;
end;
// check if birthday is a valid date
// get a date from SOTU
d := EncodeDate(
century + StrToInt(Copy(SOTU, 5, 2)),
StrToInt(Copy(SOTU, 3, 2)),
StrToInt(Copy(SOTU, 1, 2))
);
// separate the date
DecodeDate(d, year, month, day);
// check if the values are the same
// (i.e. 30.02 won't change to 01.03)
if Format('%2.2d%2.2d%2.2d', [day, month, year - century])
<> Copy(SOTU, 1, 6) then
Exit;
// try getting birthday + PIN as one integer
n := StrToInt(Copy(SOTU, 1, 6) + Copy(SOTU, 8, 3));
// odd PIN is used for males, even PIN for females
// calculate checksum
c := chars[n mod 31 + 1];
// check the calculated checksum against the one in SOTU
Result := (c = SOTU[11]);
except
// some conversion went wrong
Result := False;
Exit;
end;
end;
Explore posts in the same categories: delphi, string
November 17, 2008 at 10:18 am
I found that Free Pascal is screaming when I try to compile:
hetu.pas(7,11) Error: Identifier not found “TDate”
hetu.pas(7,11) Error: Error in type definition
hetu.pas(11,10) Error: Identifier not found “Result”
hetu.pas(16,5) Error: Identifier not found “try”
hetu.pas(16,5) Fatal: Syntax error, “;” expected but “IF” found
hetu.pas(0) Fatal: Compilation aborted
November 17, 2008 at 10:33 am
hahaha, nevermind now I realized how stooopid I am