From 39a3cb04edfcc179a88b9bf71651a95204c066a7 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sat, 27 Jun 2020 02:37:30 +0300 Subject: [PATCH] clock timezone --- include/Clock.h | 41 ++++++++++++++++++++++------------------- src/Init.cpp | 5 ++--- src/Utils/TimeUtils.cpp | 14 ++++++-------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/include/Clock.h b/include/Clock.h index bab9267f..a18c51e8 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -9,8 +9,18 @@ class Clock { const char* MODULE = "Clock"; + private: + Time_t _time_local; + Time_t _time_utc; + unsigned long _uptime; + unsigned long _unixtime; + int _timezone; + String _ntp; + bool _hasSynced; + bool _configured; + public: - Clock() : _timezone{0}, _hasSynced{false}, _configured{false} {} + Clock() : _uptime{0}, _timezone{0}, _ntp{""}, _hasSynced{false}, _configured{false} {}; void loop() { unsigned long passed = millis_since(_uptime); @@ -21,16 +31,18 @@ class Clock { // world time time_t now = getSystemTime(); - time_t estimated = _epoch + (passed / ONE_SECOND_ms); + time_t estimated = _unixtime + (passed / ONE_SECOND_ms); double drift = difftime(now, estimated); if (drift > 1) { // Обработать ситуации c дрифтом времени на значительные величины } // TODO сохранять время на флеше - _epoch = now; + _unixtime = now; - breakEpochToTime(_epoch, _time); + breakEpochToTime(_unixtime, _time_utc); + + breakEpochToTime(_unixtime + getOffsetInSeconds(_timezone), _time_local); } bool hasSync() { @@ -80,7 +92,7 @@ class Clock { } bool hasTimeSynced() const { - return _epoch > MIN_DATETIME; + return _unixtime > MIN_DATETIME; } time_t getSystemTime() const { @@ -94,7 +106,7 @@ class Clock { } const String getTimeUnix() { - return String(_epoch); + return String(_unixtime); } /* @@ -102,7 +114,7 @@ class Clock { */ const String getDateDigitalFormated() { char buf[16]; - sprintf(buf, "%02d.%02d.%02d", _time.day_of_month, _time.month, _time.year); + sprintf(buf, "%02d.%02d.%02d", _time_local.day_of_month, _time_local.month, _time_local.year); return String(buf); } @@ -111,7 +123,7 @@ class Clock { */ const String getTime() { char buf[16]; - sprintf(buf, "%02d:%02d:%02d", _time.hour, _time.minute, _time.second); + sprintf(buf, "%02d:%02d:%02d", _time_local.hour, _time_local.minute, _time_local.second); return String(buf); } @@ -120,23 +132,14 @@ class Clock { */ const String getTimeWOsec() { char buf[16]; - sprintf(buf, "%02d:%02d", _time.hour, _time.minute); + sprintf(buf, "%02d:%02d", _time_local.hour, _time_local.minute); return String(buf); } /* - * Время с момента запуска "чч:мм" далее "дд чч:мм" + * Время с момента запуска "чч:мм:cc" далее "дд чч:мм" */ const String getUptime() { return prettyMillis(_uptime); } - - private: - Time_t _time; - unsigned long _uptime; - unsigned long _epoch; - int _timezone; - String _ntp; - bool _hasSynced; - bool _configured; }; \ No newline at end of file diff --git a/src/Init.cpp b/src/Init.cpp index ee9846cd..82ae3d60 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -90,11 +90,11 @@ void telemetry_init() { } void handle_uptime() { - jsonWriteStr(configSetupJson, "getUptime", timeNow->getUptime()); + jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime()); } void handle_statistics() { - if (WiFi.status() == WL_CONNECTED) { + if (isNetworkActive()) { String urls = "http://backup.privet.lv/visitors/?"; //----------------------------------------------------------------- urls += WiFi.macAddress().c_str(); @@ -114,7 +114,6 @@ void handle_statistics() { urls += "Power on"; #endif urls += "&"; - urls += "ver: "; urls += String(FIRMWARE_VERSION); String stat = getURL(urls); } diff --git a/src/Utils/TimeUtils.cpp b/src/Utils/TimeUtils.cpp index ab96bd65..04e53d1f 100644 --- a/src/Utils/TimeUtils.cpp +++ b/src/Utils/TimeUtils.cpp @@ -164,13 +164,8 @@ int getOffsetInMinutes(int timezone) { void breakEpochToTime(unsigned long epoch, Time_t& tm) { // break the given time_input into time components // this is a more compact version of the C library localtime function - uint8_t year; - uint8_t month; - uint8_t month_length; - uint32_t time; - unsigned long days; - time = epoch; + unsigned long time = epoch; tm.second = time % 60; time /= 60; // now it is minutes tm.minute = time % 60; @@ -180,8 +175,9 @@ void breakEpochToTime(unsigned long epoch, Time_t& tm) { tm.days = time; tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1 - year = 0; - days = 0; + uint8_t year = 0; + unsigned long days = 0; + while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { year++; } @@ -191,6 +187,8 @@ void breakEpochToTime(unsigned long epoch, Time_t& tm) { time -= days; // now it is days in this year, starting at 0 tm.day_of_year = time; + uint8_t month; + uint8_t month_length; for (month = 0; month < 12; month++) { if (1 == month) { // february if (LEAP_YEAR(year)) {