MW.DateTimeSource 1.0.0

dotnet add package MW.DateTimeSource --version 1.0.0
                    
NuGet\Install-Package MW.DateTimeSource -Version 1.0.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="MW.DateTimeSource" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MW.DateTimeSource" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MW.DateTimeSource" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MW.DateTimeSource --version 1.0.0
                    
#r "nuget: MW.DateTimeSource, 1.0.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.
#:package MW.DateTimeSource@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MW.DateTimeSource&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MW.DateTimeSource&version=1.0.0
                    
Install as a Cake Tool

Mw DateTimeSource

Overview

The .NET DateTime class presents certain difficulties in unit tests. This library provides an IDateTimeSource interface, which contains two functions, Now() and UtcNow(), both of which return a DateTime object. Two implementations of this interface are provided, DateTimeSource and TestableDateTime source.

DateTimeSource is a simple wrapper around DateTiime's Now() and UtcNow() functions.

TestableDateTimeSource is designed for use in unit tests. It allows the test to specify the DateTime object that will be returned.

The library is written in C# and targets .NET Standard 2.0

Exaple use of DateTimeSource

class Program
{
    static void Main( string[] args )
    {
        // instantiate the business object, injecting a DateTimeSource via the constructor
        Alerter alerter = new Alerter( new DateTimeSource() );

        // load some events
        alerter.AddEvent( new AlertableEvent() { When = DateTime.Now, Description = $"It is today, {DateTime.Now.DayOfWeek}" } );
        DateTime tomorrow = DateTime.Now.AddDays( 1 );
        alerter.AddEvent( new AlertableEvent() { When = tomorrow, Description = $"It is not yet tomorrow, {tomorrow.DayOfWeek}" } );

        // run the business function and print the results
        alerter.GetTodaysEvents().ForEach( x => Console.WriteLine( x.Description ) );
    }
}


public class AlertableEvent
{
    public DateTime When { get; set; }
    public string Description { get; set; }
}


/// <summary>
/// This is our business class that will tell us about today's events
/// </summary>
public class Alerter
{
    private readonly IDateTimeSource _dateTimeSource;
    private readonly List<AlertableEvent> _events = new List<AlertableEvent>();

    public Alerter( IDateTimeSource dateTimeSource )
    {
        _dateTimeSource = dateTimeSource;
    }

    public void AddEvent( AlertableEvent eventToAdd )
    {
        _events.Add( eventToAdd );
    }

    /// <summary>
    /// provides a list of today's events
    /// </summary>
    /// <returns></returns>
    public List<AlertableEvent> GetTodaysEvents()
    {
        return _events.Where( x => x.When.Date == _dateTimeSource.Now.Date ).ToList();
    }
}

Example use of TestableDateTimeSource

    [TestMethod]
    public void Alerter_GetTodaysEvents_TestableDateTimeWorks()
    {
        // configure the test.  Our test will pretend that today is 4th May 2020 
        TestableDateTimeSource dateTimeSource = new TestableDateTimeSource( new DateTime( 2020, 5, 4, 0,0,0, DateTimeKind.Local ));
        // inject the TestableDateTimeSource into the business object
        Alerter alerter = new Alerter( dateTimeSource );
        // add 4 events, two of which will be on the day of interest
        alerter.AddEvent( new AlertableEvent() { When = new DateTime( 1066, 9, 28, 0, 0, 0, DateTimeKind.Local ), Description = "William lands" } );
        alerter.AddEvent( new AlertableEvent() { When = new DateTime( 1215, 6, 15, 0, 0, 0, DateTimeKind.Local ), Description = "Magna carta agreed" } );
        alerter.AddEvent( new AlertableEvent() { When = new DateTime( 2020, 5, 4, 18, 10, 0, DateTimeKind.Local ), Description = "I am writing this test" } );
        alerter.AddEvent( new AlertableEvent() { When = new DateTime( 2020, 5, 4, 19, 0, 0, DateTimeKind.Local ), Description = "I will start dinner" } );

        // call the function under test
        var events = alerter.GetTodaysEvents();

        // we expect to find 2 events for the 4th May 2020
        Assert.AreEqual( 2, events.Count );
    }
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.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 601 5/5/2020