Skip to content

Added usage of TimeProvider instead of DateTimeOffset - #63042

Merged
adityamandaleeka merged 5 commits into
dotnet:mainfrom
StickFun:main
Jan 9, 2026
Merged

adityamandaleeka merged 5 commits into
dotnet:mainfrom
StickFun:main

Conversation

@StickFun

@StickFun StickFun commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

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
@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

StickFun commented Aug 1, 2025 •

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

var lockoutTime = await store.GetLockoutEndDateAsync(user, CancellationToken).ConfigureAwait(false);
return lockoutTime >= DateTimeOffset.UtcNow;
#if NET8_0_OR_GREATER
var utcNow = UtcNow();

Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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

@StickFun
StickFun requested a review from martincostello August 6, 2025 18:09

@martincostello martincostello left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe add a test that demonstrates it's used for .NET 8.0+, but otherwise looks ok to me.

@StickFun

StickFun commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

Maybe add a test that demonstrates it's used for .NET 8.0+, but otherwise looks ok to me.

Added unit test that verifies using TimeProvider

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 15, 2025
@StickFun

StickFun commented Aug 18, 2025 •

Copy link
Copy Markdown
Contributor Author

@martincostello, do I need to rerun CI/CD myself and ping reviewer for last approve?

@martincostello

Copy link
Copy Markdown
Member

If you close and reopen the PR it will run it again.

@StickFun StickFun closed this Aug 20, 2025
@dotnet-policy-service dotnet-policy-service Bot added this to the 10.0-rc1 milestone Aug 20, 2025
@StickFun StickFun reopened this Aug 20, 2025
@StickFun

Copy link
Copy Markdown
Contributor Author

@martincostello, is there anything else I should do to merge this PR?

@martincostello

Copy link
Copy Markdown
Member

Assuming the test failures aren't due to your changes, you'll just have to wait for someone from the aspnetcore team to look at it and merge it.

@danroth27 danroth27 modified the milestones: 10.0-rc1, 10.0.0 Oct 4, 2025
@adityamandaleeka

Copy link
Copy Markdown
Member

/azp run

@dotnet-policy-service dotnet-policy-service Bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 6, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@adityamandaleeka
adityamandaleeka enabled auto-merge (squash) January 7, 2026 00:00
@adityamandaleeka

Copy link
Copy Markdown
Member

@StickFun Sorry for the delay in reviewing this, and thanks for the contribution!

@adityamandaleeka

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@adityamandaleeka

Copy link
Copy Markdown
Member

/ba-g The build analysis check seems to be remembering an old failure that is long lost and not occurring on current runs.

@adityamandaleeka
adityamandaleeka merged commit 30d4d50 into dotnet:main Jan 9, 2026
29 checks passed
@adityamandaleeka

Copy link
Copy Markdown
Member

Thanks again @StickFun!

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

6 participants