dotnet-testing-datetime-testing-timeprovider

Test time-dependent .NET logic using TimeProvider and FakeTimeProvider.

1|Updated Feb 22, 2026
One-click install
npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-testing-datetime-testing-timeprovider-rudironsoni
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-testing-datetime-testing-timeprovider
Source: https://github.com/rudironsoni/dotnet-agent-harness/tree/main/.rulesync/skills/dotnet-testing-datetime-testing-timeprovider
Command: npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-testing-datetime-testing-timeprovider-rudironsoni

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the challenge of testing time-dependent logic in .NET applications by providing a robust way to control and simulate time using TimeProvider.

Core Features & Use Cases

  • Time Abstraction: Replace static DateTime calls with TimeProvider for testability.
  • Time Simulation: Use FakeTimeProvider to freeze, fast-forward, or rewind time during tests.
  • Scenario Testing: Effortlessly test business hours validation, cache expiration, scheduled tasks, and token expiration logic.

Quick Start

Use the dotnet-testing-datetime-testing-timeprovider skill to learn how to refactor a DateTime.Now call to use TimeProvider.

Frequently Asked Questions about dotnet-testing-datetime-testing-timeprovider

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test time-dependent logic in .NET without relying on the system clock?

To test time-dependent logic in .NET, you abstract static `DateTime` calls using `TimeProvider` and inject `FakeTimeProvider` to control time. This isolates tests from the system clock, allowing exact time simulation for reliable unit testing.

What is the best way to simulate cache expiration or token expiration in unit tests?

The best way to simulate cache or token expiration in unit tests is using `FakeTimeProvider`. It allows you to fast-forward time precisely, enabling you to trigger and verify expiration logic instantly without waiting for real time to pass.

Can I use FakeTimeProvider to test scheduled tasks and business hours validation in .NET?

Yes, you can use `FakeTimeProvider` to test scheduled tasks and business hours validation. By freezing or rewinding time, you can verify that specific times correctly trigger scheduled tasks or pass business hours validation rules.

How do I refactor DateTime.Now calls to use TimeProvider for dependency injection?

You refactor `DateTime.Now` by replacing it with a `TimeProvider` instance injected via dependency injection. This allows your tests to supply a `FakeTimeProvider`, ensuring deterministic test execution and complete time control.

Does TimeProvider work with standard .NET dependency injection setups?

Yes, `TimeProvider` integrates seamlessly with standard .NET dependency injection. You register it in your services container, allowing production code to use the system clock while tests inject `FakeTimeProvider` for isolated time simulation.