Possible Bug With DateTime.Parse?

UPDATE: I think a good measure of a blog is the intelligence and quality of the comments. This comments in response to this post makes my blog look good (not all do).

As several commenters pointed out, the function returns a local DateTime adjusted from the specified UTC date. By calling ToUniversalTime() on the result, I get the behavior I am looking for. That’s why I ask you smart people before making an ass of myself on the bug report site.

Before I post this as a bug, can anyone tell me why this test fails when I think it should pass?

[Test]
public void ParseUsingAssumingUniversalReturnsDateTimeKindUtc()
{
  IFormatProvider culture = new CultureInfo("en-US", true);
  DateTime utcDate = DateTime.Parse("10/01/2006 19:30", culture, 
    DateTimeStyles.AssumeUniversal);
  Assert.AreEqual(DateTimeKind.Utc, utcDate.Kind, 
    "Expected AssumeUniversal would return a UTC date.");
}

What is going on here is I am calling the method DateTime.Parse passing in a DateTimeStyle.AssumeUniversal as an argument. My understanding is that it should indicate to the Parse method that the passed in string denotes a Coordinated Univeral Time (aka UTC).

But when I check the Kind property of the resulting DateTime instance, it returns DatTimeKind.Local rather than DatTimeKind.Utc.

The unit test demonstrates what I think should happen. Either this really is a bug, or I am wrong in my assumptions, in which case I would like to know, how are you supposed to parse a string representing a date/time in the UTC timezone?

What others have said

Requesting Gravatar... Eric Oct 01, 2006 1:00 PM
# re: Possible Bug With DateTime.Parse?
If you convert the date to a string (just using ToString()), does it give the value in local time equivalent to 10/01/2006 19:30 UTC (i.e. if you're in GMT-5, does it give 10/01/2006 14:30)?

If so, it's probably not a bug -- it just autoconverts to local time when you construct it (possibly to maintain a particular backcompat story).
Requesting Gravatar... Mark Oct 01, 2006 9:47 PM
# re: Possible Bug With DateTime.Parse?
Based on the documentation, there is no way to specify the kind of date that DateTime.Parse will produce. The AssumeUniversal is just a bit that helps DateTime.Parse do its work.

By default, I think DateTime always converts everything to "DateTimeKind.Local". If you issue:
DateTime utcDate2 = utcDate.ToUniversalTime();
and look at utcDate2.Kind, it returns "DateTimeKind.Utc".

I agree that it is confusing that you are specifying AssumeUniversal and it is returning a Local DateTime. It looks like the DateTimeStyles are strictly meant to help Parse() do its thing. It doesn't affect the output.

You could wrap it in DateTime.SpecifyKind():
DateTime utcDate = DateTime.SpecifyKind(DateTime.Parse("10/01/2006 19:30", culture, DateTimeStyles.AssumeUniversal), DateTimeKind.Utc);
and that will give you the desired results.
Requesting Gravatar... Joshua Flanagan Oct 01, 2006 10:42 PM
# re: Possible Bug With DateTime.Parse?
I thought it was a bug from your description, too, until I ran it for myself. Your last sentence makes it sound like it didn't parse the datetime as universal - it DID - it just auto-converted it to a local time. Did you not notice that the time changed?
If it wasn't parsing universal times, the time component would always be exactly what you specified in the string, no matter what time zone you are in. That's not the case.
Requesting Gravatar... Haacked Oct 02, 2006 12:37 AM
# re: Possible Bug With DateTime.Parse?
Thanks everyone. You are indeed correct. It returns a DateTime with the correct local time. By converting it back to UniversalTime, I get what I would expect.

That's why I ask you smart people before making an ass of myself. ;)
Requesting Gravatar... madpew Feb 25, 2010 1:45 AM
# re: Possible Bug With DateTime.Parse?
Thanks lot. I was struggling with this for about 7 hours until I discovered this post + comments. Thanks for saving my life. (I was getting insane with all those date offset blah blah)

What do you have to say?

(will show your gravatar)
Please add 6 and 1 and type the answer here: