Add logic for US gateway DST->STD and STD-DST changeover

pull/142/head
Jeff Lehman 1 year ago
parent eea74f48ec
commit 56af9918c9

@ -70,9 +70,10 @@
//#define MQTT_PASS "Your MQTT Password"
// NTP Time settings
//#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
//#define LOCAL_OFFSET (-6) // Local time offset in hours from UTC - if unsure, check https://time.is
//#define TIME_FETCHNTP 15 // Time in minutes between fetching time from NTP server
//#define TIME_PRINTTIME 10 // Time in minutes between printing local time
//#define TIME_SEND_INTERVAL_MS (1000 * 60 * 60) // Frequency that time is sent to repeaters and nodes
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
#define TIME_FETCHNTP 15 // Time, in minutes, between fetching time from NTP server
#define TIME_PRINTTIME 1 // Time, in minutes, between printing local time to debug
#define TIME_SEND_INTERVAL 1 // Time, in minutes, between sending out time to remote devices

@ -70,7 +70,9 @@
//#define MQTT_PASS "Your MQTT Password"
// NTP Time settings
//#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
//#define LOCAL_OFFSET (-6) // Local time offset in hours from UTC - if unsure, check https://time.is
//#define TIME_FETCHNTP 15 // Time in minutes between fetching time from NTP server
//#define TIME_PRINTTIME 10 // Time in minutes between printing local time
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
#define TIME_FETCHNTP 15 // Time, in minutes, between fetching time from NTP server
#define TIME_PRINTTIME 1 // Time, in minutes, between printing local time to debug
#define TIME_SEND_INTERVAL 1 // Time, in minutes, between sending out time to remote devices

@ -70,7 +70,9 @@
//#define MQTT_PASS "Your MQTT Password"
// NTP Time settings
//#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
//#define LOCAL_OFFSET (-6) // Local time offset in hours from UTC - if unsure, check https://time.is
//#define TIME_FETCHNTP 15 // Time in minutes between fetching time from NTP server
//#define TIME_PRINTTIME 10 // Time in minutes between printing local time
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
#define TIME_FETCHNTP 15 // Time, in minutes, between fetching time from NTP server
#define TIME_PRINTTIME 1 // Time, in minutes, between printing local time to debug
#define TIME_SEND_INTERVAL 1 // Time, in minutes, between sending out time to remote devices

@ -70,7 +70,9 @@
//#define MQTT_PASS "Your MQTT Password"
// NTP Time settings
//#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
//#define LOCAL_OFFSET (-6) // Local time offset in hours from UTC - if unsure, check https://time.is
//#define TIME_FETCHNTP 15 // Time in minutes between fetching time from NTP server
//#define TIME_PRINTTIME 10 // Time in minutes between printing local time
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
#define TIME_FETCHNTP 15 // Time, in minutes, between fetching time from NTP server
#define TIME_PRINTTIME 1 // Time, in minutes, between printing local time to debug
#define TIME_SEND_INTERVAL 1 // Time, in minutes, between sending out time to remote devices

@ -71,7 +71,9 @@
// NTP Time settings
//#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
//#define LOCAL_OFFSET (-6) // Local time offset in hours from UTC - if unsure, check https://time.is
//#define TIME_FETCHNTP 15 // Time in minutes between fetching time from NTP server
//#define TIME_PRINTTIME 10 // Time in minutes between printing local time
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
#define TIME_FETCHNTP 15 // Time, in minutes, between fetching time from NTP server
#define TIME_PRINTTIME 1 // Time, in minutes, between printing local time to debug
#define TIME_SEND_INTERVAL 1 // Time, in minutes, between sending out time to remote devices

