Skip to content

Commit

Permalink
AB#112: Fixed a bug where Age returned 1 year less than expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat-Riz authored and Johannestegner committed Jan 9, 2025
1 parent 11ae784 commit dddc78f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,15 @@ public void TestParseTooShort()
}).Message
);
}

[Fact]
public void TestEdgeCasesAroundBirthday()
{
var timeProvider = new TestTimeProvider(); //TestTime is 2025-10-05
Assert.Equal(18, new Personnummer("20071004-3654", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
Assert.Equal(18, new Personnummer("20071005-3653", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
Assert.Equal(17, new Personnummer("20071006-3652", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
}

}
}
5 changes: 4 additions & 1 deletion Personnummer/Personnummer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public int Age
var now = _options.DateTimeNow;
var age = now.Year - Date.Year;

if (now.Month >= Date.Month && now.Day >= Date.Day)
var hadBirthdayThisYear = (now.Month > Date.Month) ||
(now.Month == Date.Month && now.Day >= Date.Day);

if (!hadBirthdayThisYear)
{
age--;
}
Expand Down

0 comments on commit dddc78f

Please sign in to comment.