ToolBX.TimeProvider 2.2.0

dotnet add package ToolBX.TimeProvider --version 2.2.0
NuGet\Install-Package ToolBX.TimeProvider -Version 2.2.0
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="ToolBX.TimeProvider" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolBX.TimeProvider --version 2.2.0
#r "nuget: ToolBX.TimeProvider, 2.2.0"
#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 ToolBX.TimeProvider as a Cake Addin
#addin nuget:?package=ToolBX.TimeProvider&version=2.2.0

// Install ToolBX.TimeProvider as a Cake Tool
#tool nuget:?package=ToolBX.TimeProvider&version=2.2.0

TimeProvider

TimeProvider

A static utility class for overriding system time.

⚠️ The TimeProvider class will be renamed GlobalTimeProvider in 3.0.0 to avoid name collisions with .NET 8's new TimeProvider class.

Getting started

In order to use this library to its full potential, all your DateTime.Now calls must be changed to TimeProvider.Now. This will ensure that time overrides are accurate for your entire application.


//Sets the current date to Christmas 2025
TimeProvider.Override(new DateTime(2025, 12, 25));

//Will return the above overriden date ^ ... plus the amount of time that went by between overriding the date and now
var now = TimeProvider.Now;

//If you want to "freeze" time in place you can use
TimeProvider.Freeze(new DateTime(2021, 10, 31);

//Will return the above frozen date ^ ... which will always be halloween 2021 at exactly midnight no matter how much time goes by
var now = TimeProvider.Now;

//If you just want to test how it'll go tomorrow
TimeProvider.AddDays(1);

//Or how it'll go three years ago
TimeProvider.SubtractYears(3);

Why

In most cases, TimeProvider is used to facilitate unit and integration tests that depend on time.

Let�s say you were working on a video game, and you want to give the player a �Happy birthday!� message when they play it on their birthday.

To test it, you could have a way to override the date in your debug menu which would ultimately call TimeProvider.Override().

Alternatively, I suppose you could modify your system time manually, but I find it more complicated than it needs to be on some operating systems such as Windows. TimeProvider also provides you with a unified way of overriding dates in case you need to test your application on multiple platforms.

But it�s not all sunshine and rainbows either because nothing prevents you or your colleagues from just using DateTime.Now instead of TimeProvider.Now. There is no perfect solution to the problem- at least none that I have found yet.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ToolBX.TimeProvider:

Package Downloads
ToolBX.MisterTerminal

A high level library to easily and cleanly build smarter console applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 378 1/11/2024
2.2.0-beta1 134 11/18/2023
2.0.3 287 6/8/2023
2.0.2 237 5/1/2023
2.0.1 179 4/24/2023
2.0.0 354 11/9/2022
2.0.0-beta1 104 10/5/2022
1.0.0 630 3/3/2022