using System; using System.Globalization; public class MainClass { public static void Main() { 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(); } } static string FormatTransitionTime(TimeZoneInfo.TransitionTime tt, bool endTime) { if (endTime && tt.IsFixedDateRule && tt.Day == 1 && tt.Month == 1 && tt.TimeOfDay == DateTime.MinValue) return "-"; string s; if (tt.IsFixedDateRule) s = tt.Day.ToString(); else s = "The first second third fourth last".Split()[tt.Week - 1] + " " + tt.DayOfWeek + " in"; return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames[tt.Month - 1] + " at " + tt.TimeOfDay.TimeOfDay; } }
Author: coder
TimeZoneInfo also provides IsDaylightSavingTime and GetUtcOffset methods–the difference is that they accept either a DateTime or DateTimeOffset.
using System; public class MainClass { public static void Main() { TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time"); Console.WriteLine(wa.Id); Console.WriteLine(wa.DisplayName); Console.WriteLine(wa.BaseUtcOffset); Console.WriteLine(wa.SupportsDaylightSavingTime); } }
The TimeZoneInfo class works in a similar manner. TimeZoneInfo.Local returns the current local time zone:
using System; public class MainClass { public static void Main() { TimeZoneInfo zone = TimeZoneInfo.Local; Console.WriteLine(zone.StandardName); Console.WriteLine(zone.DaylightName); } }
The Id property corresponds to the value passed to FindSystemTimeZoneById.
using System; public class MainClass { public static void Main() { foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones()) Console.WriteLine(z.Id); } }
Convert To TimeZone
//Octavalent Extension Methods //http://sdfasdf.codeplex.com/ //Library of extension methods for .Net create by Octavalent (www.octavalent.nl) using System; public static class DateTimeExtensions { public static DateTime ConvertToTimeZone(this DateTime dateTime, string SystemTimeZoneId) { dateTime = dateTime.ToUniversalTime(); dateTime = dateTime.Add(TimeZoneInfo.FindSystemTimeZoneById(SystemTimeZoneId).GetUtcOffset(dateTime)); return dateTime; } }
The static TimeZone.CurrentTimeZone method returns a TimeZone object based on the current local settings
using System; public class MainClass { public static void Main() { TimeZone zone = TimeZone.CurrentTimeZone; Console.WriteLine(zone.StandardName); Console.WriteLine(zone.DaylightName); } }
The GetDaylightChanges method returns specific daylight saving information for a given year:
using System; using System.Globalization; public class MainClass { public static void Main() { TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime day = zone.GetDaylightChanges(2008); Console.WriteLine(day.Start); Console.WriteLine(day.End); Console.WriteLine(day.Delta); } }