Save the date

Or how to save yourself from the date – the DateTime.Now() call specifically.

I was privy to a production problem where a process that was calling DateTime.Now() rolled past midnight and everything went to pot.

These days, I try to abstract myself away from all that fun with an interface.

public interface ITimeActions
 {
 DateTime GetTodaysDate();
 }

The concrete implementation can be the actual call:
public  class TimeActions : ITimeActions
 {
      public DateTime GetTodaysDate()
           {
                return DateTime.Now;
           }
 }

Now, whenever I need the current date, I implement the interface instead of the concrete and I can test my code or save myself if I need to run for a different day.

Happy Coding,

-J

Leave a comment

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