Skip to content

Provides an interface IDataTimeProvider and static container. Also provided is a StaticDateTimeProvider and IDiposable for use in your Unit Tests

License

Notifications You must be signed in to change notification settings

dennisroche/DateTimeProvider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DateTimeProvider Build Master NuGet Version .NET Standard 1.3 .NET Framework 4.5 Join the chat at https://gitter.im/DateTimeProvider/Lobby

Provides an interface IDateTimeProvider and static container.

Targets netstandard1.3 and net45.

DateTimeProvider exists in the global:: namespace to make usage easier.

DateTimeOffset vs DateTime

The library provides DateTimeOffset, not DateTime. Why? Read this excellent answer on StackOverflow

DateTimeOffset is great, however there are use-cases where you need to use DateTime as Local or UTC.

You can use DateTimeOffset.LocalDateTime and DateTimeOffset.UtcDateTime properties. For convenience, our static DateTimeProvider has these properties as LocalNow and UtcNow.

How to use

Install the Nuget package.

Install-Package DateTimeProvider

Set the provider

DateTimeProvider.Provider = new UtcDateTimeProvider();

Or

DateTimeProvider.Provider = new LocalDateTimeProvider();

NB The default is UtcDateTimeProvider.

Then use or replace in place of DateTime in your code.

var now = DateTimeProvider.Now;
var local = DateTimeProvider.LocalNow;
var utc = DateTimeProvider.UtcNow;

Pinning DateTime

Also provided is a static IDateTimeProvider and IDisposable Override so that time can be manipulated in a fixed scope.

Why is this useful?

  • In a web request, you want to freeze time so that all your modified dates are aligned (i.e. not +/- by a few seconds)
  • In unit tests when you need to verify business logic.

Examples

Pinning Time in Logic

using (var o = new OverrideDateTimeProvider(DateTimeProvider.Now))
{
    // logic
}

Pinning Time in Unit Tests

In your tests, you need may need to manipulate time to verify your logic. This is easy using the OverrideDateTimeProvider.

Console.WriteLine($"{DateTimeProvider.Now}");

var testingWithDate = new DateTimeOffset(new DateTime(2014, 10, 01), TimeSpan.FromHours(8));
using (var o = new OverrideDateTimeProvider(testingWithDate))
{
    Console.WriteLine($"{DateTimeProvider.Now} (Testing)");
    o.MoveTimeForward(TimeSpan.FromHours(5));
    Console.WriteLine($"{DateTimeProvider.Now} (+ 5 hours)");
}

Console.WriteLine($"{DateTimeProvider.Now} (Restored)");

Output

6/11/2015 5:08:12 AM +00:00
1/10/2014 12:00:00 AM +08:00 (Testing)
1/10/2014 5:00:00 AM +08:00 (+ 5 hours)
6/11/2015 5:08:12 AM +00:00 (Restored)

DateTimeProvider.Analyser NuGet Version

Also available is a Roslyn Analyser that will suggest replacements for DateTime for DateTimeProvider.

Install the Nuget package.

Install-Package DateTimeProvider.Analyser

License

MIT

About

Provides an interface IDataTimeProvider and static container. Also provided is a StaticDateTimeProvider and IDiposable for use in your Unit Tests

Resources

License

Stars

Watchers

Forks

Packages

No packages published