Skip to content

hbjorgo/TimeService

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);