Skip to content

Commit

Permalink
tryfix: use UTC on Phone API (#3576)
Browse files Browse the repository at this point in the history
  • Loading branch information
caveman99 committed Apr 8, 2024
1 parent b14ac77 commit ea61808
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions src/gps/NMEAWPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 15 additions & 9 deletions src/gps/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/gps/RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ea61808

Please sign in to comment.