Skip to content
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
2 changes: 0 additions & 2 deletions Personnummer.Tests/CoordinationNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void TestParseInvalidCn(PersonnummerData ssn)
}));
}

#if NET8_0_OR_GREATER
[Theory]
[ClassData(typeof(ValidCnDataProvider))]
public void TestAgeCn(PersonnummerData ssn)
Expand All @@ -146,7 +145,6 @@ public void TestAgeCn(PersonnummerData ssn)
// 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 = true, TimeProvider = timeProvider }).Age);
}
#endif

[Theory]
[ClassData(typeof(ValidCnDataProvider))]
Expand Down
4 changes: 0 additions & 4 deletions Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public void TestParseInvalid(PersonnummerData ssn)
}));
}


#if NET8_0_OR_GREATER // Requires TimeProvider.
[Theory]
[ClassData(typeof(ValidSsnDataProvider))]
public void TestAge(PersonnummerData ssn)
Expand Down Expand Up @@ -111,8 +109,6 @@ public void TestEdgeCasesAroundBirthday()
Assert.Equal(9, new Personnummer("20800110-8516", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
}

#endif

[Theory]
[ClassData(typeof(ValidSsnDataProvider))]
public void TestFormat(PersonnummerData ssn)
Expand Down
16 changes: 7 additions & 9 deletions Personnummer.Tests/TestTimeProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;

namespace Personnummer.Tests;

#if NET8_0_OR_GREATER
using TimeProvider = System.TimeProvider;
#else
using TimeProvider = Personnummer.TimeProvider;
#endif

namespace Personnummer.Tests;

/// <summary>
/// TimeProvider which always returns the same date: 2025 01 01 00:00:01 with UTC timezone on local time.
Expand All @@ -16,11 +20,5 @@ public class TestTimeProvider : TimeProvider
);

public override DateTimeOffset GetUtcNow()
{
return Now;
}

public override TimeZoneInfo LocalTimeZone { get; } = TimeZoneInfo.Utc;
=> Now;
}

#endif
12 changes: 7 additions & 5 deletions Personnummer/Personnummer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
using System.Text.RegularExpressions;
using Personnummer.Exceptions;

#if NET8_0_OR_GREATER
using TimeProvider = System.TimeProvider;
#else
using TimeProvider = Personnummer.TimeProvider;
#endif

namespace Personnummer
{
public class Personnummer
Expand All @@ -25,20 +31,16 @@ public Options()
/// </summary>
public bool AllowInterimNumber { get; set; } = false;

#if (NET8_0_OR_GREATER)
/// <summary>
/// TimeProvider to use in calculations which are time dependent.<br/>
/// Uses `GetLocalNow` method.
/// <remarks>
/// Defaults to System provider.
/// </remarks>
/// </summary>
public TimeProvider TimeProvider { private get; init; } = TimeProvider.System;
public TimeProvider TimeProvider { private get; set; } = TimeProvider.System;

internal DateTimeOffset DateTimeNow => TimeProvider.GetLocalNow();
#else
internal DateTimeOffset DateTimeNow => DateTimeOffset.Now;
#endif
}

#region Fields and Properties
Expand Down
21 changes: 21 additions & 0 deletions Personnummer/TimeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Personnummer;

#if (!NET8_0_OR_GREATER)
public abstract class TimeProvider
{
public virtual DateTimeOffset GetLocalNow() => GetUtcNow().ToLocalTime();

public static TimeProvider System { get; }
= new FallbackTimeProvider();

public abstract DateTimeOffset GetUtcNow();

private sealed class FallbackTimeProvider : TimeProvider
{
public override DateTimeOffset GetUtcNow()
=> DateTimeOffset.UtcNow;
}
}
#endif
Loading