#include "clock.h" #include #include #include #define CLOCK_GMT_OFFSET_SECONDS 3600 #define CLOCK_DST_OFFSET_SECONDS 3600 #define CLOCK_NTP_SERVER2_URL "de.pool.ntp.org" #define CLOCK_EPOCH_SECONDS_MIN 1735686000 time_t clockOffset = 0; time_t startupTime = 0; auto ntpSet = false; void clockLoop() { if (isClockSet()) { return; } if (!ntpSet && isWiFiConnected()) { configTime(CLOCK_GMT_OFFSET_SECONDS, CLOCK_DST_OFFSET_SECONDS, WiFi.gatewayIP().toString().c_str(), CLOCK_NTP_SERVER2_URL); ntpSet = true; } const auto now = time(nullptr); if (isCorrectTime(now)) { clockOffset = now; } else { startupTime = now - clockOffset; const auto startStr = String(ctime(&startupTime)); info("clock set after %ld seconds! So startup was at %s", clockOffset, startStr.c_str()); } } bool isClockSet() { return startupTime != 0; } String getClockStr() { const auto t = time(nullptr); return {ctime(&t)}; } bool isCorrectTime(const time_t now) { return now < CLOCK_EPOCH_SECONDS_MIN; } time_t clockCorrect(const time_t t) { if (!isClockSet() || isCorrectTime(t)) { return t; } return t + clockOffset; } void clockCorrect(time_t *t) { if (!isClockSet() || isCorrectTime(*t)) { return; } *t += clockOffset; }