![]() ![]() While it is easy to calculate whether a particular date is in DST for a particular location under the current rules, do note that DST is at the whim of politicians and could change at any point. Var isSummer = f2.IsDaylightSavingTime() Ĭonsole.WriteLine(date.AddDays(-1).ToShortDateString()) Var isDst = IsDst(date.Day, date.Month, dow) ĭateTime f2 = TimeZoneInfo.ConvertTime(date, tzf2) TimeZoneInfo tzf2 = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time") Test function static void Main(string args) Return false // this line never gonna happend If (month = 10) return previousSunday < 25 I did not account for the 2 am rule, but you could easily do that check if the day of week is Sunday and the date is between 8 and 14 (in March) or 1 and 7 (in November).Ĭode for Central Europe (tested for every day in range 2014-3000 year) public static bool IsDst(int day, int month, int dow) I wrote a quick unit test and verified that this code agrees with TimeZone.IsDayLightSavingsTime() for all dates from 1800 to 2200. It turns out you don't even need to know the year to do this, as long as you can trust your day of the week value. That means the previous sunday must be before the 1st. ![]() In november we must be before the first sunday to be dst. January, february, and december are out. These facts lead to the following code (C#, but trivially portable to your platform): public bool IsDST(int day, int month, int dow) The day of week numbering is quite convenient because the day - day of week will give you the previous Sunday.The first Sunday in November will always be between the 1st and 7th inclusive.The second Sunday in March will always be between the 8th and the 14th inclusive.In most of the US, DST starts on the second Sunday of March and ends on the first Sunday of November, at 2:AM both times. ![]()
0 Comments
Leave a Reply. |