Skip to content

Commit

Permalink
Merge pull request #1 from personnummer/format-coordination-number
Browse files Browse the repository at this point in the history
Format coordination numbers correct
  • Loading branch information
frozzare authored Apr 20, 2020
2 parents baaba0a + fd95168 commit 45cfebe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
16 changes: 7 additions & 9 deletions lib/Personnummer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use feature qw( say );
use Carp;
use DateTime;

our $VERSION = '0.01';
our $VERSION = '3.00';

sub new {
my ( $class, $pnr ) = @_;
Expand All @@ -33,14 +33,10 @@ sub format {
croak "cannot format invalid social security numbers" if not $self->valid();

my $year = $long_format ? $self->{date}->year : $self->{date}->year % 100;
my $day = $self->{is_coordination_number} ? $self->{date}->day + 60 : $self->{date}->day;

return sprintf(
"%s%02d%02d-%03d%d",
$year,
$self->{date}->month,
$self->{date}->day,
$self->{serial}, $self->{control}
);
return
sprintf( "%s%02d%02d-%03d%d", $year, $self->{date}->month, $day, $self->{serial}, $self->{control} );
}

sub get_age {
Expand Down Expand Up @@ -105,7 +101,9 @@ sub _luhn {
$even ^= 1;
}

return 10 - ( $sum % 10 );
my $checksum = 10 - ( $sum % 10 );

return $checksum == 10 ? 0 : $checksum;
}

sub _parse {
Expand Down
5 changes: 5 additions & 0 deletions t/00-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ subtest 'Formatting' => sub {
short => "900101-0017",
long => "19900101-0017"
},
{
input => "900161-0017",
short => "900161-0017",
long => "19900161-0017"
},
);

foreach my $tc ( @cases ) {
Expand Down
4 changes: 2 additions & 2 deletions t/10-validate.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use Personnummer;

subtest 'Valid social security numbers' => sub {
my @cases = (
"20160229-1237", "6403273813", "19900101-0017", "196408233234",
"000101-0107", "510818-9167", "19130401+2931", "0001010107"
"20160229-1237", "6403273813", "19900101-0017", "196408233234", "000101-0107", "510818-9167",
"19130401+2931", "0001010107", "19090903-6600",
);

foreach my $tc ( @cases ) {
Expand Down

0 comments on commit 45cfebe

Please sign in to comment.