mirror of
https://github.com/esphome/esphome.git
synced 2026-10-06 19:06:37 +00:00
[time] Fix strftime %Z and %z returning wrong timezone (#15330)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "posix_tz.h"
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
|
||||
namespace esphome::time {
|
||||
|
||||
@@ -442,6 +443,18 @@ bool parse_posix_tz(const char *tz_string, ParsedTimezone &result) {
|
||||
return internal::parse_dst_rule(p, result.dst_end);
|
||||
}
|
||||
|
||||
// Format a POSIX offset (positive = west) as "+HHMM" / "-HHMM" for display.
|
||||
// Convention: negate POSIX sign so east-of-UTC is positive (ISO 8601 / RFC 2822).
|
||||
void format_designation(int32_t posix_offset, char *buf, size_t buf_size) {
|
||||
int32_t display = -posix_offset;
|
||||
char sign = display >= 0 ? '+' : '-';
|
||||
if (display < 0)
|
||||
display = -display;
|
||||
int h = display / 3600;
|
||||
int m = (display % 3600) / 60;
|
||||
snprintf(buf, buf_size, "%c%02d%02d", sign, h, m);
|
||||
}
|
||||
|
||||
bool epoch_to_local_tm(time_t utc_epoch, const ParsedTimezone &tz, struct tm *out_tm) {
|
||||
if (!out_tm) {
|
||||
return false;
|
||||
|
||||
@@ -36,6 +36,9 @@ struct ParsedTimezone {
|
||||
bool has_dst() const { return this->dst_start.type != DSTRuleType::NONE; }
|
||||
};
|
||||
|
||||
/// Format a POSIX offset as "+HHMM"/"-HHMM" into buf (must be >= 6 bytes).
|
||||
void format_designation(int32_t posix_offset, char *buf, size_t buf_size);
|
||||
|
||||
/// Parse a POSIX TZ string into a ParsedTimezone struct.
|
||||
///
|
||||
/// @deprecated Remove before 2026.9.0 (bridge code for backward compatibility).
|
||||
|
||||
Reference in New Issue
Block a user