DaylightTime daylight = TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Today.Year);But that only gives it for the time zone of the server. I needed to get a System.Globalization.DaylightTime for a timezone that observes Daylight Saving.
public static DaylightTime GetDaylightChanges(TimeZoneInfo InTimeZoneInfo, int InYear) { TimeZoneInfo.AdjustmentRule ruleFound = null;
TimeZoneInfo.AdjustmentRule[] adjustments = InTimeZoneInfo.GetAdjustmentRules(); if (adjustments.Length == 0) { throw new Exception(InTimeZoneInfo.StandardName + " has no adjustment rules"); } //Find the correct adjustment rule foreach (TimeZoneInfo.AdjustmentRule adjustment in adjustments) { if (adjustment.DateStart.Year <= InYear && adjustment.DateEnd.Year >= InYear) ruleFound = adjustment; } if (ruleFound == null) { throw new Exception("No TimeZoneInfo.AdjustmentRule found for TimeZoneInfo " + InTimeZoneInfo.StandardName +" for year " + InYear); }
DaylightTime outDaylightTime = new DaylightTime( GetDateTime(InYear, ruleFound.DaylightTransitionStart) , GetDateTime(InYear, ruleFound.DaylightTransitionEnd) , ruleFound.DaylightDelta);
return outDaylightTime; }Here is the usage.
TimeZoneInfo PacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");DaylightTime daylight = CustomTimeZone.GetDaylightChanges(PacificTimeZone, 2010);Console.WriteLine("daylight.Start = " + daylight.Start);Console.WriteLine("daylight.End = " + daylight.End);Console.WriteLine("daylight.Delta = " + daylight.Delta);
0 comments:
Post a Comment