Skip
# Magical DST Formula, or just funky math?

(adsbygoogle = window.adsbygoogle || []).push({});

It does care about leap years, just in a sneaky way. Try plugging in successive numbers into the floor (year*5/4) function and you get:

The reason this manages to account for leap years is because the calendar shifts one day later in the week every common year, but two days later every leap year. This is because 365 days equal 52 weeks + one day, but 366 days equal 52 weeks + two days. So New Year's Day 2007 fell on a Monday; New Year's Day 2008 fell on a Tuesday, one day in the week later; and New Year's Day 2009 will fall on a Thursday, two weekdays later (because there was a leap day this year that pushed it back an extra day.)

posted by Johnny Assay at 5:26 AM on November 24, 2008

Post

# Magical DST Formula, or just funky math?

November 23, 2008 8:21 PM Subscribe

Please help explain why this formula works.

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

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.

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.

*How does it not care about leap years?*

It does care about leap years, just in a sneaky way. Try plugging in successive numbers into the floor (year*5/4) function and you get:

- 2005 -> 2506
- 2006 -> 2507
- 2007 -> 2508
- 2008 -> 2510
- 2009 -> 2511
- 2010 -> 2512
- 2011 -> 2513
- 2012 -> 2515

The reason this manages to account for leap years is because the calendar shifts one day later in the week every common year, but two days later every leap year. This is because 365 days equal 52 weeks + one day, but 366 days equal 52 weeks + two days. So New Year's Day 2007 fell on a Monday; New Year's Day 2008 fell on a Tuesday, one day in the week later; and New Year's Day 2009 will fall on a Thursday, two weekdays later (because there was a leap day this year that pushed it back an extra day.)

posted by Johnny Assay at 5:26 AM on November 24, 2008

This thread is closed to new comments.

posted by chndrcks at 9:29 PM on November 23, 2008