Automatically Setting DST from Sparktime Library

I’ve successfully gotten the Sparktime library to run on my device. However, I would like to automatically set the DST (or GMT offset) for my location. What would be the easiest way to do this so I don’t have to manually adjust things twice a year? Also, is there an easy way to set this as 24-hour time? Here is my current code:

#include "lib1.h"
#include "SparkTime/SparkTime.h"

UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr;

void setup() {
  rtc.begin(&UDPClient, "north-america.pool.ntp.org");
  rtc.setTimeZone(-6); // current gmt offset
  Serial.begin(9600);
}

void loop() {
    currentTime = rtc.now();
    if (currentTime != lastTime) {
      byte sec = rtc.second(currentTime);
	timeStr = "";
	timeStr += rtc.hour12String(currentTime);
	timeStr += ":";
	timeStr += rtc.minuteString(currentTime);
	timeStr += ":";
	timeStr += rtc.secondString(currentTime);	
	timeStr += " ";	
	timeStr += rtc.AMPMString(currentTime);
	Serial.println(timeStr);
    }
    lastTime = currentTime;
}

you can do it programmatically (example for North America)

bool IsDST(int dayOfMonth, int month, int dayOfWeek)
{
  if (month < 3 || month > 11)
  {
    return false;
  }
  if (month > 3 && month < 11)
  {
    return true;
  }
  int previousSunday = dayOfMonth - (dayOfWeek - 1);
  if (month == 3)
  {
    return previousSunday >= 8;
  }
  return previousSunday <= 0;
}

and in loop:

void loop()
{
  bool daylightSavings = IsDST(Time.day(), Time.month(), Time.weekday());
  Time.zone(daylightSavings? -4 : -5);
  // your other stuff
}

or you can use a webhook:

2 Likes

Hi @Alligator

With my SparkTime NTP library you don’t need to have your own DST function, it is built into the library.

In setup you can call rtc.setUseDST(true); and you can add rtc.setUseEuroDSTRule(true); if you are in Europe instead of the US, but at GMT-6 it looks like you are in the US.

2 Likes

Where do I get this library?

Never mind, I found it: GitHub - bkobkobko/SparkTime: NTP Time Client Code for the Spark Core

2 Likes

Hi @Alligator

It looks like you already had it.

Yep, just got it working. Now, here’s a silly question. How does one test the DST line? I can’t go forward or backward in time to test this…

I’d rather not have to fix my code once I put this device in its enclosure. It’s possible, just hard.

And how do I pull 24 hour time instead of 12-hour AM or PM time?
Edit: Got it, it is the following line:

	timeStr += rtc.hourString(currentTime);

For testing, currentTime is in seconds so you can just figure out how many seconds you want to advance or retard the clock by to test and add or subtract that from currentTime. So for instance, US DST ends on 11/1 at 2:00 AM–that is around three weeks plus one day and three hours from the time I am writing this, so the testing offset would be:

unsigned long offset = 3*7*24*60*60UL + 24*60*60UL + 3*60*60UL;
currentTime = currentTime + offset;

Pretty neat library! Any plans to include dst for other locations like Australia?

Hi @Guto

I have not worked on this library in some time and many of its features were superseded by things that Particle added, so I would say it is not likely to be updated.

But you do have me curious: does Australia have different rules for DST?