Thursday, August 05, 2010

Dates and Times and Timezones and .NET

Yesterday I learned something about parsing dates and times.  I was trying to get a .NET application to process date/time strings of the form "2010-08-04T16:30:00 CEST" using DateTime.TryParse.  This approach didn't work because .NET doesn't grok timezone strings like "CEST" (Central European Summer Time).

My first thought was that since CEST is defined as UTC+2 (i.e., Universal Coordinated Time plus two hours), I could rewrite these date/time strings as "2010-08-04T16:30:00+02:00", which DateTime.TryParse can handle (note that the timezone delta on the end must be subtracted from the preceding date/time to obtain UTC).  My attempts to tackle this were stymied by the fact that timezone names are not unique.  For example, CST can mean either Central Summer Time or Central Standard Time, and that's just in Australia!

So, to conclude, for the love of all that is holy, don't use timezone names in your date/time strings.  Use attached UTC deltas instead.

[For those who are wondering, as I did, the difference between UTC and GMT is never allowed to exceed 0.9s; UTC is atomic clock time with some leap seconds added here and there; GMT is not.]

No comments: