smaller rtc fixes
This commit is contained in:
@@ -13,6 +13,23 @@ static uint8_t dec_to_bcd(uint8_t val) {
|
||||
return static_cast<uint8_t>(((val / 10) << 4) | (val % 10));
|
||||
}
|
||||
|
||||
static time_t timegm_fallback(struct tm *tm_utc) {
|
||||
if (!tm_utc) {
|
||||
return static_cast<time_t>(-1);
|
||||
}
|
||||
char *old_tz = getenv("TZ");
|
||||
setenv("TZ", "UTC0", 1);
|
||||
tzset();
|
||||
time_t t = mktime(tm_utc);
|
||||
if (old_tz) {
|
||||
setenv("TZ", old_tz, 1);
|
||||
} else {
|
||||
unsetenv("TZ");
|
||||
}
|
||||
tzset();
|
||||
return t;
|
||||
}
|
||||
|
||||
static bool read_registers(uint8_t start_reg, uint8_t *out, size_t len) {
|
||||
if (!out || len == 0) {
|
||||
return false;
|
||||
@@ -77,7 +94,7 @@ bool rtc_ds3231_read_epoch(uint32_t &epoch_utc) {
|
||||
tm_utc.tm_year = year - 1900;
|
||||
tm_utc.tm_isdst = 0;
|
||||
|
||||
time_t t = timegm(&tm_utc);
|
||||
time_t t = timegm_fallback(&tm_utc);
|
||||
if (t <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user