dotnet-testing-datetime-testing-timeprovider

Abstract time behind TimeProvider for deterministic tests with FakeTimeProvider.

28|4|Updated Jan 20, 2026
One-click install
npx skills add https://github.com/kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-datetime-testing-timeprovider
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-testing-datetime-testing-timeprovider
Source: https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-datetime-testing-timeprovider
Command: npx skills add https://github.com/kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-datetime-testing-timeprovider

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Time-dependent code is hard to test because it relies on the system clock. This Skill provides a TimeProvider abstraction so time can be controlled in tests.

Core Features & Use Cases

  • Time abstraction via TimeProvider and GetLocalNow/GetUtcNow to replace DateTime.Now/DateTime.UtcNow.
  • FakeTimeProvider for precise control over time, time freezing, and time advancement in tests.
  • Scenarios include business hours checks, expiry logic, caching, and scheduled tasks, all with deterministic tests.

Quick Start

Install Microsoft.Bcl.TimeProvider and Microsoft.Extensions.TimeProvider.Testing; inject TimeProvider.System in production and FakeTimeProvider in tests, then use SetLocalNow and Advance to simulate time.

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?

FakeTimeProvider enables deterministic .NET testing by allowing precise time control. You use SetLocalNow to freeze time and Advance to simulate its passage, replacing DateTime.Now dependencies for reliable unit tests.

How do I use FakeTimeProvider to simulate time advancement in unit tests?

FakeTimeProvider simulates time advancement in unit tests via the Advance method. Configure SetLocalNow to establish a starting point, then call Advance to move time forward deterministically for scheduled task testing.

What scenarios are suitable for testing with TimeProvider and FakeTimeProvider?

TimeProvider and FakeTimeProvider are suitable for testing business hours checks, expiry logic, caching, and scheduled tasks. These scenarios benefit from deterministic time simulation rather than unpredictable system clock dependencies.

Do I need to use dependency injection to test time-dependent code with TimeProvider?

Yes, dependency injection is required to test time-dependent code with TimeProvider. You must inject TimeProvider.System for production environments and FakeTimeProvider for test environments to ensure proper time abstraction.

What .NET packages are required to implement TimeProvider for unit testing?

To implement TimeProvider for unit testing, you need the Microsoft.Bcl.TimeProvider package for production abstraction and Microsoft.Extensions.TimeProvider.Testing for FakeTimeProvider test capabilities.