mirror of
https://github.com/esphome/esphome.git
synced 2026-03-23 13:22:45 +08:00
[time] Fix timezone_offset() and recalc_timestamp_local() always returning UTC (#14996)
This commit is contained in:
@@ -283,19 +283,15 @@ void ESPTime::recalc_timestamp_local() {
|
|||||||
bool dst_valid = time::is_in_dst(utc_if_dst, tz);
|
bool dst_valid = time::is_in_dst(utc_if_dst, tz);
|
||||||
bool std_valid = !time::is_in_dst(utc_if_std, tz);
|
bool std_valid = !time::is_in_dst(utc_if_std, tz);
|
||||||
|
|
||||||
if (dst_valid && std_valid) {
|
if (dst_valid && !std_valid) {
|
||||||
// Ambiguous time (repeated hour during fall-back) - prefer standard time
|
|
||||||
this->timestamp = utc_if_std;
|
|
||||||
} else if (dst_valid) {
|
|
||||||
// Only DST interpretation is valid
|
// Only DST interpretation is valid
|
||||||
this->timestamp = utc_if_dst;
|
this->timestamp = utc_if_dst;
|
||||||
} else if (std_valid) {
|
|
||||||
// Only standard interpretation is valid
|
|
||||||
this->timestamp = utc_if_std;
|
|
||||||
} else {
|
} else {
|
||||||
// Invalid time (skipped hour during spring-forward)
|
// All other cases use standard offset:
|
||||||
// libc normalizes forward: 02:30 CST -> 08:30 UTC -> 03:30 CDT
|
// - Both valid (ambiguous fall-back repeated hour): prefer standard time
|
||||||
// Using std offset achieves this since the UTC result falls during DST
|
// - Only standard valid: straightforward
|
||||||
|
// - Neither valid (spring-forward skipped hour): std offset normalizes
|
||||||
|
// forward to match libc mktime(), e.g. 02:30 CST -> 03:30 CDT
|
||||||
this->timestamp = utc_if_std;
|
this->timestamp = utc_if_std;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/defines.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|||||||
Reference in New Issue
Block a user