DateTime.ParseExact “R” the liar liar pants on fire API aka how do I make Response.Cache.SetLastModified work right

I generally have orders of praise for .NET (most specifically C#) and it’s immensely robust CLR.

Today I had to resolve bugs that have exist in the .NET API since atleast 1.1 if not earlier, this is just stupid.

When you’re working with Http Caching and the method

Response.Cache.SetLastModified()

This will emit a date over the wire effectively doing:

DateTimeUtil.ConvertToUniversalTime(date).ToString(“R”)

This returns us a date string similar to “Sat, 01 Nov 2008 19:35:00 GMT”.

Now the major major GOTCHA:

#FAIL DateTime.ParseExact(“Sat, 01 Nov 2008 19:35:00 GMT”, “R”, CultureInfo.InvariantCulture)#FAIL

will not actually parse this exactly. It will give you some other invalid time because of bugs in handling timezones.

The proper way to parse this is:

DateTime.Parse(“Sat, 01 Nov 2008 19:35:00 GMT”, null, DateTimeStyles.AdjustToUniversal);

Very deceptive, very deceitful of an API. Details explained at length on MSDN

BloggingContext.ApplicationInstance.CompleteRequest();

Advertisements