Blog of Adrian Anttila, containing my thoughts, comments and questions. RSS Feed


TimeZoneInfo : Another reason to love .NET 3.5

One of the new classes in .NET 3.5 is TimeZoneInfo. MSDN's vague and all-encompassing description states "Represents any time zone in the world." Scrolling further down, you'll find a list of actual things that TimeZoneInfo can do:

  • Retrieving a time zone that is already defined by the operating system.
  • Enumerating the time zones that are available on a system.
  • Converting times between different time zones.
  • Creating a new time zone that is not already defined by the operating system.
  • Serializing a time zone for later retrieval.

Personally, I'm using TimeZoneInfo for all of my DateTime conversions to support different time zones. Performing the conversion is as easy as calling ConvertTimeBySystemTimeZoneId. Here are a few examples:

DateTime pstDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime, TimeZoneInfo.Utc.Id, "Pacific Standard Time");

DateTime utcDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(pstDateTime, "Pacific Standard Time", TimeZoneInfo.Utc.Id);

TimeZoneInfo supports daylight savings time (DST), and makes supporting time zones much easier than rolling your own solution.

 
Posted by Adrian Anttila | 2 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Sunday, 6 Apr 2008 10:52 by Re: TimeZoneInfo : Another reason to love .NET 3.5
If only there were a clean way to get the TZ for a web client and make use of this in code behind for rendering on the client.

Thursday, 17 Apr 2008 02:36 by TimeZoneInfo & SQLCLR
Too bad we can't call TimeZoneInfo methods from CLR stored procedure due to Host Protection Attributes...

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation