DateTime.MinValue – be careful!

We’ve just had a support call at work where the user was running our application in Singapore.

Our investigation uncovered an interesting “feature” of using DateTime.MinValue.

Have a look at the following test:

namespace Stuff
{
    using System;
    using NUnit.Framework;

    [TestFixture]
    public class MinDateTimeOffset
    {
        [Test]
        public void Testing_MinValue_in_DateTime()
        {
            Action<string> p = Console.WriteLine;

            // This is Now in the current timezone
            var nowOffset = new DateTimeOffset(DateTime.Now);
            p("Current time:");
            p("- current timezone: " + nowOffset);
            p("- UTC equivilent time: " + nowOffset.ToUniversalTime());
            p("");

            // This is the minimum time allowed in the current timezone
            var minOffset = DateTimeOffset.MinValue;
            p("DateTimeOffset.MinValue:");
            p("- current timezone: " + minOffset);
            p("- UTC equivilent time: " + minOffset.ToUniversalTime());
            p("");

            // This is UTC minimum time
            var minValue = DateTime.MinValue;
            p("DateTime.MinValue:");
            p("- current timezone: " + minValue);
            p("- UTC equivilent time: " + minValue.ToUniversalTime());
            p("");

            // Converted to the current timezone
            try
            {
                p("Trying to cast DateTime.Min to DateTimeOffset:");

                var minDateTimeAsDateTimeOffset = (DateTimeOffset)minValue;
                p("- current timezone: " + minDateTimeAsDateTimeOffset);
                p("- UTC equivilent time: " + minDateTimeAsDateTimeOffset.ToUniversalTime());
            }
            catch (Exception ex)
            {
                p("Error: " + ex.Message);
            }
        }
    }
}

We are based in London, and when we run it, we see this output:


Current time:
- current timezone: 31/10/2012 11:46:35 +00:00
- UTC equivilent time: 31/10/2012 11:46:35 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 00:00:00

Trying to cast DateTime.Min to DateTimeOffset:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

However, when run in Singapore, you get this:

Current time:
- current timezone: 31/10/2012 19:47:38 +08:00
- UTC equivilent time: 31/10/2012 11:47:38 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 00:00:00

Trying to cast DateTime.Min to DateTimeOffset:
Error: The UTC time represented when the offset is applied must be between year 0 and 10,000.
Parameter name: offset

In New York, you get this :

Current time:
- current timezone: 31/10/2012 07:48:50 -04:00
- UTC equivilent time: 31/10/2012 11:48:50 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 05:00:00

Trying to cast DateTime.Min to DateTimeOffset:
- current timezone: 01/01/0001 00:00:00 -05:00
- UTC equivilent time: 01/01/0001 05:00:00 +00:00

The value of DateTime.MinValue can not be cast to a DateTimeOffset if you are east of London!

Advertisements

2 thoughts on “DateTime.MinValue – be careful!

  1. Pingback: Office 365 Hybrid Configuration Wizard fails due to DateTime.MinValue issue | Morgan Simonsen's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s