Cunning Chronograph: Outfoxed by Daylight Saving Time - Embedded.com

Cunning Chronograph: Outfoxed by Daylight Saving Time

I must admit to finding mself a tad disgruntled yesterday morning. As I was meandering across the bay outside my office on the way to the kitchen to grab a well-earned mug of coffee, I happened to glance at my Cunning Chronograph to bask in the glow of its display, only to discover that it was off by an hour.


(Source: Max Maxfield / Embedded.com)

As a simple rule of thumb, when anything happens like this it's usually my fault, so my knee-jerk reaction was to assume that something had gone wrong with my Arduino code. The first thing I did was to cycle the power to the Cunning Chronograph, but to no avail. Then I remembered that we'd entered Daylight Saving Time (DST) the day before, which meant that the clocks had moved one hour forward. Dagnabbit! I had forgotten all about this, which prompted some gnashing of teeth and rending of garb, let me tell you.

As always, of course, there are a variety of potential solutions. Perhaps the simplest would be to add a menu entry to the Simblee-based iPad app I use to control the Cunning Chronograph (see Controlling IoT devices with mobile platforms — Creating hierarchical menus and Cunning Chronograph: Migrating to custom boards).

This really would take only a few seconds, and all it means is that I'd have to change the time on the Cunning Chronograph by hand twice a year, just like I have to do on the stove and the microwave and my bedside alarm clock and my car dashboard (to name but a few), but…

…then we have my chum, Bob, to contend with. Bob is a master carpenter. He's the one who made the Cunning Chronograph's cabinet with its hand-carved Celtic knot for me. He's also making one for himself. My cabinet is stained black with silver gilt; Bob is planning to make his a dark cherry color with antiqued brass gilt. Bob made the cabinets — I'm furnishing the electronics.

Now, I'm sure that Bob would be in no way perturbed if I told him that he had to occasionally change the time on his Cunning Chronograph to accommodate DST. On the other hand, I would much prefer not to even mention this to him. Once we've finished his clock, when the time changes in the future, I'd like him to come into his workshop, glance at his Chronograph, and realize that it's changed the time automatically, at which point he'll think “Max, you clever little rascal!”

What we need is an auspicious algorithm. In the case of Alabama USA, which is where I currently hang my hat, the time advances by one hour on the second Sunday in March and it is set back by one hour on the first Sunday in November (“Spring Forward, Fall Back,” as the saying goes). More specifically, these one-hour shifts occur at 2:00 a.m. local time. In spring, the clock jumps forward from the last moment of 1:59 a.m. standard time to 3:00 a.m. DST and that day has 23 hours; in autumn, the clock jumps backward from the last moment of 1:59 a.m. DST to 1:00 a.m. standard time, repeating that hour, and that day has 25 hours.

The real-time-clock (RTC) module my Cunning Chronograph uses to keep track of time is the ChronoDot from Adafruit. Based on the DS3231 temperature-compensated RTC, the ChronoDot is accurate to around a minute a year, which I think is pretty darned good.

Using the RTClib library recommended by Adafruit, we can easily access the following integer values:

    year   (2016, for example)    month  (1 to 12)    day    (1 to 31)    hour   (0 to 23)    minute (0 to 59)    second (0 to 59)

This gives us a pretty good start, but how do we now work out if we're between 2:00 a.m. on the second Sunday of March and 2:00 a.m. on the first Sunday of November?

I had a quick Google (while no one was looking) and found this Determination of the day of the week page on the Wikipedia. Assuming y , m , and d represent the current year, month, and day, respectively, then the following expression will return an integer value for dow (day of the week), where 0 = Sunday, 1 = Monday, 2 = Tuesday, etc. (note that the values of y and d are not preserved):

    dow = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;

Well, this looks promising -- it even accounts for leap years. From our RTC, we know the current year, month, day, hour, minute, and second. Using the above formula, we can determine what day of the week it is. Now all we need is a way to munge all of this information together to determine if we're between 2:00 a.m. on the second Sunday of March and 2:00 a.m. on the first Sunday of November, in which case we add an hour to whatever value we're getting from the RTC.

