Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] Age check error #115

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Personnummer.Tests/CoordinationNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ public void TestParseInvalidCn(PersonnummerData ssn)
public void TestAgeCn(PersonnummerData ssn)
{
var timeProvider = new TestTimeProvider();
var localNow = timeProvider.GetLocalNow();

int day = int.Parse(ssn.LongFormat.Substring(ssn.LongFormat.Length - 6, 2)) - 60;
string strDay = day < 10 ? $"0{day}" : day.ToString();

DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 6) + strDay, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
int years = timeProvider.GetLocalNow().Year - dt.Year;
var years = localNow.Year - dt.Year;
if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day)))
{
years--;
}

Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);
Expand Down
22 changes: 21 additions & 1 deletion Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,35 @@ public void TestParseInvalid(PersonnummerData ssn)
public void TestAge(PersonnummerData ssn)
{
var timeProvider = new TestTimeProvider();
var localNow = timeProvider.GetLocalNow();
DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 4), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
int years = timeProvider.GetLocalNow().Year - dt.Year;

var years = localNow.Year - dt.Year;
if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day)))
{
years--;
}

Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
// Du to age not being possible to fetch from >100 year short format without separator, we aught to check this here.
Assert.Equal(years > 99 ? years - 100 : years, Personnummer.Parse(ssn.ShortFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
}

[Fact]
public void TestEdgeCasesAroundBirthday()
{
var timeProvider = new TestTimeProvider
{
Now = DateTimeOffset.Parse("2090-01-09 12:00")
};

Assert.Equal(10, new Personnummer("20800108-6670", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
Assert.Equal(10, new Personnummer("20800109-8287", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
Assert.Equal(9, new Personnummer("20800110-8516", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
}

#endif

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion Personnummer.Tests/TestTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Personnummer.Tests;
public class TestTimeProvider : TimeProvider
{
internal DateTimeOffset Now { get; set; } = new(
new DateOnly(2025, 1, 5),
new DateOnly(2025, 1, 1),
new TimeOnly(0, 0, 0, 1),
TimeSpan.Zero
);
Expand Down
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
Loading