agentleFS
LeanMeanCleanAppsLeanMeanCleanApps/CleanModelContextProtocol/.agents/skills/time-provider/SKILL.md

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.

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
  1. Very specific:

    Read the time once per operation into a local (var now = this.timeProvider.GetUtcNow().UtcDateTime;) and use that value everywhere in the …

    line 19
  2. Very specific:

    Call DateTime.Now, DateTime.UtcNow or DateTimeOffset.UtcNow in src.

    line 50
  3. Distinctive:

    Read the time more than once in one operation.

    line 51

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

  1. Take TimeProvider timeProvider in the constructor, check it with ArgumentNullException.ThrowIfNull, and keep it in a private readonly field.
  2. 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.
  3. In repository tests, use the clock from MovieReviewsTestDatabase (fixed at 2009-12-31 23:51:01 UTC) and assert against it.
  4. To test something that depends on time passing, call Advance(TimeSpan) on the fake. LoggingBehaviourTests advance it inside the fake next delegate, 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.UtcNow or DateTimeOffset.UtcNow in src.
  • 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.Integration

Download now (ZIP)

Does 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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.