Rants and Raves

Thanks for visiting my blog!

Fun with .NET 3.5 - Part 1
Fun with .NET 3.5 - Part 1
December 27, 2024

I’ve been digging into the .NET 3.5 runtime to find any tidbits that didn’t make the front pages of everyone’s blogs this last year.  You can find the big stories anywhere else, I want to cover the small interesting stories of what’s new in the Framework.  I’ll be covering some classes over the next few days that I’ve found in the .NET 3.5 Framework (and hopefully I don’t find anything I didn’t notice in 2.0 or 3.0, but feel free to correct me if I am wrong).

Here’s the first tidbit: TimeZoneInfo.  This class allows you to enumerate and convert from specific time zones.  We could do convertions to and from UTC and if we knew the offsets we could do these calculations before but now this new class allows us to actually look at the time zones.  It reads these time zones from the registry so it is not hard coded with the time zones and any system updates should be supported.  Here is how it could be used:

  class Program
  {
    static void Main(string[] args)
    {
      var zones = from zone in TimeZoneInfo.GetSystemTimeZones()
                  where zone.StandardName == "Pacific Standard Time"
                  select zone;
      if (zones.Count() == 1)
      {
        TimeZoneInfo zoneInfo = zones.First();
        DateTime now = DateTime.Now;
        Console.WriteLine("Current Time: {0}{1}In {2}: {3}", 
          now, 
          Environment.NewLine, 
          zoneInfo.StandardName, 
          TimeZoneInfo.ConvertTime(now, zoneInfo));
      }
      Console.Read();
    }
  }

Notice I am using a simple LINQ query to find the right timezone and then using it to convert local time to a specific timezone.  Enjoy!