Hallo,
Da das Thema bald ansteht hab ich mich mal damit etwas beschäftigt.
Zum Thema selbst habe ich hier im Forum folgenden Code gefunden.
boolean summertime_EU(int year, byte month, byte day, byte hour, byte tzHours)
// European Daylight Savings Time calculation by "jurs" for German Arduino Forum
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
// return value: returns true during Daylight Saving Time, false otherwise
{
if (month < 3 || month > 10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if (month > 3 && month < 10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if (month == 3 && (hour + 24 * day) >= (1 + tzHours + 24 * (31 - (5 * year / 4 + 4) % 7)) || month == 10 && (hour + 24 * day) < (1 + tzHours + 24 * (31 - (5 * year / 4 + 1) % 7)))
return true;
else
return false;
}
beim upload gibt es eine Warnung.
C:\Users\Heinz\Documents\Arduino\D1mini\displayNTP\displayNTP.ino:150:18: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (month == 3 && (hour + 24 * day) >= (1 + tzHours + 24 * (31 - (5 * year / 4 + 4) % 7)) || month == 10 && (hour + 24 * day) < (1 + tzHours + 24 * (31 - (5 * year / 4 + 1) % 7)))
^
ich denke mal man sollte noch für das oder je zwei Klammer machen. siehe code unten dann ist das weg.
aber bei der Analyse der Zeile ist mir was anderes aufgefallen.
hour und day sind als byte deklariet, die Berchnung hour+24*day kann grösser sein als 255 . Geht das dennoch weil intern auch in dem Fall mit integer gerechnet wird?
für die ESP Famile gibt es ja die neue Funktion configTime() die ja recht einfach zu händeln ist.
zur Einstellung der Sommer und Winterzeit gibts einen Parameter den man aber nicht wirklich für die automatische Umstellung nutzen kann denke ich. Wenn man nun die obige Funktion summertime_EU verwendet erhält man ja eine bool Variable zurück, abhängig davon kann man dann z.B. dst 0 oder 3600 setzen Alledings würde dann die Zeitumstellung tatsächlich erst beim nächsten Sync aktive werden, oder ??
configTime(3600, dst , “pool.ntp.org”, “time.nist.gov”);
ich hab jetzt abhängig von Sommerzeit bei hour eine Stunde zu addiert, gefällt mir aber eigendlich nicht so gut. Eigendlich ist es ja fast egal , ist ja eh mitten in der Nacht, aber dennoch hat noch einer eine gute Idee ? Man müste nach einem Sync einmalig die Funktion aufrufen und dann
time_t now = time(nullptr)+3600*sommerzeit
hier mein Versuchscode.
// Hardware ESP D1
#include <ESP8266WiFi.h>
#include <time.h>
const char* ssid = "xxxx";
const char* password = "xxxx";
int timezone = 1;
int dst = 3600;
bool sommerzeit = 0;
int year;
byte month, day, hour, minute, second;
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
configTime(timezone * 3600, dst * 0, "pool.ntp.org", "time.nist.gov");
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("");
}
void loop() {
readtime();
Serial.printf("%02u-%02u-%4u\n", day, month, year);
Serial.printf("%02u:%02u:%02u\n", hour, minute, second);
delay(1000);
}
// Datum und zeit auslesen
void readtime() {
time_t now = time(nullptr);
struct tm*timeinfo;
time(&now);
timeinfo = localtime(&now);
year = 1900 + timeinfo->tm_year;
month = 1 + timeinfo->tm_mon;
day = timeinfo->tm_mday;
hour = timeinfo->tm_hour + sommerzeit;
minute = timeinfo->tm_min;
second = timeinfo->tm_sec;
sommerzeit = summertime_EU(year, month, day, hour, 0);
}
boolean summertime_EU(int year, byte month, byte day, byte hour, byte tzHours)
// European Daylight Savings Time calculation by "jurs" for German Arduino Forum
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
// return value: returns true during Daylight Saving Time, false otherwise
{
if (month < 3 || month > 10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if (month > 3 && month < 10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if ((month == 3 && (hour + 24 * day) >= (1 + tzHours + 24 * (31 - (5 * year / 4 + 4) % 7))) || ( month == 10 && (hour + 24 * day) < (1 + tzHours + 24 * (31 - (5 * year / 4 + 1) % 7))))
return true;
else
return false;
}
Heinz