tryfix: use UTC on Phone API (#3576)

This commit is contained in:
Thomas Göttgens 2024-04-09 00:26:23 +02:00 committed by GitHub
parent b14ac777f1
commit ea61808fd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 12 deletions

View File

@ -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()) {

View File

@ -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(

View File

@ -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;
}

View File

@ -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