Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding DateTime.MinValue causing issue

We are trying to override the DateTime.MinValue in our application, but by doing it we noticed that our Web services are timing-out, following is a sample code. Not sure what is wrong/what we are missing.

 public MainWindow()
 {
     //Introducing this.. Causes timeout of the webservice call...
     typeof(DateTime).GetField("MinValue").SetValue(typeof(DateTime),new DateTime(1900, 1, 1));
     var yesitworks= DateTime.MinValue;
     InitializeComponent();
     ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
     //Below call will timeout...
     var value =client.GetData(10);
}

PS: This might not be the best solution for what we are trying resolve but now its more of curiosity as to why it is not working? how is it related.

like image 341
Vikram Avatar asked Aug 19 '26 09:08

Vikram


2 Answers

DateTime.MinValue is a static readonly field. That means that library authors will not expect it to change, and may write code that depends on it having the expected value.

Hence, you should not change the value of DateTime.MinValue.

For example, a library may use it as the default value for a variable:

private mostRecentDate= DateTime.MinValue;
foreach (var date in myDates)
{
    if (date > mostRecentDate)
    {
        mostRecentDate= date;
    }
}

// Do something with the most recent date in myDates...

In this example, if myDates only contained dates earlier than your new value for DateTime.MinValue, then this code would set mostRecentDate to DateTime.MinValue rather than the latest date in myDates.

While this rather contrived example may not be good programming practise (for example, you could use Nullable instead), it is valid code, whose behaviour would be changed if you changed the value of DateTime.MinValue.

The point is that libraries you are using could also be dependant on the value on DateTime.MinValue, so changing it could break them. You are llucky in so far as you found out that this introduced a bug early. If you are unlucky, you would not see a problem until your software had gone live and some corner case was hit.

like image 55
Ergwun Avatar answered Aug 22 '26 00:08

Ergwun


I had a similar problem recently.
You didn't tell why you wanted to override DateTime.MinValue, but I guess the reason is similar to mine:

I have a server written in .NET, which has .NET clients and (via COM-Interop) MS Access clients.

The clients pass DateTime values, and the server needs to check whether they passed a "real" value or DateTime.MinValue.

My problem was:

  • .NET's DateTime.MinValue is January 1st of the year 1
  • The smallest possible value for VBA's Date type is January 1st of the year 100

⇒ Checking for DateTime.MinValue didn't work when the data was coming from MS Access, because Date variables in Access can't hold a date as small as .NET's DateTime.MinValue.

At that point I tried to override DateTime.MinValue too, and found out it doesn't work.

My solution was to write an extension method for DateTime:

public static class DateTimeExtensions
{
    public static bool MinValue(this DateTime input)
    {
        // check the min values of .NET *and* VBA
        if (input == DateTime.MinValue || input == new DateTime(100, 1, 1))
        {
            return true;
        }

        return false;
    }
}

For the code in your question, it would need to look like this:

public static class DateTimeExtensions
{
    public static bool MinValue(this DateTime input)
    {
        if (input == new DateTime(1900, 1, 1))
        {
            return true;
        }

        return false;
    }
}

Usage:

DateTime theDate = DateTime.Now;

// vanilla .NET
bool isMin1 = (theDate == DateTime.MinValue);

// with the extension method
bool isMin2 = theDate.MinValue();
like image 35
Christian Specht Avatar answered Aug 21 '26 22:08

Christian Specht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!