Calculating daylight saving time from only date

后端 未结 9 1609
春和景丽
春和景丽 2021-01-31 10:53

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 s

9条回答
  •  春和景丽
    2021-01-31 11:19

    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).

提交回复
热议问题