Nightstand and alarm clockIt’s that time of year again. Time to spring forward. Well, not exactly the same time as last year. The formula for daylight savings time changed for this year. That meant that I had to rewrite firmware that used daylight savings time. According to Wikipedia,

Beginning in 2007, DST will start on the second Sunday in March (March 11, 2007), and change back to standard time on the first Sunday in November (November 4, 2007). Under Section 110 of the Energy Policy Act of 2005, the U.S. Department of Energy is required to study the impact of the DST extension no later than nine months after the change takes effect. Congress has retained the right to revert to the DST schedule set in 1986 if it cannot be shown that there are significant energy savings from an extension of DST or if the extension may prove to be unpopular with the American public. One potential issue is that some northern regions on the western edge of time zones will for the first time since the 1974-75 “almost year round” DST experiment have sunrise times that occur after 8am.

In two of my controllers I use, the built in hardware clock of the PC/104 card automatically handles the daylight savings time change. I simply set a bit and it would automatically spring forward or fall back. Well, almost. One of the controllers used a realtime clock chip by Odin which didn’t come out of daylight savings time and got stuck at 1am on the last Sunday of October. Since I could no longer utilize the hardware clock, I had to handle the daylight savings time change in software. There were two cases that I needed to cover. The first case is handling the timer interrupt and springing forward one hour or falling back one hour. The timer interrupt code turned out to be much simpler than I first thought. It is a good thing to be simple in an interrupt. Here is part of the code:

1.
span class=”coMULTI”>/* begin DST – Second Sunday in March */ /\* ANSI is months since January \*//\*March\*/) &amp;&amp; (data.tm\_wday == 1 /\*Sunday\*/) &amp;&amp; (data.tm\_mday >= 8) &amp;&amp; /\* second Sunday \*/ (data.tm\_mday <= 14) &amp;&amp; (data.tm\_hour == 2) &amp;&amp; /\* 2:00 am \*/ (data.tm\_min == 0) &amp;&amp; (data.tm\_sec == 0)) { /\* do something here \*/ }}

The second case was to determine if the date and time was during daylight savings time or not. This is used when the date and time get set on the controller, and to display the daylight savings time status. The code is more complex than the interrupt routine. Here is part of the code:

1.
span class=”coMULTI”>/* simple bounds check *//\* get the start date for DST – Second Sunday in March \*/
Testing the Daylight Savings Time algorithm was fairly routine using Unit Testing:
1.
span class=”coMULTI”>/* original DST *//\* new for 2007 DST \*/
Hope you are getting used to waking up earlier!