Skip to content

Melchy/Clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 

Repository files navigation

Clock

This package provides thread safe testable global DateTimeUtc provider.

NuGet

Clock

ClockTesting

How to use

Install package Clock and use Clock.UtcNow instead of DateTimeOffest.UtcNow.

In your test project install package ClockTesting and mock calls to Clock.UtcNow like this:

using (new OverrideClock(DateTime.MinValue)) //Clock.UtcNow will provide DateTime.MinValue in this using
{
    DateTimeOffset timeNow = Clock.UtcNow; // timeNow is now set to DateTime.MinValue
}

Analyzer

Clock package contains analyzer which detects all calls to DateTimeOffest.UtcNow, DateTimeOffest.Now, DateTime.UtcNow, DateTime.Now and suggests replacing with Clock.UtcNow.

This analyzer is very restrictive to help programmer avoid mistakes. If you need less restrictive provider use this package.

How it works

Clock package contains static class Clock. Clock has internal static property which provides current time. This property is set by default to DateTimeOffset.UtcNow(). Clock has internals visible to ClockTesting which allows ClockTesting package set Time provider. OverrideClock then overrides clock provider and Dispose method restarts it back to DateTimeOffset.UtcNow().

About

Golbal Clock used to replace DateTime

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published