mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2025-12-13 11:02:12 +01:00
Github firmware updates #171
This commit is contained in:
parent
b91c54d377
commit
a9325eeca5
23 changed files with 922 additions and 78 deletions
84
Utils.cpp
84
Utils.cpp
|
|
@ -6,11 +6,82 @@
|
|||
/*********************************************************************
|
||||
* Timestamp class members
|
||||
********************************************************************/
|
||||
time_t Timestamp::now() {
|
||||
struct tm tmNow;
|
||||
getLocalTime(&tmNow);
|
||||
return mktime(&tmNow);
|
||||
}
|
||||
time_t Timestamp::getUTC() {
|
||||
time_t t;
|
||||
time(&t);
|
||||
return t;
|
||||
}
|
||||
time_t Timestamp::mkUTCTime(struct tm *dt) {
|
||||
time_t tsBadLocal = mktime(dt);
|
||||
|
||||
struct tm tmUTC;
|
||||
struct tm tmLocal;
|
||||
gmtime_r(&tsBadLocal, &tmUTC);
|
||||
localtime_r(&tsBadLocal, &tmLocal);
|
||||
time_t tsBadUTC = mktime(&tmUTC);
|
||||
time_t tsLocal = mktime(&tmLocal);
|
||||
time_t tsLocalOffset = tsLocal - tsBadUTC;
|
||||
return tsBadLocal + tsLocalOffset;
|
||||
}
|
||||
time_t Timestamp::parseUTCTime(const char *buff) {
|
||||
struct tm dt = {0};
|
||||
char num[5];
|
||||
uint8_t i = 0;
|
||||
memset(num, 0x00, sizeof(num));
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_year = atoi(num)-1900;
|
||||
memset(num, 0x00, sizeof(num));
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_mon = atoi(num)-1;
|
||||
memset(num, 0x00, sizeof(num));
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-' || ch == 'T' || ch == 't') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_mday = atoi(num);
|
||||
memset(num, 0x00, sizeof(num));
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-' || ch == ':') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_hour = atoi(num);
|
||||
memset(num, 0x00, sizeof(num));
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-' || ch == ':') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_min = atoi(num);
|
||||
for(uint8_t j = 0; j < 5 && i < strlen(buff);) {
|
||||
char ch = buff[i++];
|
||||
if(ch == '-' || ch == ':' || ch == 'Z') break;
|
||||
if(!isdigit(ch)) continue;
|
||||
else num[j++] = ch;
|
||||
}
|
||||
dt.tm_sec = atoi(num);
|
||||
//Serial.printf("Y:%d M:%d D:%d H:%d M:%d S:%d\n", dt.tm_year, dt.tm_mon, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
|
||||
return Timestamp::mkUTCTime(&dt);
|
||||
}
|
||||
time_t Timestamp::getUTC(time_t t) {
|
||||
tm tmUTC;
|
||||
gmtime_r(&t, &tmUTC);
|
||||
|
|
@ -29,15 +100,18 @@ char * Timestamp::formatISO(struct tm *dt, int tz) {
|
|||
dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min, dt->tm_sec, ms, tzHrs < 0 ? "-" : "+", abs(tzHrs), abs(tzMin));
|
||||
return this->_timeBuffer;
|
||||
}
|
||||
int Timestamp::tzOffset() {
|
||||
time_t now;
|
||||
time(&now);
|
||||
int Timestamp::calcTZOffset(time_t *dt) {
|
||||
tm tmLocal, tmUTC;
|
||||
gmtime_r(&now, &tmUTC);
|
||||
localtime_r(&now, &tmLocal);
|
||||
gmtime_r(dt, &tmUTC);
|
||||
localtime_r(dt, &tmLocal);
|
||||
long diff = mktime(&tmLocal) - mktime(&tmUTC);
|
||||
if(tmLocal.tm_isdst) diff += 3600;
|
||||
int hrs = (int)((diff/3600) * 100);
|
||||
int mins = diff - (hrs * 36);
|
||||
return hrs + mins;
|
||||
}
|
||||
int Timestamp::tzOffset() {
|
||||
time_t now;
|
||||
time(&now);
|
||||
return Timestamp::calcTZOffset(&now);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue