Ministry of Technology
Show Menu

Date Time Abstractions

Date Time Abstractions
Created by Ministry
Language
.net Framework 4.0+ / .net Standard 1.0 / C#
Type
Library

This project provides a DateTimeAccessor class, with matching interface, which allows you to obtain the current DateTime object. This allows you to inject the accessor through Dependency Injection and mock the return value for accurate testing.

This allows you to replace the following code (which cannot be tested)…

class MyClass
{
  public DateTime GetSeconds()
    => DateTime.Now.Second;            
}

with this (which can, by mocking IDateTimeAccessor and returning a specific value for 'Now')…

class MyClass
{
  private readonly IDateTimeAccessor dateTimeAccessor;

  public MyClass(IDateTimeAccessor dateTimeAccessor)
  {
    this.dateTimeAccessor = dateTimeAcessor;
  }

  public DateTime GetSeconds()
    => dateTimeAccessor.Now.Second;            
}

Supporting Functions

In addition to the core date time accessor, this library also provides a static DateTimeHelper class which provides the following DateTime conversion features as extension methods to the DateTime class...

  • ToGmtTime()
  • FromGmtTime()
  • To UnixEpochTime()
  • FromUnixEpochTime()
Visit us on GitHub