lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.11-38-g5441ffa

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.11-38-g5441ffa



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  5441ffada1a8ff6e860f5675264d40bfbb470820 (commit)
      from  9c18ac53c339a7c643b83c9ca62c59421775df3f (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=5441ffada1a8ff6e860f5675264d40bfbb470820

commit 5441ffada1a8ff6e860f5675264d40bfbb470820
Author: Amin Solhizadeh <amin.solhizadeh@oneflow.com>
Date:   Wed Oct 9 13:21:35 2019 +0200

    Handle - and + sign correctly in Swedish Personnummer
    
    For people aged 100 and up, the minus/dash in the personnummer is
    changed to a plus, on new year's eve the year they turn 100. See
    Folkbokföringslagen (1991:481), §18.
    
    This makes the - or + sign part of the number.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/156
    Closes https://github.com/arthurdejong/python-stdnum/pull/160

diff --git a/stdnum/se/personnummer.py b/stdnum/se/personnummer.py
index 1435e59..a495242 100644
--- a/stdnum/se/personnummer.py
+++ b/stdnum/se/personnummer.py
@@ -31,7 +31,9 @@ More information:
 * https://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)
 
 >>> validate('880320-0016')
-'8803200016'
+'880320-0016'
+>>> validate('8803200016')
+'880320-0016'
 >>> validate('880320-0018')
 Traceback (most recent call last):
     ...
@@ -54,7 +56,10 @@ from stdnum.util import clean, isdigits
 def compact(number):
     """Convert the number to the minimal representation. This strips the
     number of any valid separators and removes surrounding whitespace."""
-    return clean(number, ' -+:')
+    number = clean(number, ' :')
+    if len(number) in (10, 12) and number[-5] not in '-+':
+        number = '%s-%s' % (number[:-4], number[-4:])
+    return number
 
 
 def get_birth_date(number):
@@ -63,13 +68,18 @@ def get_birth_date(number):
     Note that it may be 100 years off because the number has only the last
     two digits of the year."""
     number = compact(number)
-    if len(number) == 12:
+    if len(number) == 13:
         year = int(number[0:4])
         month = int(number[4:6])
         day = int(number[6:8])
     else:
         year = datetime.date.today().year
-        year = year - (year - int(number[0:2])) % 100
+        century = year // 100
+        if int(number[0:2]) > year % 100:
+            century -= 1
+        if number[-5] == '+':
+            century -= 1
+        year = int('%d%s' % (century, number[0:2]))
         month = int(number[2:4])
         day = int(number[4:6])
     try:
@@ -90,12 +100,15 @@ def get_gender(number):
 def validate(number):
     """Check if the number is a valid identity number."""
     number = compact(number)
-    if len(number) not in (10, 12):
+    if len(number) not in (11, 13):
         raise InvalidLength()
-    if not isdigits(number):
+    if number[-5] not in '-+':
+        raise InvalidFormat()
+    digits = clean(number, '-+')
+    if not isdigits(digits):
         raise InvalidFormat()
     get_birth_date(number)
-    luhn.validate(number[-10:])
+    luhn.validate(digits[-10:])
     return number
 
 
@@ -109,5 +122,4 @@ def is_valid(number):
 
 def format(number):
     """Reformat the number to the standard presentation format."""
-    number = compact(number)
-    return number[:6] + '-' + number[6:]
+    return compact(number)
diff --git a/tests/test_se_personnummer.doctest 
b/tests/test_se_personnummer.doctest
index 0e57de1..2238e63 100644
--- a/tests/test_se_personnummer.doctest
+++ b/tests/test_se_personnummer.doctest
@@ -32,6 +32,10 @@ Test for non-digit number.
 Traceback (most recent call last):
     ...
 InvalidFormat: ...
+>>> personnummer.validate('a' * 11)
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
 
 
 These numbers should be detected as male or female.
@@ -49,6 +53,12 @@ rejected.
 datetime.date(1988, 3, 20)
 >>> personnummer.get_birth_date('191705120424')
 datetime.date(1917, 5, 12)
+>>> personnummer.get_birth_date('121212-1212')
+datetime.date(2012, 12, 12)
+>>> personnummer.get_birth_date('121212+1212')
+datetime.date(1912, 12, 12)
+>>> personnummer.get_birth_date('400606+5827')
+datetime.date(1840, 6, 6)
 >>> personnummer.validate('8899200425')
 Traceback (most recent call last):
     ...

-----------------------------------------------------------------------

Summary of changes:
 stdnum/se/personnummer.py          | 30 +++++++++++++++++++++---------
 tests/test_se_personnummer.doctest | 10 ++++++++++
 2 files changed, 31 insertions(+), 9 deletions(-)


hooks/post-receive
-- 
python-stdnum