/TimeService

Testable and configurable time service for .Net.

Primary LanguageC#MIT LicenseMIT

TimeService

CI Nuget Nuget

Configurable and simple time service for .Net that makes it easy to unit test your code.

Feedback is welcome 🙂

Usage

Install as NuGet package

dotnet add package HeboTech.TimeService

Example usage in production code

// Set up during application startup
TimeService.Set(TimeProviders.SystemTime);

// Use as a static service
DateTime time = TimeService.Now;

Example usage when running unit tests

// Set the time
DateTime startTime = new DateTime(2020, 1, 2);
TimeService.Set(() => startTime);

// TimeService returns the set time
Assert.Equal(startTime, TimeService.Now);

// Set a new time
DateTime newTime = new DateTime(2021, 2, 3);
TimeService.Set(() => newTime);

// TimeService returns the new time
Assert.Equal(newTime, TimeService.Now);