clock timezone

This commit is contained in:
Yuri Trikoz
2020-06-27 02:37:30 +03:00
parent 00fd9f9bb9
commit 39a3cb04ed
3 changed files with 30 additions and 30 deletions

View File

@@ -9,8 +9,18 @@
class Clock { class Clock {
const char* MODULE = "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: public:
Clock() : _timezone{0}, _hasSynced{false}, _configured{false} {} Clock() : _uptime{0}, _timezone{0}, _ntp{""}, _hasSynced{false}, _configured{false} {};
void loop() { void loop() {
unsigned long passed = millis_since(_uptime); unsigned long passed = millis_since(_uptime);
@@ -21,16 +31,18 @@ class Clock {
// world time // world time
time_t now = getSystemTime(); 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); double drift = difftime(now, estimated);
if (drift > 1) { if (drift > 1) {
// Обработать ситуации c дрифтом времени на значительные величины // Обработать ситуации c дрифтом времени на значительные величины
} }
// TODO сохранять время на флеше // TODO сохранять время на флеше
_epoch = now; _unixtime = now;
breakEpochToTime(_epoch, _time); breakEpochToTime(_unixtime, _time_utc);
breakEpochToTime(_unixtime + getOffsetInSeconds(_timezone), _time_local);
} }
bool hasSync() { bool hasSync() {
@@ -80,7 +92,7 @@ class Clock {
} }
bool hasTimeSynced() const { bool hasTimeSynced() const {
return _epoch > MIN_DATETIME; return _unixtime > MIN_DATETIME;
} }
time_t getSystemTime() const { time_t getSystemTime() const {
@@ -94,7 +106,7 @@ class Clock {
} }
const String getTimeUnix() { const String getTimeUnix() {
return String(_epoch); return String(_unixtime);
} }
/* /*
@@ -102,7 +114,7 @@ class Clock {
*/ */
const String getDateDigitalFormated() { const String getDateDigitalFormated() {
char buf[16]; 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); return String(buf);
} }
@@ -111,7 +123,7 @@ class Clock {
*/ */
const String getTime() { const String getTime() {
char buf[16]; 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); return String(buf);
} }
@@ -120,23 +132,14 @@ class Clock {
*/ */
const String getTimeWOsec() { const String getTimeWOsec() {
char buf[16]; char buf[16];
sprintf(buf, "%02d:%02d", _time.hour, _time.minute); sprintf(buf, "%02d:%02d", _time_local.hour, _time_local.minute);
return String(buf); return String(buf);
} }
/* /*
* Время с момента запуска "чч:мм" далее "дд чч:мм" * Время с момента запуска "чч:мм:cc" далее "дд чч:мм"
*/ */
const String getUptime() { const String getUptime() {
return prettyMillis(_uptime); return prettyMillis(_uptime);
} }
private:
Time_t _time;
unsigned long _uptime;
unsigned long _epoch;
int _timezone;
String _ntp;
bool _hasSynced;
bool _configured;
}; };

View File

@@ -90,11 +90,11 @@ void telemetry_init() {
} }
void handle_uptime() { void handle_uptime() {
jsonWriteStr(configSetupJson, "getUptime", timeNow->getUptime()); jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime());
} }
void handle_statistics() { void handle_statistics() {
if (WiFi.status() == WL_CONNECTED) { if (isNetworkActive()) {
String urls = "http://backup.privet.lv/visitors/?"; String urls = "http://backup.privet.lv/visitors/?";
//----------------------------------------------------------------- //-----------------------------------------------------------------
urls += WiFi.macAddress().c_str(); urls += WiFi.macAddress().c_str();
@@ -114,7 +114,6 @@ void handle_statistics() {
urls += "Power on"; urls += "Power on";
#endif #endif
urls += "&"; urls += "&";
urls += "ver: ";
urls += String(FIRMWARE_VERSION); urls += String(FIRMWARE_VERSION);
String stat = getURL(urls); String stat = getURL(urls);
} }

View File

@@ -164,13 +164,8 @@ int getOffsetInMinutes(int timezone) {
void breakEpochToTime(unsigned long epoch, Time_t& tm) { void breakEpochToTime(unsigned long epoch, Time_t& tm) {
// break the given time_input into time components // break the given time_input into time components
// this is a more compact version of the C library localtime function // 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; tm.second = time % 60;
time /= 60; // now it is minutes time /= 60; // now it is minutes
tm.minute = time % 60; tm.minute = time % 60;
@@ -180,8 +175,9 @@ void breakEpochToTime(unsigned long epoch, Time_t& tm) {
tm.days = time; tm.days = time;
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1 tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
year = 0; uint8_t year = 0;
days = 0; unsigned long days = 0;
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++; 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 time -= days; // now it is days in this year, starting at 0
tm.day_of_year = time; tm.day_of_year = time;
uint8_t month;
uint8_t month_length;
for (month = 0; month < 12; month++) { for (month = 0; month < 12; month++) {
if (1 == month) { // february if (1 == month) { // february
if (LEAP_YEAR(year)) { if (LEAP_YEAR(year)) {