Sunday, June 4, 2017

DateTime parsing in .NET

Good percentage of bugs and test failures comes from the way date is parsed.
Popular example, a unit test is created, runs on local machine without any problems, then on the test agents, sometimes it passes, and other times fails.
Usually this happens when there is a date stored in UTC for example, then we parse it without specifying date kind, which may change the time value in some cases.

Example below for different ways to parse date, and the effect of that on a time comparison.
Whenever the parsed date kind is local, and the time is adjusted, the comparison is not as we expect.
In the below examples, i am comparing the dates checking if less than 8:40 and expecting all to be true. Some of the them the comparison was giving false, due to time adjustment.

namespace DateParsing
{
    using System.Globalization;
    using System.Xml;

    class Program
    {
        static void Main(string[] args)
        {
            Parse("2017-06-01T08:33:49Z");
            Parse("2017-06-01T08:33:49");
            Parse("2017-06-01T08:00:00.0000000Z");
            ParseByFormat("2017-06-01 08:33:49", "yyyy-MM-dd HH:mm:ss");
            Console.ReadKey();
        }

        private static void Parse(string dateString)
        {
            Console.WriteLine("========== input string is: {0} ===============", dateString);
            var date1 = DateTime.Parse(dateString);
            PrintDate(date1, "Using parse, no date style");
            var date2 = DateTime.Parse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
            PrintDate(date2, "Using parse, Adjust to universal style");
            var date3 = DateTime.Parse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            PrintDate(date3, "Using parse, Assume universal style");
            var date4 = XmlConvert.ToDateTime(dateString, XmlDateTimeSerializationMode.Utc);
            PrintDate(date4, "Using XmlConvert, SerializationMode UTC");
        }

        private static void ParseByFormat(string dateString, string format)
        {
            Console.WriteLine("========== input string is: {0} ===============", dateString);
            var date1 = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
            PrintDate(date1, "Using parse exact, no date style");
            var date2 = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
            PrintDate(date2, "Using parse exact, Adjust to universal style");
            var date3 = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            PrintDate(date3, "Using parse exact, Assume universal style");
            var date4 = XmlConvert.ToDateTime(dateString, format);
            PrintDate(date4, "Using XmlConvert, SerializationMode not specified");
        }

        static void PrintDate(DateTime date, string format)
        {
            DateTime x = new DateTime(2017, 06, 01, 08, 40, 00);
            Console.WriteLine("Date is {0}, date kind is {1}, {2}", date, date.Kind, format);
            Console.WriteLine("{0} < than {1} is {2} ", date, x, date < x);
        }
    }
}


========== input string is: 2017-06-01T08:33:49Z ===============
Date is 01/06/2017 09:33:49, date kind is Local, Using parse, no date style
01/06/2017 09:33:49 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:33:49, date kind is Utc, Using parse, Adjust to universal style
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 09:33:49, date kind is Local, Using parse, Assume universal style
01/06/2017 09:33:49 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:33:49, date kind is Utc, Using XmlConvert, SerializationMode UTC
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True

========== input string is: 2017-06-01T08:33:49 ===============
Date is 01/06/2017 08:33:49, date kind is Unspecified, Using parse, no date style
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 08:33:49, date kind is Unspecified, Using parse, Adjust to universal style
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 09:33:49, date kind is Local, Using parse, Assume universal style
01/06/2017 09:33:49 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:33:49, date kind is Utc, Using XmlConvert, SerializationMode UTC
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True

========== input string is: 2017-06-01T08:00:00.0000000Z ===============
Date is 01/06/2017 09:00:00, date kind is Local, Using parse, no date style
01/06/2017 09:00:00 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:00:00, date kind is Utc, Using parse, Adjust to universal style
01/06/2017 08:00:00 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 09:00:00, date kind is Local, Using parse, Assume universal style
01/06/2017 09:00:00 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:00:00, date kind is Utc, Using XmlConvert, SerializationMode UTC
01/06/2017 08:00:00 < than 01/06/2017 08:40:00 is True

========== input string is: 2017-06-01 08:33:49 ===============
Date is 01/06/2017 08:33:49, date kind is Unspecified, Using parse exact, no date style
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 08:33:49, date kind is Unspecified, Using parse exact, Adjust to universal style
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True
Date is 01/06/2017 09:33:49, date kind is Local, Using parse exact, Assume universal style
01/06/2017 09:33:49 < than 01/06/2017 08:40:00 is False
Date is 01/06/2017 08:33:49, date kind is Unspecified, Using XmlConvert, SerializationMode not specified
01/06/2017 08:33:49 < than 01/06/2017 08:40:00 is True