4
def id_number_checksum(gd):
6
Calculates a Swedish ID number checksum, using the
10
for c in (gd['year'] + gd['month'] + gd['day'] + gd['serial']):
11
tmp = ((n % 2) and 1 or 2) * int(c)
14
tmp = sum([int(i) for i in str(tmp)])
22
return (((s / 10) + 1) * 10) - s
24
def validate_id_birthday(gd, fix_coordination_number_day=True):
26
Validates the birth_day and returns the datetime.date object for
29
If the date is an invalid birth day, a ValueError will be raised.
32
today = datetime.date.today()
35
if fix_coordination_number_day and day > 60:
38
if gd['century'] is None:
40
# The century was not specified, and need to be calculated from todays date
41
current_year = today.year
42
year = int(today.strftime('%Y')) - int(today.strftime('%y')) + int(gd['year'])
44
if ('%s%s%02d' % (gd['year'], gd['month'], day)) > today.strftime('%y%m%d'):
47
# If the person is older than 100 years
51
year = int(gd['century'] + gd['year'])
53
# Make sure the year is valid
54
# There are no swedish personal identity numbers where year < 1800
58
# ValueError will be raise for invalid dates
59
birth_day = datetime.date(year, int(gd['month']), day)
61
# birth_day must not be in the future
67
def format_personal_id_number(birth_day, gd):
68
# birth_day.strftime cannot be used, since it does not support dates < 1900
69
return unicode(str(birth_day.year) + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
71
def format_organisation_number(gd):
72
if gd['century'] is None:
75
century = gd['century']
77
return unicode(century + gd['year'] + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
79
def valid_organisation(gd):
80
return gd['century'] in (None, 16) and \
81
int(gd['month']) >= 20 and \
82
gd['sign'] in (None, '-') and \
83
gd['year'][0] in ('2', '5', '7', '8', '9') # group identifier