Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

StickFun
Copy link

@StickFun StickFun commented Aug 1, 2025

Added usage of TimeProvider instead of DateTimeOffset

Description

Added usage of TimeProvider for NET 8 and higher versions.
Other versions using DateTimeOffset

Fixes #62796

@github-actions github-actions bot added the area-identity Includes: Identity and providers label Aug 1, 2025
Copy link
Contributor

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 dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 1, 2025
@StickFun
Copy link
Author

StickFun commented Aug 1, 2025

@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();
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed UtcNow method implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-identity Includes: Identity and providers community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use TimeProvider instead of DateTimeOffset in UserManager
2 participants