The answer is obvious; all we have to do is... but no, why should I have all the fun? Let's see who can come up with the most capriciously cunning function called isDST () that accepts integer arguments of (year, month, day, hour, minute, second) and returns a Boolean value of true if we are indeed currently in Daylight Saving Time.

"But how shall we determine which offering is the most capriciously cunning?" I hear you cry. Well, all I can say is that we'll know it when we see it.

48 thoughts on “Cunning Chronograph: Outfoxed by Daylight Saving Time

  1. “Max – what RTC IC are you using? The DS1307, a very commonly used one, has a register (03h) which gives the day of the week, which you could use? Datasheet herenhttp://datasheets.maximintegrated.com/en/ds/DS1307.pdfnA lot of RTCs do this, if yours

    Log in to Reply
  2. “I'm truly sorry if I have misunderstood, but I thought this website and this column were aimed at professional engineers and designers, not at school children and hobbyists. In the firmware for your watch, you FORGOT to consider DST? What if Google forgot

    Log in to Reply
  3. “ChronoDot uses the DS3231 controler and it has its own register for the day of the week (bits [0:2] at address 03h).n@Max I think you're going the wrong way and you should listen to David :)”

    Log in to Reply
  4. “You just had to go there, didn't you — you couldn't walk by and let a sleeping Max lay there snoozing — you had to poke him with a sharp WWVB stick.nnSo I did a Google search for WWVB (very interesting) in conjunction with Arduino and found reference

    Log in to Reply
  5. “Sez you! I never listen to David LOLnnThe first thing to do — just “because” — is to check the results from the code snippet above against the ChronoDot's day-of-the-week register, just to make sure that everything matches, but that's all I'm promis

    Log in to Reply
  6. “Hey — give me a break. This is a hobby project that's growing in the telling. If it had been a real project there would have been a spec, and the spec would have said “must account for DST” (along with qualifiers like “Only in Alabama” or “Anywhere

    Log in to Reply
  7. “I always say that the great thing about sites like Embedded.com is that you learn stuff. I knew about the atomic clock stuff as a “background” sort of thing, but I'd never heard of WWVB — the fact that it transmits at 1 baud and takes a full minute to

    Log in to Reply
  8. “Hey Max, before I jump into this I've got a couple questions.nFirst, are you also building a time machine, or can I assume that the year will always be a positive integer?nSecond, I assume you are building this to last. For a long time. A really long ti

    Log in to Reply
  9. “Thus far my attempts at a time machine have been thwarted — it's almost as though a future version of me were throwing a spanner in the works (so maybe this indicates that I will one day succeed).nnI'll be happy if it keeps on working until my 100th Bi

    Log in to Reply
  10. “I'm sorry. I found a site called “embedded.com”, and thought it was a resource for professional firmware designers. I am not “annoyed”, and I apologise if I gave that impression. “

    Log in to Reply
  11. “”I'm sorry. I found a site called “embedded.com”, and thought it was a resource for professional firmware designers. I am not “annoyed”, and I apologise if I gave that impression.”nnNo worries — but one question — you thought Embedded.com would

    Log in to Reply
  12. “Max – I was looking at this last night but looks like you solved the problem with the WWVB Time Receiver Module. My suggestion was to use the two alarm functions in the DS3231.nnYou could set ALARM2 to interrupt when the DATE = 1 (first day of the month

    Log in to Reply
  13. “The problem with using the alarm functions in the way you describe is that this only works if the clock is continually powered on — if there's a power outage, then it would have to check for the current state of play when it's powered back up. In this ba

    Log in to Reply
  14. “”Embedded is the resource for everything embedded systems designers and developers need to do their jobs, from the latest products, design and coding trends and tips, source code, to practical design information, discussions, features and industry happen

    Log in to Reply
  15. “I don't think the RTC works out the day of the week, you have to program it along with the other time settings, but once done it will advance itself. SO all you'd have to do is look for the second instance of day 7 in month x and change the time then. I

    Log in to Reply
  16. “Max – hint – don't put URLs in brackets – it stops them working and just reloads this page. I always put them on their own line too. Here's your above URLs:nRadio Time Modules:nnhttp://www.pvelectronics.co.uk/index.php?main_page=index&cPath=9 nnHi

    Log in to Reply
  17. “MaxnnYou may remember back in the days of APP, I mentioned my friend Leo, a 98 year old at the gym I go to. Well Leo just celebrated his 101st birthday. Still goes to the gym every day! Still drives and has a 79 year old girlfriend.”

    Log in to Reply
  18. “”Some of us are hardware designers who got lumbered with writing firmware, because newbies aren't the only ones who think that hardware design knowledge is all you need to write firmware”nnThat's not what I said — all I said was that you need our har

    Log in to Reply
  19. “”We need all the help we can get, and that includes good advice and good examples from a website that claims to be a “resource for everything embedded systems designers and developers need to do their jobs”. If that is unreasonable, then I am unreasona

    Log in to Reply
  20. “Hi All, I have always used Zellers Congruency Algorithm, From the date it will will work out the day. At the moment I have lost my code source for this so will have to work out some code to do it in C99.nBest Crusty”

    Log in to Reply
  21. “Don't stress, it works perfectly in Arizona. In any case the politicians can change when Daylight Saving Time occurs, and I would love to see the code that fixes that!”

    Log in to Reply
  22. “Unfortunately, DST is set by governments, which means that the “spring ahead”, “fall back” dates can change, therefore, coding a scheme will probably not last to your 100th birthday.nOne problem with WWVB is reception, in some areas it is only receiv

    Log in to Reply
  23. “”…coding a scheme will probably not last to your 100th birthday…”nnWell, aren't you a little ray of sunshine LOL :-)nnThe great thing about the WWVB is that it's so weird. As you say, it's strongest at night — I think I'd just add that as an ex

    Log in to Reply
  24. “”Sounds like fodder for a few articles comparing WWVB vs RBDS implementation and real-world usefulness.”nnIt does — it does — so when can I expect your first draft? LOL”

    Log in to Reply
  25. “I should have plenty of time when I retire. I will be sure to let you know. Too much firewood to cut and split right now, not to mention completing a kitchen cabinet project.”

    Log in to Reply
  26. “It's like the old saying goes: “Work is the curse of the drinking classes” — there's just too much fun stuff to do, but not enough time to do it all (sad face).”

    Log in to Reply
  27. “Remember to not “fall back” more than once on the same day.nnA very early Microsoft Operating System had that bug.nn”It is 2:00 AM now so fall back to 1:00 AM” until it is 2:00 AM again over and over …nnn”

    Log in to Reply
  28. “I'm fed up with DST. Currently there is a lot of debate about it in my country. Not only have they changed the dates a few times, complicating things between not only my windows machines, but NAS, IP cameras, routers, etc and a number of embedded devices,

    Log in to Reply
  29. “I just stumbled upon this very pertinent document: WWVB Radio Controlled Clocks: Recommended Practices for Manufacturers and Consumers (2009 Edition)nhttp://tf.nist.gov/general/pdf/2422.pdfnI quote from page 22:n”Manufacturers might elect to desi

    Log in to Reply
  30. “Great input — thanks for sharing — I now have the WWVB receiver mentioned in the column above sitting on my desk — it's just waiting in line for me to find the time to play with it. “

    Log in to Reply
  31. “Now that you'll be using WWVB, don't forget to account for LEAP SECONDS (minutes with 61 seconds) that WILL occur at RANDOM years. Not sure how your Cunning Chronograph will be showing it ;-)”

    Log in to Reply
  32. “I'll be happy to get far enough along for this to be a problem LOL At the moment I am snowed under with work — up to my armpits in alligators fighting fires without a paddle (i never metaphor I didn't like :-)”

    Log in to Reply
  33. “And so it happened this weekend on the day I celebrated my birthay. I forgot to disable all automatic clock update mechanisms when I had to change my cellphone's motherboard last year. DST will start in two weeks, but half the countries cell phones and co

    Log in to Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.