Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is actually deceptively simple. There are a few facts that will help us:</p> <ol> <li>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.</li> <li>The second Sunday in March will always be between the 8th and the 14th inclusive.</li> <li>The first Sunday in November will always be between the 1st and 7th inclusive.</li> <li>The day of week numbering is quite convenient because the day - day of week will give you the previous Sunday.</li> </ol> <p>These facts lead to the following code (C#, but trivially portable to your platform):</p> <pre><code> public bool IsDST(int day, int month, int dow) { //January, february, and december are out. if (month &lt; 3 || month &gt; 11) { return false; } //April to October are in if (month &gt; 3 &amp;&amp; month &lt; 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 &gt;= 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 &lt;= 0; } </code></pre> <p>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.</p> <p>I wrote a quick unit test and verified that this code agrees with <code>TimeZone.IsDayLightSavingsTime()</code> 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).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload