ToolBX.TimeProvider
3.0.0
dotnet add package ToolBX.TimeProvider --version 3.0.0
NuGet\Install-Package ToolBX.TimeProvider -Version 3.0.0
<PackageReference Include="ToolBX.TimeProvider" Version="3.0.0" />
paket add ToolBX.TimeProvider --version 3.0.0
#r "nuget: ToolBX.TimeProvider, 3.0.0"
// Install ToolBX.TimeProvider as a Cake Addin #addin nuget:?package=ToolBX.TimeProvider&version=3.0.0 // Install ToolBX.TimeProvider as a Cake Tool #tool nuget:?package=ToolBX.TimeProvider&version=3.0.0
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ToolBX.TimeProvider:
Package | Downloads |
---|---|
ToolBX.Dummies
A lightweight object generation framework for unit testing purposes |
|
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 |
---|---|---|
3.0.0 | 3,799 | 9/26/2024 |
3.0.0-beta1 | 112 | 9/23/2024 |
2.2.0 | 1,047 | 1/11/2024 |
2.2.0-beta1 | 185 | 11/18/2023 |
2.0.3 | 323 | 6/8/2023 |
2.0.2 | 278 | 5/1/2023 |
2.0.1 | 216 | 4/24/2023 |
2.0.0 | 396 | 11/9/2022 |
2.0.0-beta1 | 133 | 10/5/2022 |
1.0.0 | 677 | 3/3/2022 |