Timecop 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Timecop --version 1.0.1
NuGet\Install-Package Timecop -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Timecop" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Timecop --version 1.0.1
#r "nuget: Timecop, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Timecop as a Cake Addin
#addin nuget:?package=Timecop&version=1.0.1

// Install Timecop as a Cake Tool
#tool nuget:?package=Timecop&version=1.0.1

Timecop - Easy Date and Time Testing in C#

Timecop is a small library that helps you test DateTime in a static, thread-safe, ambient context.

Timecop targets .NET Standard 2.0, has no external dependencies, and can be used with .NET Framework 4.5+ and any version of .NET and .NET Core.

Timecop has been inspired by the timecop Ruby gem.

Installation

You can install Timecop from NuGet using the .NET CLI:

dotnet add package Timecop

Basic usage

Timecop allows you to freeze and travel in time. Just use the Clock class instead of DateTimeto get the current time via Now or UtcNow properties, and manipulate time with the Timecop class in your tests.

string Greet()
{
    var timeOfDay = Clock.Now.Hour switch // Use Clock instead of DateTime
    {
        >= 0 and < 6 => "night",
        >= 6 and < 12 => "morning",
        >= 12 and < 18 => "afternoon",
        _ => "evening"
    };

    return $"Good {timeOfDay}!";
}

// freeze at 2pm local time:
using var tc = Timecop.Frozen(o => o.At(14,0,0).LocalTime()); 

Greet(); // Good afternoon!

// travel to 8pm local time:
tc.TravelBy(TimeSpan.FromHours(6)); 

Greet(); // Good evening!

Available methods

Freezing and resuming time

Time is frozen with either an instance Freeze or a static Frozen method, both having the same set of signatures. The instance Freeze freezes the instance of Timecop, the static Frozen creates an already frozen instance.

Frozen time doesn't run for your tests until you call Resume or dispose the Timecop instance:

using var tc = Timecop.Frozen(1990, 12, 2, 14, 38, 51, DateTimeKind.Local);

Clock.Now; // 1990-12-02 14:38:51

Thread.Sleep(TimeSpan.FromSeconds(3));

Clock.Now; // 1990-12-02 14:38:51 - still the same value

tc.Resume();

Thread.Sleep(TimeSpan.FromSeconds(3));

Clock.Now; // 1990-12-02 14:38:54 - time has changed

Both Freeze and Frozen have multiple overloads:

// freeze at the current instant:
tc.Freeze();

// freeze at the specified DateTime:
tc.Freeze(new DateTime(1990, 12, 2, 14, 38, 51, DateTimeKind.Utc));

// freeze at the specified date and time:
tc.Freeze(1990, 12, 2, 14, 38, 51, DateTimeKind.Utc);

// freeze at the specified date:
tc.Freeze(1990, 12, 2, DateTimeKind.Utc);

// freeze at the specified date or time or both:
tc.Freeze(o => o.On(1990, 12, 2)
                .At(14, 13, 51)
                .LocalTime());

Traveling in time

Use the TravelBy method to travel forward and backward in time:

using var tc = Timecop.Frozen(1990, 12, 2, 14, 38, 51, DateTimeKind.Local);

tc.TravelBy(TimeSpan.FromDays(1));

Clock.Now; // 1990-12-03 14:38:51 - one day in the future

License

Timecop was created by Dmytro Khmara and is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Timecop:

Package Downloads
Timecop.Extensions.DependencyInjection

Extensions to register Timecop's IClock with IServiceCollection in one line of code.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.0 336 8/5/2023
2.2.1 125 8/3/2023
2.2.0 137 7/31/2023
2.1.0 117 7/29/2023
2.0.1 121 7/25/2023
2.0.0 129 7/20/2023
1.0.1 120 7/14/2023
1.0.0 128 7/12/2023