There are several solutions to this:
- move to Arizona, where there is no DST.
- move to New Your, where the burglary rate is the lowest.
- use an "atomic clock" that adjusts automatically.
- use code like this with time from a GPS or RTC:
define some variables
byte daysPerMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const int DSTBegin [] = { 310, 309, 308, 313, 312, 311, 310, 308, 314, 313, 312, 310, 309};
const int DSTEnd [] = {1103, 1102, 1101, 1106, 1105, 1104, 1103, 1101, 1107, 1106, 1105, 1103, 1102};
int theHour;
int theDay;
int theMonth;
int theYear;
int timeZone = -7;
boolean isDST = false;
Include this code snippet in your sketch to adjust theHour, theDay, theMonth and theYear for time zones, leap years and DST:
// The time comes from an attached GPS.
// GPS.hour, GPS.day, and GPS.month
...
// ---- Convert UTC to Local Time ------------------------------------
theHour = GPS.hour;
theDay = GPS.day;
theMonth = GPS.month;
theYear = GPS.year;
// -------------------- check for leap year --------------------------
if ((theYear % 4) == 0) {
daysPerMonth[1] = 29; // leap year
}
// --------------------change the hour according to time zone --------
theHour += timeZone;
// ------------------- apply daylight savings time [rolleyes] --------
if (isDST) {
if (theMonth * 100 + theDay >= DSTBegin[theYear - 13] && theMonth * 100 + theDay < DSTEnd[theYear - 13]) {
theHour += 1;
}
}
// ----------------------- correct day, mo & yr for roll backward ----
if (theHour < 0) {
theHour += 24;
theDay -= 1;
if (theDay < 1) {
if (theMonth == 1) { // Jan 1
theMonth = 12;
theYear -= 1;
} else {
theMonth -= 1;
}
theDay = daysPerMonth[theMonth - 1];
}
}
// ----------------------- roll forward if east of prime meridian ----
if (theHour >= 24) {
theHour -= 24;
theDay += 1;
if (theDay > daysPerMonth[theMonth - 1]) {
theDay = 1;
theMonth += 1;
if (theMonth > 12) { // Jan 1
theYear += 1;
theMonth = 1;
}
}
}
...
I have been using this code for 3 years now, and hit hasn't failed yet. It should be good through the year 2025.