Make Spark DST-aware to set external analog clock?

I’m working on my first Spark project, which involves using the Spark to “control” an analog slave clock (the type you probably had in your grade school). This clock requires the correction coil to be activated for several seconds near the end of each hour, and for another period of time toward the end of the 5 PM and AM hour.

I’ve got all that pretty much figured out, but the problem I’m having is how to set the time on the Spark so that it sets my clock properly. I’m familiar with the time functions given in the documentation, but I’m not sure what the best method would be to make the Spark DST-aware. I’m thinking that I might have to write a if-then statement into my code to change the Time.zone() variable based on date, but that gets complicated since I’d have to use other variables to determine, for example, the second Sunday of March (i.e. increment the variable if it’s March until it reaches 2), then change the value passed into Time.zone() accordingly. This is definitely do-able, but it seems like a poor allocation of resources, and resources are important on a small piece of hardware like this.

Does anyone have more savvy recommendations on how to deal with changing the time zone to match DST (I should probably mention, this is US DST)?

Thanks!

Hi @dbsoundman

I have some DST handling code ported from my earlier SparkTime NTP-based library that I am planning on releasing as library for the built-in Time.now() real-time clock.

I know that the Spark team has some ideas in this area too, but it could be that this would be a temporary solution until there is an official DST-aware built-in real-time clock.

If you want to prototype, my SparkTime library which already handles US and Euro DST is available in the libraries tab of the webIDE. The calls are very similar between the built-in Time library and my NTP library, so it won’t be hard to change later.

@bko, I’m not quite sure how your library wants me to request the current hour, minute, and second, but it doesn’t seem to like the call format I was using before.

This is what I have now, that causes errors:

int secaccum;
if(secaccum<rtc.second()) secaccum++;

Before, I was able to call something like Time.second(), and presumably get the current second, but it looks like your library requires that I create a variable to pass into it (e.g. rtc.second(now), where “now” is an int). Am I correct here? If so, why did you choose to set this up differently?

Yes and I set this up this way on purpose. The problem with having hours minutes and seconds that do not take arguments is that they can get out sync. If the current time 12:59:59 and just changes to 1:00:00 and you call the three functions in different orders you can get:

12:59:59 (correct)
12:59:00
12:00:00
1:59:59
etc etc.

In this particular case since you are only calling seconds, you won’t see this, but it is problem in other cases.

You should call

int secaccum;
if(secaccum<rtc.second(rtc.now()) secaccum++;
2 Likes

Thanks for the clarification, I’m still brushing up on my coding skills so I didn’t think to call a function within a function like that.

One other question: I’ve set rtc.SetTimeZone to -7 to match my time zone, but what happens when DST ends? Is the fact that I’m pooling from north-america.pool.ntp.org that defines whether or not I want US DST?