Magical DST Formula, or just funky math?
November 23, 2008 8:21 PM Subscribe
Please help explain why this formula works.
posted by SquidLips to science & nature (2 answers total) 3 users marked this as a favorite
So, I had a need to find a formula that would calculate whether or not a particular date was in DST or not. I actually found one, and it works perfect for what I need. But purely put of curiosity, I want to know why it works. Here is the basic formula:
floor(1 + (year * 5 / 4)) % 7
To find the day of March that DST begins, subtract the result from 14. Similarly, to find the date in November when it ends, subtract the result from 7. The formula only works for years 2007 and beyond, when the dates for DST were changed in the US.
Some basic thoughts / assumptions:
Guessing that the formulas are identical (save for the cardinal number to subtract from) because in any given year, March and November always start on the same day of the week.
Prior to 2007, the formulas are different based on which date you are finding, and though both employ roughly similar concepts, the equation is relatively more complex for April; Oct is very similar to the above.
The 14 probably comes from DST starting on the second Sunday of March, and the 7 probably comes from DST ending on the first Sunday in November. The modulo probably comes from 7 days in the week.
The formula works regardless of whether the year is a leap year, but only before the year 2100, which is one of the 'exception to the rule' years which is not a leap year. For years 2100-2199, you have to eliminate the "1 +" part of the equation. Subsequent centuries require similar minor adjustment.
Is the basic formula that simple because of some mathematical coincidence regarding those particular dates, or days of the year? How does it not care about leap years? I would love to have the logic and math explained; understanding this would make some of the other oddball calculations I have to do much easier, I think.
For those wondering... the 'floor' function is a simple function that essentially says 'round down'. So, floor(4.99) would result in 4. And the '%' operator is modulo, which is more or less integer remainder. So 7 % 3 = 1, 14 % 5 = 4, etc.
Thanks in advance to anyone who can help figure this out. Google was no help... heck, I had to weed through dozens of results just to find this formula, much less an explanation.