Home » New in .NET 8.0 [18]: A time lapse with its own FakeTimeProvider

New in .NET 8.0 [18]: A time lapse with its own FakeTimeProvider

by admin
New in .NET 8.0 [18]: A time lapse with its own FakeTimeProvider

The previous blog post about the new features in .NET 8.0 discussed the newly introduced System.TimeProvider class and the FakeTimeProvider from Microsoft based on it in the NuGet package Microsoft.Extensions.TimeProvider.Testing.

Advertisement

But you can also write a FakeTimeProvider yourself. In the following listing, the class ITVisionsFakeTimeProvider implements its own provider based on the abstract base class System.TimeProvider. This ITVisionsFakeTimeProvider always returns the same time that was passed in the constructor and can be presented using Advance().

In addition to Microsoft’s implementation, the class offers an adjustable time lapse. For example, by specifying 3 as time lapse in the constructor, waiting only takes two seconds instead of six:

ITVisionsFakeTimeProvider ftp =
new(DateTime.Parse(“14.11.2023 18:00:00”), 3);
await ftp.Delay(new TimeSpan(0, 0, 6));
// oder
await Task.Delay(new TimeSpan(0, 0, 5), ftp);

The time lapse also works on timers. While this timer goes off every three seconds

TimeProvider.System.CreateTimer
((x) => Callback(TimeProvider.System),
null,
new TimeSpan(0, 0, 3),
new TimeSpan(0, 0, 3));

Here, due to time lapse 3, the trigger occurs every second:

ftp.CreateTimer((x) => Callback(ftp),
null,
new TimeSpan(0, 0, 3),
new TimeSpan(0, 0, 3));

Here is a code example using the ITVisionsFakeTimeProvider:

///

/// This FakeTimeProvider always returns the same time /// that was passed in the constructor. The timer can be advanced /// using Advance(). There is a time lapse for the timer. ///

class ITVisionsFakeTimeProvider : TimeProvider
{
private readonly DateTimeOffset _date;
int _timeLapse = 1;
public ITVisionsFakeTimeProvider(DateTimeOffset date,
int timeLapse = 1)
{
_date = date;
_timeLapse = timeLapse;
}
public override DateTimeOffset GetUtcNow()
{
return _date;
}

///

/// Introduce time ///

public void Advance(TimeSpan timeSpan)
{
_date.Add(timeSpan);
}

See also  Microsoft Windows’ benchmark AI functional specification requirements exposed!No computer is up to standard now

///

/// Timer with time lapse ///

public override ITimer CreateTimer(TimerCallback callback,
object state,
TimeSpan dueTime,
TimeSpan period)
{
return base.CreateTimer(callback, state,
new TimeSpan((long)(dueTime.Ticks / _timeLapse)),
new TimeSpan((long)(period.Ticks / _timeLapse)));
}

}

(Image: Dmytro Vikarchuk/Shutterstock)

In the online conference betterCode() .NET 9.0 on November 19, 2024 by iX and dpunkt.verlag, .NET experts from www.IT-Visions.de will present the finished version of .NET 9.0 using practical examples. These include the innovations regarding the .NET 9.0 SDK, C# 13.0, ASP.NET Core 9.0, Blazor 9.0, OR mapping with Entity Framework Core 9.0, Windows Forms 9.0, WPF 9.0, WinUI, cross-platform development with .NET MAUI 9.0 and an outlook on .NET 10.0.

Ticket sales have already started: discounted blind bird tickets are available before the program is announced.

(rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy