Alphanumeric GPS Wall Clock
Woes of Daylight Savings Time (DST)
The hardware was pretty straightforward; the trick was stitching the code together. I originally built this project back in February of 2011 and I hard-coded the DST dates:
language:c
if(year == 2011) {
if(month == 3 && day > 12) hour++; //DST begins March 13th
else if(month > 3 && month < 11) hour++;
else if(month == 11 && day < 6) hour++; //DST ends November 6th
}
If the year was 2011, and the date was between March 12th and November 6th then add an hour for daylight savings time. What about 2012? Well, I’ll hard code those dates too (March 10, November 3). What about 2014? Forget 2014 - something will break before 2014 and I’ll just fix it then…
So 2014 rolls around, and there’s nothing more annoying than a clock on the wall that tells you the wrong time. This was my first lesson:
Perhaps it’s just me and my projects, but I always assume they’re going to break within a few days. When really, silicon and the code we load onto it will run for many tens of years in the right conditions; possibly longer than we will be alive.
Shoot. Where did I put that code? 2011 was before we started using github at SparkFun so I got to go digging through old hard drives. Yuck. Next lesson:
I finally found the original code. I could have simply updated the hard coded values for the next 5-10 years, but what’s the fun in that?
Daylight Savings Rules
The history of daylight savings sucked up a few hours of my time, but setting that aside, the US government changed the rules (thanks guys!) back in 2007. We now go from -7 UTC (here in Boulder, CO) to -6 UTC between the 2nd Sunday in March and returns to -7 UTC on the 1st Sunday in November. From the date, we know when we’re in March or April, but how do we tell if we’re after the 2nd Sunday? We need to know the day of the week!
A Tuesday Next Year
Marty in Back To the Future went into the future to October 21, 2015. Holy smokes there’s already a countdown website. Neat! But how do we determine what day of the week October 21st will be? Luckily for us this is a wonderful problem for academics and math students to figure out. There is a reasonably straight forward formula to help us out:
language:c
//Pulled from Wikipedia: http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
//Posted by Tomohiko Sakamoto in 1993, it is accurate for any Gregorian date:
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
day_of_week = (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7; //0 = Sunday
Given a day/month/year and these three lines of code, we can establish that October 21st, 2015 will be a Wednesday. Now that we know the day of the week, we can determine if we are in or out of DST.