Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0279 Daylight saving time and TimeZoneInfo

IsInvalidTime returns true if a DateTime is within the hour that's skipped when the clocks move forward. IsAmbiguousTime returns true if a DateTime or DateTimeOffset is within the hour that's repeated when the clocks move back. GetAmbiguousTimeOffsets returns an array of TimeSpans representing the valid offset choices for an ambiguous DateTime or DateTimeOffset. using System; using System.Text; using System.Globalization; class Sample { public static void Main() { TimeZoneInfo zone = TimeZoneInfo.Local; DateTime dt1 = new DateTime(2008, 1, 1); Console.WriteLine(zone.IsInvalidTime(dt1)); Console.WriteLine(zone.IsAmbiguousTime(dt1)); Console.WriteLine(zone.GetAmbiguousTimeOffsets(dt1)); } } The output: False False Unhandled Exception: System.ArgumentException: The supplied DateTime is not in a n ambiguous time range. Parameter name: dateTime at System.TimeZoneInfo.GetAmbiguousTimeOffsets(DateTime dateTime) at Sample.Main()