Skip to content

Commit 165f249

Browse files
Updated tests to use 'real' age calculation.
Updated timeprovider to use 0101 as default date. Signed-off-by: Johannes Tegnér <[email protected]>
1 parent dddc78f commit 165f249

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

Personnummer.Tests/CoordinationNumberTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ public void TestParseInvalidCn(PersonnummerData ssn)
105105
public void TestAgeCn(PersonnummerData ssn)
106106
{
107107
var timeProvider = new TestTimeProvider();
108+
var localNow = timeProvider.GetLocalNow();
108109

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

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

115120
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);
116121
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);

Personnummer.Tests/PersonnummerTests.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,35 @@ public void TestParseInvalid(PersonnummerData ssn)
8282
public void TestAge(PersonnummerData ssn)
8383
{
8484
var timeProvider = new TestTimeProvider();
85+
var localNow = timeProvider.GetLocalNow();
8586
DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 4), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
86-
int years = timeProvider.GetLocalNow().Year - dt.Year;
87+
88+
var years = localNow.Year - dt.Year;
89+
if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day)))
90+
{
91+
years--;
92+
}
8793

8894
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
8995
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
9096
Assert.Equal(years, Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
9197
// Du to age not being possible to fetch from >100 year short format without separator, we aught to check this here.
9298
Assert.Equal(years > 99 ? years - 100 : years, Personnummer.Parse(ssn.ShortFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
9399
}
100+
101+
[Fact]
102+
public void TestEdgeCasesAroundBirthday()
103+
{
104+
var timeProvider = new TestTimeProvider
105+
{
106+
Now = DateTimeOffset.Parse("2090-01-09 12:00")
107+
};
108+
109+
Assert.Equal(10, new Personnummer("20800108-6670", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
110+
Assert.Equal(10, new Personnummer("20800109-8287", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
111+
Assert.Equal(9, new Personnummer("20800110-8516", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
112+
}
113+
94114
#endif
95115

96116
[Theory]
@@ -236,15 +256,5 @@ public void TestParseTooShort()
236256
}).Message
237257
);
238258
}
239-
240-
[Fact]
241-
public void TestEdgeCasesAroundBirthday()
242-
{
243-
var timeProvider = new TestTimeProvider(); //TestTime is 2025-10-05
244-
Assert.Equal(18, new Personnummer("20071004-3654", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
245-
Assert.Equal(18, new Personnummer("20071005-3653", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
246-
Assert.Equal(17, new Personnummer("20071006-3652", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
247-
}
248-
249259
}
250260
}

Personnummer.Tests/TestTimeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Personnummer.Tests;
1010
public class TestTimeProvider : TimeProvider
1111
{
1212
internal DateTimeOffset Now { get; set; } = new(
13-
new DateOnly(2025, 1, 5),
13+
new DateOnly(2025, 1, 1),
1414
new TimeOnly(0, 0, 0, 1),
1515
TimeSpan.Zero
1616
);

Personnummer/Personnummer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public int Age
4646
var now = _options.DateTimeNow;
4747
var age = now.Year - Date.Year;
4848

49-
var hadBirthdayThisYear = (now.Month > Date.Month) ||
49+
var hadBirthdayThisYear = now.Month > Date.Month ||
5050
(now.Month == Date.Month && now.Day >= Date.Day);
5151

5252
if (!hadBirthdayThisYear)

0 commit comments

Comments
 (0)