Skip to content

Instantly share code, notes, and snippets.

@cth
Created February 24, 2016 06:58
Show Gist options
  • Save cth/11120b3993a1a71386fe to your computer and use it in GitHub Desktop.
Save cth/11120b3993a1a71386fe to your computer and use it in GitHub Desktop.
# A datatype of storing Danish CPR numbers and accommpanying function for converversion to/from strings
type CPR
date::Date
serialnumber::UInt16
end
Base.string(x::CPR) = string( dec(Integer(Dates.Day(x.date)),2), dec(Integer(Dates.Month(x.date)),2), dec(Integer(Dates.Year(x.date)) % 100), "-", dec(x.serialnumber, 4))
ismale(x::CPR) = x.serialnumber % 2 == 1
function convert(::Type{CPR}, x::ASCIIString)
matches = match(r"(\d{2})(\d{2})(\d{2})-?(\d{4})", x)
seventh_digit = parse(UInt8,matches[4][1])
year2 = parse(UInt8,matches[3])
if seventh_digit <= 3 || (seventh_digit == 4 && year2 > 36) || (seventh_digit == 9 && year2 > 36)
year_str = string("19", matches[3])
elseif (seventh_digit == 4 && twodigit_year < 37) || (seventh_digit >= 5 && seventh_digit <= 8 && year2 < 37) || (seventh_digit == 9 && year2 < 37)
year_str = string("20", matches[3])
elseif (seventh_digit >= 5 && seventh_digit <= 8 && year2 >= 58)
year_str = string("18", matches[3])
else
error("invalid seventh digit!")
end
CPR(Date(string(matches[1],matches[2],year_str), "ddmmyyyy"), parse(UInt16, matches[4]))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment