Timecop.NodaTime
1.1.0
See the version list below for details.
dotnet add package Timecop.NodaTime --version 1.1.0
NuGet\Install-Package Timecop.NodaTime -Version 1.1.0
<PackageReference Include="Timecop.NodaTime" Version="1.1.0" />
paket add Timecop.NodaTime --version 1.1.0
#r "nuget: Timecop.NodaTime, 1.1.0"
// Install Timecop.NodaTime as a Cake Addin #addin nuget:?package=Timecop.NodaTime&version=1.1.0 // Install Timecop.NodaTime as a Cake Tool #tool nuget:?package=Timecop.NodaTime&version=1.1.0
Timecop for NodaTime
Timecop.NodaTime is Timecop with the NodaTime interface.
Timecop targets .NET Standard 2.0, and supports NodaTime v3.
Timecop.NodaTime and the original Timecop have been inspired by the timecop Ruby gem.
Installation
You can install Timecop.NodaTime from NuGet using the .NET CLI:
dotnet add package Timecop.NodaTime
Basic usage
Timecop.NodaTime allows you to freeze and travel in time. Just use the NodaClock
class instead of SystemClock
to get the current instant with the GetCurrentInstant
method, and manipulate time with the NodaTimecop
class in your tests.
string Greet(DateTimeZone zone)
{
var timeOfDay = NodaClock.GetCurrentInstant().InZone(zone).Hour switch // Use NodaClock instead of SystemClock.Instance
{
>= 0 and < 6 => "night",
>= 6 and < 12 => "morning",
>= 12 and < 18 => "afternoon",
_ => "evening"
};
return $"Good {timeOfDay}!";
}
var zone = DateTimeZoneProviders.Tzdb["Europe/Kyiv"];
// freeze at 2pm Kyiv time:
using var tc = NodaTimecop.Frozen(o => o.At(14, 0, 0).InZone(zone));
Greet(zone); // Good afternoon!
// travel to 8pm local time:
tc.TravelBy(Duration.FromHours(6));
Greet(zone); // Good evening!
Available methods
Freezing and resuming time
You can freeze the time so that it stops running for your tests until you call Resume
or dispose the Timecop
instance.
You freeze time with either an instance Freeze
or a static Frozen
method, which both have the same set of overloads. Both methods have the same effect, however the static Frozen
creates an already frozen Timecop
instance.
using var tc = Timecop.Frozen(Instant.FromUtc(1990, 12, 2, 14, 38, 51));
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
Freeze
and Frozen
have multiple overloads:
// freeze at the current instant:
var frozenAt = tc.Freeze();
// freeze at the specific instant:
var instant = Instant.FromUtc(1990, 12, 2, 14, 53, 27);
var frozenAt = tc.Freeze(instant);
// freeze at the specified ZonedDateTime:
ZonedDateTime zonedDateTime = new LocalDateTime(1990, 12, 2, 14, 38, 51).InUtc();
frozenAt = tc.Freeze(zonedDateTime);
// freeze at the specified date or time using a PointInTimeBuilder:
frozenAt = tc.Freeze(o => o.On(1990, 12, 2)
.At(14, 13, 51)
.InUtc());
Traveling in time
Use the TravelBy
method to travel forward and backward in time:
using var tc = Timecop.Frozen(Instant.FromUtc(1990, 12, 2, 14, 38, 51));
tc.TravelBy(Duration.FromDays(1));
NodaClock.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 | Versions 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. 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. |
.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. |
-
.NETStandard 2.0
- NodaTime (>= 3.0.0 && < 4.0.0)
- Timecop.Core (>= 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.