-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added usage of TimeProvider instead of DateTimeOffset #63042
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for your PR, @@StickFun. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
@dotnet-policy-service agree |
@@ -2067,7 +2067,12 @@ public virtual async Task<bool> IsLockedOutAsync(TUser user) | |||
return false; | |||
} | |||
var lockoutTime = await store.GetLockoutEndDateAsync(user, CancellationToken).ConfigureAwait(false); | |||
return lockoutTime >= DateTimeOffset.UtcNow; | |||
#if NET8_0_OR_GREATER | |||
var utcNow = UtcNow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you made the #if
just change the behaviour of UtcNow()
you could do it in just one place and then use the method everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using #if
inside UtcNow()
causes CA1822 error and results in build failure. This happens because the ServiceProvider
field is used only in .NET 8 and higher versions. For older versions, this method should be marked static.
private DateTimeOffset UtcNow()
{
#if NET8_0_OR_GREATER
var timeProvider = ServiceProvider.GetService<TimeProvider>();
return timeProvider?.GetUtcNow() ?? DateTimeOffset.UtcNow;
#else
return DateTimeOffset.UtcNow;
#endif
}
Another solution I see is to create two separate methods for UtcNow(). One of them should be static for older versions and the other should not.
#if NET8_0_OR_GREATER
private DateTimeOffset UtcNow()
{
var timeProvider = ServiceProvider.GetService<TimeProvider>();
return timeProvider?.GetUtcNow() ?? DateTimeOffset.UtcNow;
}
#else
private static DateTimeOffset UtcNow()
{
return DateTimeOffset.UtcNow;
}
#endif
If you have any other ideas or approaches for this, please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it were me I would do the second one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed UtcNow method implementation
Added usage of TimeProvider instead of DateTimeOffset
Description
Added usage of TimeProvider for NET 8 and higher versions.
Other versions using DateTimeOffset
Fixes #62796