time-provider
Skill1 starsChanged 6 days agoMITView on GitHub
How code reads the current time through TimeProvider instead of DateTime.UtcNow, where the system clock is registered, and how tests fix the clock with FakeTimeProvider. Use when code needs the current date or time, sets a created or modified date, or a test depends on time.
In this repository40 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- add-a-use-caseSkill
- api-conventionsSkill
- branches-and-pull-requestsSkill
- cancellationSkill
- ci-for-your-solutionSkill
- ci-pipelineSkill
- clean-architecture-layersSkill
- configuration-and-environmentsSkill
- coverageSkill
- csharp-house-styleSkill
- data-access-ef-coreSkill
- database-migrationsSkill
- docker-and-composeSkill
- docs-and-voiceSkill
- errors-and-exceptionsSkill
- expose-a-use-caseSkill
- family-syncSkill
- health-checksSkill
- integration-testsSkill
- json-serializationSkill
- mapping-with-mapperlySkill
- mcp-inspectorSkill
- mediatorSkill
- optional-token-authSkill
- packages-and-dependenciesSkill
- pagingSkill
- presentation-integration-testsSkill
- releasesSkill
- sample-dataSkill
- security-findingsSkill
- serilogSkill
- telemetrySkill
- template-authoringSkill
- test-designSkill
- unit-testsSkill
- validationSkill
- writing-a-skillSkill
- xml-docs-and-openapiSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 19
Read the time once per operation into a local (var now = this.timeProvider.GetUtcNow().UtcDateTime;) and use that value everywhere in the …
- Very specific: line 50
Call DateTime.Now, DateTime.UtcNow or DateTimeOffset.UtcNow in src.
- Distinctive: line 51
Read the time more than once in one operation.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Time provider
The rule
Nothing reads the clock directly. Code that needs the time takes TimeProvider through its constructor and calls GetUtcNow(). Production gets TimeProvider.System, registered with TryAddSingleton in AddInfrastructure only. The system clock is an outside dependency, and Infrastructure owns those; Application only uses the abstract TimeProvider, which is part of .NET and needs no package. Every host calls AddInfrastructure, and TryAdd lets a host or test register its own clock first. Tests pass a FakeTimeProvider (Microsoft.Extensions.TimeProvider.Testing) set to a fixed moment, so every date is known in advance.
Times are stored and compared in UTC. Two things read the clock today. The repository stamps DateCreated and DateModified on writes, and LoggingBehaviour times each request with GetTimestamp() and GetElapsedTime().
Steps
- Take
TimeProvider timeProviderin the constructor, check it withArgumentNullException.ThrowIfNull, and keep it in aprivate readonlyfield. - Read the time once per operation into a local (
var now = this.timeProvider.GetUtcNow().UtcDateTime;) and use that value everywhere in the operation, so created and modified dates match. - In repository tests, use the clock from
MovieReviewsTestDatabase(fixed at 2009-12-31 23:51:01 UTC) and assert against it. - To test something that depends on time passing, call
Advance(TimeSpan)on the fake.LoggingBehaviourTestsadvance it inside the fakenextdelegate, so the logged time is exact.
Example
// src/Infrastructure/Databases/MovieReviews/EntityFrameworkMovieReviewsRepository.cs
public async Task<bool> UpdateReview(Guid id, int stars, CancellationToken cancellationToken)
{
var now = this.timeProvider.GetUtcNow().UtcDateTime;
var updated = await this.context.Reviews
.Where(r => r.Id == id)
.ExecuteUpdateAsync(
setters => setters
.SetProperty(r => r.Stars, stars)
.SetProperty(r => r.DateModified, now),
cancellationToken);
return updated > 0;
}// tests/Infrastructure.Tests.Integration/Databases/MovieReviews/EntityFrameworkMovieReviewsRepositoryTests.cs
updatedReview.DateModified.ShouldBe(this.database.TimeProvider.GetUtcNow().UtcDateTime);Don't
- Call
DateTime.Now,DateTime.UtcNoworDateTimeOffset.UtcNowinsrc. - Read the time more than once in one operation.
- Store local times.
- Assert a date with a tolerance ("within a second of now"). Fix the clock and assert the exact value.
The one exception, in a solution with authentication, is the test that creates a token for the real JWT handler, which checks expiry against the system clock (tests/Presentation.Tests.Integration/Authentication/AuthenticationTests.cs).
Check
grep -rn "DateTime.UtcNow\|DateTime.Now\|DateTimeOffset.UtcNow" src # expect no output
dotnet test tests/Infrastructure.Tests.IntegrationDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

