mirror of
https://github.com/esphome/esphome.git
synced 2025-09-08 06:12:20 +01:00
[core] Fix timezone offset calculation (#10426)
This commit is contained in:
@@ -203,27 +203,13 @@ void ESPTime::recalc_timestamp_local() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int32_t ESPTime::timezone_offset() {
|
int32_t ESPTime::timezone_offset() {
|
||||||
int32_t offset = 0;
|
|
||||||
time_t now = ::time(nullptr);
|
time_t now = ::time(nullptr);
|
||||||
auto local = ESPTime::from_epoch_local(now);
|
struct tm local_tm = *::localtime(&now);
|
||||||
auto utc = ESPTime::from_epoch_utc(now);
|
local_tm.tm_isdst = 0; // Cause mktime to ignore daylight saving time because we want to include it in the offset.
|
||||||
bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
|
time_t local_time = mktime(&local_tm);
|
||||||
|
struct tm utc_tm = *::gmtime(&now);
|
||||||
if (utc.minute > local.minute) {
|
time_t utc_time = mktime(&utc_tm);
|
||||||
local.minute += 60;
|
return static_cast<int32_t>(local_time - utc_time);
|
||||||
local.hour -= 1;
|
|
||||||
}
|
|
||||||
offset += (local.minute - utc.minute) * 60;
|
|
||||||
|
|
||||||
if (negative) {
|
|
||||||
offset -= (utc.hour - local.hour) * 3600;
|
|
||||||
} else {
|
|
||||||
if (utc.hour > local.hour) {
|
|
||||||
local.hour += 24;
|
|
||||||
}
|
|
||||||
offset += (local.hour - utc.hour) * 3600;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
|
bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
|
||||||
|
Reference in New Issue
Block a user