1 minute read

I’ve been working on an application that needs to handle the case where different users and/or different servers are located in different time zones. To accommodate this, I figured it would be best to ensure that all date/time values would be stored and manipulated in universal time (UTC).

For textual representations of date/time values, I’ve used the XML canonical format CCYY-MM-DDThh:mm:ssZ, for example 2002-04-20T23:59:07Z. Unfortunately, I had wrongly assumed that using DateTime.Parse() on such a string would return my UTC date.

The fact that DateTime.Parse() converted the value to local time was a bit of a surprise to begin with. The more I think about it, though, the more sense it makes. Consider these two strings: “2002-04-20T23:59:07Z” and “2002-04-20T23:59:07”. The first is in UTC and the second is, well, we don’t know - local time I suppose. And I guess that’s the key - if the second is parsed and retains the given hour and we accept that it is in local time, it probably makes sense that the first is adjusted by the local time zone offset in order that we can make a comparison between them in a like for like manner. Just caught me off guard is all. A quick call to ToUniversalTime() and all is well again.