Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating daylight saving time from only date

Tags:

I am working with an Arduino and a real time clock chip. The chip compensates for leap years and such, so it will always have the correct date, but it does not handle daylight saving time, I assume due to regional complications. The clock can give me the day, month, and year (1 based) and the day of the week (sunday = 0 to saturday = 6).

Because I need to compare with user entered dates and times, I need to know the date and time adjusted for daylight saving time. If the current date is in daylight saving time I can simply add an hour to the time from the clock and I have what I need.

The hard part is determining whether I am in daylight saving time or not, because it changes from year to year. I only care that it works in my location (Mountain Time). There doesn't appear to be any comprehensive date libraries for my platform, and I feel like that would be overkill anyway. Is there a simple formula to determine if I am in DST or not?

like image 631
Muggz Avatar asked Apr 08 '11 04:04

Muggz


People also ask

What is the function to offset the date for daylight saving?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

Is daylight Savings 1 hour behind?

When does Daylight Saving begin and end? Daylight Saving Time begins at 2am on the first Sunday in October when clocks are put forward one hour. Daylight Saving Time ends at 2am (3am Daylight Saving Time) on the first Sunday in April when clocks are put back one hour.


2 Answers

This is actually deceptively simple. There are a few facts that will help us:

  1. 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.
  2. The second Sunday in March will always be between the 8th and the 14th inclusive.
  3. The first Sunday in November will always be between the 1st and 7th inclusive.
  4. The day of week numbering is quite convenient because the day - day of week will give you the previous Sunday.

These facts lead to the following code (C#, but trivially portable to your platform):

    public bool IsDST(int day, int month, int dow)     {         //January, february, and december are out.         if (month < 3 || month > 11) { return false; }         //April to October are in         if (month > 3 && month < 11) { return true; }         int previousSunday = day - dow;         //In march, we are DST if our previous sunday was on or after the 8th.         if (month == 3) { return previousSunday >= 8; }         //In november we must be before the first sunday to be dst.         //That means the previous sunday must be before the 1st.         return previousSunday <= 0;     } 

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.

I wrote a quick unit test and verified that this code agrees with TimeZone.IsDayLightSavingsTime() for all dates from 1800 to 2200. 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).

like image 102
captncraig Avatar answered Oct 08 '22 17:10

captncraig


Code for Central Europe (tested for every day in range 2014-3000 year)

    public static bool IsDst(int day, int month, int dow)     {         if (month < 3 || month > 10)  return false;          if (month > 3 && month < 10)  return true;           int previousSunday = day - dow;          if (month == 3) return previousSunday >= 25;         if (month == 10) return previousSunday < 25;          return false; // this line never gonna happend     } 

Test function

    static void Main(string[] args)     {         TimeZoneInfo tzf2 = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");          var date = new DateTime(2014, 01, 1, 5, 0,0);         bool wasSummer = false;          while (date <= new DateTime(3000,1,1))         {                                                      var dow = (int) date.DayOfWeek;              var isDst = IsDst(date.Day, date.Month, dow);                             DateTime f2 = TimeZoneInfo.ConvertTime(date, tzf2);             var isSummer = f2.IsDaylightSavingTime();              if (isSummer != isDst)             {                 Console.WriteLine("ERROR");                 Console.WriteLine(date);             }              if (isSummer != wasSummer)             {                 Console.WriteLine(date.AddDays(-1).ToShortDateString());             }              date = date.AddDays(1);             wasSummer = isSummer;         }          Console.ReadKey(); 

}

like image 25
akworob Avatar answered Oct 08 '22 17:10

akworob