Hey, All, back in March of 2020 a user named Kolaha posted the following code as a response to someone trying to determine when DST was active and inactive. This code works, seemingly fine, to calculate the day date of the last Sunday in March and October when Europe, apparently, does their DST changing. I'm trying to do this in Los Estados Unidos where DST is the 2nd Sunday of March and the first Sunday of November. To that end, I'm trying to understand how this works. This is probably more a question for "date math" irrespective of how the math gets done (Arduino, PC, calculator, etc) but I'll ask here: How the heck does this work (the calcs for x1 and x2)?
// European Daylight Saving Time / Summertime
bool DST() {
byte yy = year.now() % 100; // adjust your RTC functions call
byte mm = month.now();
byte dd = day.now();
byte x1 = 31 - (yy + yy / 4 - 2) % 7; //last Sunday March
byte x2 = 31 - (yy + yy / 4 + 2) % 7; // last Sunday October
return (mm > 3 && mm < 10) || (mm == 3 && dd >= x1) || (mm == 10 && dd < x2);
}
To look at the "byte1 = " line, where yy = the two digit year
order of operations means we do 25 / 4 first = 6
next we add that to the other 25 + 6 = 31
next we subtract 2 from that 31 - 2 = 29
next we mod that with 7 to get 29 % 7 = 1
finally we subtract that from 31 to get the last Sunday in March 31 - 1 = 30
3/30/25 is the last Sunday in March.
I understand the math to be able to do it but I do not understand how it is working so I am unable to know/determine if I can modify it to calculate the second Sunday.
I immediately see that I can use 30 % 7 = 2 to get the date for the first Sunday. I can then add 7 to that to get 9 which (3/9/25) is the second Sunday of the month. That's kludgy and, more than that, I'd like to know why the formula posted by Kolaha works.
Thank you for your time.
--HC
Those calculations are simplified from the general formula to calculate the day of week from the date, and are specific to the European convention for the switch to DST.
See for example https://artofmemory.com/blog/how-to-calculate-the-day-of-the-week/
1 Like
Thank you. I used to think I was smart. Not after realizing someone, somewhere, figured all that out. And someone else found a way to abbreviate it for certain instances.
--HC
Naturally, someone has also figured out the equivalent formulas for the US:
https://stackoverflow.com/questions/5590429/calculating-daylight-saving-time-from-only-date
Be careful if you are in the US! With the new administration, referring to the US as Los Estados Unidos might lead to rapid deportation 
4 Likes
Well, of course they did. I spent a couple of hours trying to figure out a fast way to get those Sunday dates...#$%# Daylight Savings Time. That link you sent was awesome. While I can't remember that stuff and impress folks at the parties I don't go to, I was able to modify the code I posted to get the last Sunday in November and then I just mod 7 that for November and the same for March then plus 7 to get the first and second Sunday, respectively, and just southern engineer the heck out of it. It works and tests out.
Man, I had to to re-up my DL...they wanted a birth certificate or a passport. Don't we let people vote without a state-issued ID? Maybe I can deported to England...expand my vocabulary.
Thank you again.
--HC
For future readers:
The final answer is this, in two parts:
-
reading the article that jremington posted I see that the magic month code for March is the same as the one for November. I changed the "byte x2..." line to include "-2" instead of "+2" making it identical to the "byte x1..." line.
-
knowing the last Sunday date it was easy to mod that number by 7 to get the date of the first Sunday (LastSundayDate % 7). I then beat it into submission for March by simply adding 7.
I still don't have an understanding how that formula was derived or how it came up with the last Sunday in the month as opposed to another day in the month but it works and I'm rolling on.
--HC