diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index a6f68f2efb..6a0e3e44a3 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -1379,7 +1379,7 @@ bool GPS::lookForLocation() t.tm_mon = reader.date.month() - 1; t.tm_year = reader.date.year() - 1900; t.tm_isdst = false; - p.timestamp = mktime(&t); + p.timestamp = gm_mktime(&t); // Nice to have, if available if (reader.satellites.isUpdated()) { diff --git a/src/gps/NMEAWPL.cpp b/src/gps/NMEAWPL.cpp index cdac3bb27e..71943b76c6 100644 --- a/src/gps/NMEAWPL.cpp +++ b/src/gps/NMEAWPL.cpp @@ -75,10 +75,10 @@ uint32_t printWPL(char *buf, size_t bufsz, const meshtastic_Position &pos, const uint32_t printGGA(char *buf, size_t bufsz, const meshtastic_Position &pos) { GeoCoord geoCoord(pos.latitude_i, pos.longitude_i, pos.altitude); - tm *t = localtime((time_t *)&pos.timestamp); + tm *t = gmtime((time_t *)&pos.timestamp); if (getRTCQuality() > 0) { // use the device clock if we got time from somewhere. If not, use the GPS timestamp. uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice); - t = localtime((time_t *)&rtc_sec); + t = gmtime((time_t *)&rtc_sec); } uint32_t len = snprintf( diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index 58b267a6cd..26af7cac21 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -40,7 +40,7 @@ void readFromRTC() t.tm_hour = rtc.getHour(); t.tm_min = rtc.getMinute(); t.tm_sec = rtc.getSecond(); - tv.tv_sec = mktime(&t); + tv.tv_sec = gm_mktime(&t); tv.tv_usec = 0; LOG_DEBUG("Read RTC time from RV3028 as %ld\n", tv.tv_sec); timeStartMsec = now; @@ -68,7 +68,7 @@ void readFromRTC() t.tm_hour = tc.hour; t.tm_min = tc.minute; t.tm_sec = tc.second; - tv.tv_sec = mktime(&t); + tv.tv_sec = gm_mktime(&t); tv.tv_usec = 0; LOG_DEBUG("Read RTC time from PCF8563 as %ld\n", tv.tv_sec); timeStartMsec = now; @@ -177,13 +177,7 @@ bool perhapsSetRTC(RTCQuality q, struct tm &t) */ // horrible hack to make mktime TZ agnostic - best practise according to // https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html - setenv("TZ", "GMT0", 1); - time_t res = mktime(&t); - if (*config.device.tzdef) { - setenv("TZ", config.device.tzdef, 1); - } else { - setenv("TZ", "UTC0", 1); - } + time_t res = gm_mktime(&t); struct timeval tv; tv.tv_sec = res; tv.tv_usec = 0; // time.centisecond() * (10 / 1000); @@ -236,3 +230,15 @@ uint32_t getValidTime(RTCQuality minQuality, bool local) { return (currentQuality >= minQuality) ? getTime(local) : 0; } + +time_t gm_mktime(struct tm *tm) +{ + setenv("TZ", "GMT0", 1); + time_t res = mktime(tm); + if (*config.device.tzdef) { + setenv("TZ", config.device.tzdef, 1); + } else { + setenv("TZ", "UTC0", 1); + } + return res; +} diff --git a/src/gps/RTC.h b/src/gps/RTC.h index f74e17cd0a..0561819bd9 100644 --- a/src/gps/RTC.h +++ b/src/gps/RTC.h @@ -36,6 +36,8 @@ uint32_t getValidTime(RTCQuality minQuality, bool local = false); void readFromRTC(); +time_t gm_mktime(struct tm *tm); + #define SEC_PER_DAY 86400 #define SEC_PER_HOUR 3600 #define SEC_PER_MIN 60 \ No newline at end of file