This ESP code pulls the date from NTP and calculates if today is DST. The DST algorithm is based on this post.
//--- Libraries...
//--- WiFi + NTP...
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
//--- Date to epoch...
#include <TimeLib.h>
//--- WiFi and network...
const char* ssid = "{SSID}";
const char* password = "{password}";
//--- NTP...
WiFiUDP ntpUDP;
const int CETtoGMToffset = 3600; //--- Central European Time (CET) offset to GMT...
NTPClient timeClient(ntpUDP, "time.kriss.re.kr",CETtoGMToffset,60000);
//--- setup() ----------------------------------------------------------------------------
void setup(){
Serial.begin(74880); //--- Prevent garbled output, show ESP8266 boot output...
delay(500);
Serial.println();
//--- Start WiFi...
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("*** Connecting to SSID: " + String(ssid));
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.print(".");
delay(50);
}
digitalWrite(LED_BUILTIN,HIGH);
Serial.println("\r\n*** WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//--- WiFi connection established...
// Initialize a NTPClient to get time from NTP server...
timeClient.begin();
//--- Is today in DST?
Serial.println("DST: " + String(nowInDST()));
}
//--- loop() ----------------------------------------------------------------------------
void loop(){
}
boolean nowInDST()
{
boolean isInDST = false;
//--- Retrieve epoch from NTP server...
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
//--- Destil current year...
struct tm *ptm = gmtime ((time_t *)&epochTime);
int y = ptm->tm_year+1900;
//--- Calculate epoch values for start and end of DST for this year...
unsigned long firstDSTepoch = toEpoch(y,3, lastSundayOfMonth(y,3) ,2,0,0); //--- Start of CET DST in epoch...
unsigned long lastDSTepoch = toEpoch(y,10,lastSundayOfMonth(y,10),2,0,0); //--- End of CET DST in epoch...
//--- Check epoch if in DST...
if (epochTime >= firstDSTepoch && epochTime < lastDSTepoch)
{
isInDST = true;
}
return isInDST;
}
//--- Calculate the date of the last Sunday in a given month...
int lastSundayOfMonth(int y, int M)
{
//--- Take care of leap years...
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (y % 4 == 0 && y % 100 != 0) {daysInMonth[1] = 29;}
//--- Last day of month M to epoch...
unsigned long epoch = toEpoch(y,M,daysInMonth[M-1],0,0,0);
//--- Day of week, based on ISO8601...
int dayOfWeek = ((epoch / 86400) + 4) % 7;
//--- Calculate date of last Sunday...
int lastSundayOfMonth = daysInMonth[M-1] - dayOfWeek;
return lastSundayOfMonth;
}
//--- Converts a date to epoch (TimeLib.h)...
unsigned long toEpoch(int y, byte M , byte d, byte h, byte m, byte s)
{
tmElements_t tmSet;
tmSet.Year = y - 1970;
tmSet.Month = M;
tmSet.Day = d;
tmSet.Hour = h;
tmSet.Minute = m;
tmSet.Second = s;
return makeTime(tmSet);
}