CSharp - Date Time TimeZoneInfo Type

Introduction

TimeZoneInfo.Local returns the current local time zone:

Demo

using System;
class MainClass{/*from   ww w .j  a v  a2s  . c  o m*/
   public static void Main(string[] args){
     TimeZoneInfo zone = TimeZoneInfo.Local;
     Console.WriteLine (zone.StandardName);      // Pacific Standard Time
     Console.WriteLine (zone.DaylightName);      // Pacific Daylight Time
   }
}

Result

TimeZoneInfo provides IsDaylightSavingTime and GetUtcOffset methods and they accept either a DateTime or a DateTimeOffset.

You can obtain a TimeZoneInfo for any of the world's time zones by calling FindSystemTimeZoneById with the zone ID.

The following code gets time zone information on Western Australia

Demo

using System;
class MainClass// w w  w .j  a  va  2 s.  com
{
   public static void Main(string[] args)
   {
     TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById
                       ("W. Australia Standard Time");
     Console.WriteLine (wa.Id);                
     Console.WriteLine (wa.DisplayName);       
     Console.WriteLine (wa.BaseUtcOffset);     
     Console.WriteLine (wa.SupportsDaylightSavingTime);  
   }
}

Result

Id property corresponds to the value passed to FindSystemTimeZoneById.

static GetSystemTimeZones method returns all world time zones and you can list all valid zone ID strings as follows:

foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
       Console.WriteLine (z.Id);

static ConvertTime method converts a DateTime or DateTimeOffset from one time zone to another.

GetAdjustmentRules returns a declarative summary of all daylight saving rules that apply to all years.

Each rule has a DateStart and DateEnd indicating the date range within which the rule is valid:

TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");
foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules())
  Console.WriteLine ("Rule: applies from " + rule.DateStart +
                                    " to " + rule.DateEnd);

Each AdjustmentRule has a DaylightDelta property of type TimeSpan and properties called DaylightTransitionStart and DaylightTransitionEnd.

We can demonstrate this by enumerating the adjustment rules for our time zone:

TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById
                  ("W. Australia Standard Time");
foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules())
{
  Console.WriteLine ("Rule: applies from " + rule.DateStart + " to " + rule.DateEnd);

  Console.WriteLine ("   Delta: " + rule.DaylightDelta);

  Console.WriteLine ("   Start: " + FormatTransitionTime
                                   (rule.DaylightTransitionStart, false));

  Console.WriteLine ("   End:   " + FormatTransitionTime
                                   (rule.DaylightTransitionEnd, true));
  Console.WriteLine();
}