[time] Fix strftime %Z and %z returning wrong timezone (#15330)

This commit is contained in:
J. Nick Koston
2026-04-01 11:59:45 +13:00
committed by GitHub
parent 9dca7e0daf
commit 23dcc5389d
3 changed files with 68 additions and 2 deletions
+13
View File
@@ -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;
+3
View File
@@ -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).