@ -6,11 +6,25 @@
#else
#define FDRS_TIME_PRINTTIME GLOBAL_TIME_PRINTTIME
#endif // TIME_PRINTTIME
#define DSTSTART (timeinfo.tm_mon == 2 && timeinfo.tm_wday == 0 && timeinfo.tm_mday > 7 && timeinfo.tm_mday < 15 && timeinfo.tm_hour == 2)
#define DSTEND (timeinfo.tm_mon == 10 && timeinfo.tm_wday == 0 && timeinfo.tm_mday < 8 && timeinfo.tm_hour == 2)
// select Local Standard time Offset from UTC configuration
#if defined(STD_OFFSET)
#define FDRS_STD_OFFSET STD_OFFSET
#else
#define FDRS_STD_OFFSET GLOBAL_STD_OFFSET
#endif // STD_OFFSET
// select Local savings time Offset from UTC configuration
#if defined(DST_OFFSET)
#define FDRS_DST_OFFSET DST_OFFSET
#else
#define FDRS_DST_OFFSET GLOBAL_DST_OFFSET
#endif // DST_OFFSET
#define USDSTSTART (timeinfo.tm_mon == 2; timeinfo.tm_wday == 0 && timeinfo.tm_mday > 7 && timeinfo.tm_mday < 15 && timeinfo.tm_hour == 2)
#define USDSTEND (timeinfo.tm_mon == 10 && timeinfo.tm_wday == 0 && timeinfo.tm_mday < 8 && timeinfo.tm_hour == 2)
time_t now; // Current time - number of seconds since Jan 1 1970 (epoch)
time_t now; // Current time in UTC- number of seconds since Jan 1 1970 (epoch)
struct tm timeinfo; // Structure containing time elements
struct timeval tv;
char strftime_buf[64];
@ -20,12 +34,15 @@ bool isDST; // Keeps track of Daylight Savings Time vs
long slewSecs = 0; // When time is set this is the number of seconds the time changes
time_t lastUpdate = 0;
time_t lastTimeSend = 0;
double stdOffset = (FDRS_STD_OFFSET * 60 * 60); // UTC -> Local time, in Seconds, offset from UTC in Standard Time
double dstOffset = (FDRS_DST_OFFSET * 60 * 60); // DST offset from standard time (in seconds)
time_t lastDstCheck = 0;
void sendTimeLoRa();
esp_err_t sendTimeESPNow();
bool validTime() {
if(now < 1677000000 || (millis() - lastNTPFetchSuccess > (24*60*60*1000))) {
if(now < 1672000000 || (millis() - lastNTPFetchSuccess > (24*60*60*1000))) {
if(validTimeFlag) {
DBG("Time no longer reliable.");
validTimeFlag = false;
@ -41,39 +58,87 @@ bool validTime() {
}
void printTime() {
if(!validTime()) {
return;
}
if(validTime()) {
// UTC Time
// now -= localOffset;
// // print Unix time:
// //DBG("Unix time = " + String(now));
// localtime_r(&now, &timeinfo);
// strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
// DBG("The current UTC date/time is: " + String(strftime_buf));
// now += localOffset;
// Local time
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
DBG("The current local date/time is: " + String(strftime_buf));
// UTC Time
// // print Unix time:
// //DBG("Unix time = " + String(now));
// localtime_r(&now, &timeinfo);
// strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
// DBG("The current UTC date/time is: " + String(strftime_buf));
// Local time
time_t local = time(NULL) + (isDST?dstOffset:stdOffset);
localtime_r(&local, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
DBG("The current local date/time is: " + String(strftime_buf) + (isDST?" DST":" STD"));
}
}
void checkDST() {
// DST -> STD - subtract one hour (3600 seconds)
if(validTimeFlag && isDST && (DSTEND || timeinfo.tm_isdst == 1)) {
isDST = false;
now -= 3600;
localtime_r(&now, &timeinfo); // write to timeinfo struct
DBG("Time change from DST -> STD");
}
// STD -> DST - add one hour (3600 seconds)
else if(validTimeFlag && !isDST && (DSTSTART || timeinfo.tm_isdst == 0)) {
isDST = true;
now += 3600;
localtime_r(&now, &timeinfo); // write to timeinfo struct
DBG("Time change from STD -> DST");
if(validTime() && (time(NULL) - lastDstCheck > 5)) {
lastDstCheck = time(NULL);
int dstFlag = -1;
localtime_r(&now, &timeinfo);
if(timeinfo.tm_mon == 2) {
struct tm dstBegin;
dstBegin.tm_year = timeinfo.tm_year;
dstBegin.tm_mon = 2;
dstBegin.tm_mday = 8;
dstBegin.tm_hour = 2;
dstBegin.tm_min = 0;
dstBegin.tm_sec = 0;
mktime(&dstBegin); // calculate tm_dow
dstBegin.tm_mday = dstBegin.tm_mday + ((7 - dstBegin.tm_wday) % 7);
// mktime(&dstBegin); // recalculate tm_dow
// strftime(buf, sizeof(buf), "%c", &dstBegin);
// DBG("DST Begins: " + String(buf) + " local");
time_t tdstBegin = mktime(&dstBegin) - stdOffset;
if(tdstBegin != -1 && (time(NULL) - tdstBegin >= 0) && isDST == false) { // STD -> DST
dstFlag = 1;
}
else if(tdstBegin != -1 && (time(NULL) - tdstBegin < 0) && isDST == true) { // DST -> STD
dstFlag = 0;
}
}
else if(timeinfo.tm_mon == 10) {
struct tm dstEnd;
dstEnd.tm_year = timeinfo.tm_year;
dstEnd.tm_mon = 10;
dstEnd.tm_mday = 1;
dstEnd.tm_hour = 2;
dstEnd.tm_min = 0;
dstEnd.tm_sec = 0;
mktime(&dstEnd); // calculate tm_dow
dstEnd.tm_mday = dstEnd.tm_mday + ((7 - dstEnd.tm_wday) % 7);
// mktime(&dstEnd); // recalculate tm_dow
// strftime(buf, sizeof(buf), "%c", &dstEnd);
// DBG("DST Ends: " + String(buf) + " local");
time_t tdstEnd = mktime(&dstEnd) - dstOffset;
if(tdstEnd != -1 && (time(NULL) - tdstEnd >= 0) && isDST == true) { // DST -> STD
dstFlag = 0;
}
else if(tdstEnd != -1 && (time(NULL) - tdstEnd < 0) && isDST == false) { // STD -> DST
dstFlag = 1;
}
}
else if((timeinfo.tm_mon == 11 || timeinfo.tm_mon == 0 || timeinfo.tm_mon == 1) && isDST == true) {
dstFlag = 0;
}
else if(timeinfo.tm_mon >= 3 && timeinfo.tm_mon <= 9 && isDST == false) {
dstFlag = 1;
}
if(dstFlag == 1) {
isDST = true;
DBG("Time change from STD -> DST");
}
else if(dstFlag == 0) {
isDST = false;
// Since we are potentially moving back an hour we need to prevent flip flopping back and forth
// 2AM -> 1AM, wait 70 minutes -> 2:10AM then start DST checks again.
lastDstCheck += (70 * 60); // skip checks for another 70 minutes
DBG("Time change from DST -> STD");
}
}
return;
}
@ -99,7 +164,6 @@ bool setTime(time_t previousTime) {
// Do not call sendFDRS here. It will not work for some reason.
if(validTime()) {
lastNTPFetchSuccess = millis();
printTime();
return true;
}
else {

@ -45,13 +45,6 @@
#define FDRS_TIME_SERVER GLOBAL_TIME_SERVER
#endif // TIME_SERVER
// select Local Offset from UTC configuration
#if defined(LOCAL_OFFSET)
#define FDRS_LOCAL_OFFSET LOCAL_OFFSET
#else
#define FDRS_LOCAL_OFFSET GLOBAL_LOCAL_OFFSET
#endif // LOCAL_OFFSET
// select Time, in minutes, between NTP time server queries configuration
#if defined(TIME_FETCHNTP)
#define FDRS_TIME_FETCHNTP TIME_FETCHNTP
@ -65,7 +58,6 @@ unsigned int localPort = 8888; // local port to listen for UDP packets
const char timeServer[] = FDRS_TIME_SERVER; // NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
time_t localOffset = (FDRS_LOCAL_OFFSET * 60 * 60); // UTC -> Local time in Seconds in Standard Time
uint NTPFetchFail = 0; // consecutive NTP fetch failures
extern time_t now;
@ -194,9 +186,7 @@ void fetchNtpTime() {
// subtract seventy years:
// now is epoch format - seconds since Jan 1 1970
now = secsSince1900 - seventyYears;
// Adjust for local time
now += localOffset;
setTime(now);
setTime(now); // time in UTC
}
else {
NTPFetchFail++;

Loading…
Cancel
Save